Reflect.construct.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. test("length is 2", () => {
  2. expect(Reflect.construct).toHaveLength(2);
  3. });
  4. describe("errors", () => {
  5. test("target must be a function", () => {
  6. [null, undefined, "foo", 123, NaN, Infinity, {}].forEach(value => {
  7. expect(() => {
  8. Reflect.construct(value);
  9. }).toThrowWithMessage(
  10. TypeError,
  11. "First argument of Reflect.construct() must be a function"
  12. );
  13. });
  14. });
  15. test("arguments list must be an object", () => {
  16. [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
  17. expect(() => {
  18. Reflect.construct(() => {}, value);
  19. }).toThrowWithMessage(TypeError, "Arguments list must be an object");
  20. });
  21. });
  22. test("new target must be a function", () => {
  23. [null, undefined, "foo", 123, NaN, Infinity, {}].forEach(value => {
  24. expect(() => {
  25. Reflect.construct(() => {}, [], value);
  26. }).toThrowWithMessage(
  27. TypeError,
  28. "Optional third argument of Reflect.construct() must be a constructor"
  29. );
  30. });
  31. });
  32. });
  33. describe("normal behavior", () => {
  34. test("built-in Array function", () => {
  35. var a = Reflect.construct(Array, [5]);
  36. expect(a instanceof Array).toBeTrue();
  37. expect(a).toHaveLength(5);
  38. });
  39. test("built-in String function", () => {
  40. var s = Reflect.construct(String, [123]);
  41. expect(s instanceof String).toBeTrue();
  42. expect(s).toHaveLength(3);
  43. expect(s.toString()).toBe("123");
  44. });
  45. test("user-defined function", () => {
  46. function Foo() {
  47. this.name = "foo";
  48. }
  49. var o = Reflect.construct(Foo, []);
  50. expect(o.name).toBe("foo");
  51. expect(o instanceof Foo).toBeTrue();
  52. });
  53. test("user-defined function with different new target", () => {
  54. function Foo() {
  55. this.name = "foo";
  56. }
  57. function Bar() {
  58. this.name = "bar";
  59. }
  60. var o = Reflect.construct(Foo, [], Bar);
  61. expect(o.name).toBe("foo");
  62. expect(o instanceof Foo).toBeFalse();
  63. expect(o instanceof Bar).toBeTrue();
  64. });
  65. });