array-basic.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. test("basic functionality", () => {
  2. var a = [1, 2, 3];
  3. expect(typeof a).toBe("object");
  4. expect(a).toHaveLength(3);
  5. expect(a[0]).toBe(1);
  6. expect(a[1]).toBe(2);
  7. expect(a[2]).toBe(3);
  8. a[1] = 5;
  9. expect(a[1]).toBe(5);
  10. expect(a).toHaveLength(3);
  11. a.push(7);
  12. expect(a[3]).toBe(7);
  13. expect(a).toHaveLength(4);
  14. a = [,];
  15. expect(a).toHaveLength(1);
  16. expect(a.toString()).toBe("");
  17. expect(a[0]).toBeUndefined();
  18. a = [, , , ,];
  19. expect(a).toHaveLength(4);
  20. expect(a.toString()).toBe(",,,");
  21. expect(a[0]).toBeUndefined();
  22. expect(a[1]).toBeUndefined();
  23. expect(a[2]).toBeUndefined();
  24. expect(a[3]).toBeUndefined();
  25. a = [1, , 2, , , 3];
  26. expect(a).toHaveLength(6);
  27. expect(a.toString()).toBe("1,,2,,,3");
  28. expect(a[0]).toBe(1);
  29. expect(a[1]).toBeUndefined();
  30. expect(a[2]).toBe(2);
  31. expect(a[3]).toBeUndefined();
  32. expect(a[4]).toBeUndefined();
  33. expect(a[5]).toBe(3);
  34. a = [1, , 2, , , 3];
  35. Object.defineProperty(a, 1, {
  36. get() {
  37. return this.getterSetterValue;
  38. },
  39. set(value) {
  40. this.getterSetterValue = value;
  41. },
  42. });
  43. expect(a).toHaveLength(6);
  44. expect(a.toString()).toBe("1,,2,,,3");
  45. expect(a.getterSetterValue).toBeUndefined();
  46. a[1] = 20;
  47. expect(a).toHaveLength(6);
  48. expect(a.toString()).toBe("1,20,2,,,3");
  49. expect(a.getterSetterValue).toBe(20);
  50. });