Array.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. test("constructor properties", () => {
  2. expect(Array).toHaveLength(1);
  3. expect(Array.name).toBe("Array");
  4. expect(Array.prototype.length).toBe(0);
  5. });
  6. describe("errors", () => {
  7. test("invalid array length", () => {
  8. [-1, -100, -0.1, 0.1, 1.23, Infinity, -Infinity, NaN].forEach(value => {
  9. expect(() => {
  10. new Array(value);
  11. }).toThrowWithMessage(RangeError, "Invalid array length");
  12. });
  13. });
  14. });
  15. describe("normal behavior", () => {
  16. test("typeof", () => {
  17. expect(typeof Array()).toBe("object");
  18. expect(typeof new Array()).toBe("object");
  19. });
  20. test("constructor with single numeric argument", () => {
  21. var a = new Array(5);
  22. expect(a instanceof Array).toBeTrue();
  23. expect(a).toHaveLength(5);
  24. });
  25. test("constructor with single non-numeric argument", () => {
  26. var a = new Array("5");
  27. expect(a instanceof Array).toBeTrue();
  28. expect(a).toHaveLength(1);
  29. expect(a[0]).toBe("5");
  30. });
  31. test("constructor with multiple numeric arguments", () => {
  32. var a = new Array(1, 2, 3);
  33. expect(a instanceof Array).toBeTrue();
  34. expect(a).toHaveLength(3);
  35. expect(a[0]).toBe(1);
  36. expect(a[1]).toBe(2);
  37. expect(a[2]).toBe(3);
  38. });
  39. test("constructor with single array argument", () => {
  40. var a = new Array([1, 2, 3]);
  41. expect(a instanceof Array).toBeTrue();
  42. expect(a).toHaveLength(1);
  43. expect(a[0][0]).toBe(1);
  44. expect(a[0][1]).toBe(2);
  45. expect(a[0][2]).toBe(3);
  46. });
  47. });