array-basic.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. });
  51. test("assigning array expression with destination referenced in array expression", () => {
  52. function go(i) {
  53. var i = [i];
  54. return i;
  55. }
  56. expect(go("foo")).toEqual(["foo"]);
  57. });