Reflect.set.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. test("length is 3", () => {
  2. expect(Reflect.set).toHaveLength(3);
  3. });
  4. describe("errors", () => {
  5. test("target must be an object", () => {
  6. [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
  7. expect(() => {
  8. Reflect.set(value);
  9. }).toThrowWithMessage(TypeError, `${value} is not an object`);
  10. });
  11. });
  12. });
  13. describe("normal behavior", () => {
  14. test("setting properties of regular object", () => {
  15. var o = {};
  16. expect(Reflect.set(o)).toBeTrue();
  17. expect(o.undefined).toBeUndefined();
  18. expect(Reflect.set(o, "foo")).toBeTrue();
  19. expect(o.foo).toBeUndefined();
  20. expect(Reflect.set(o, "foo", "bar")).toBeTrue();
  21. expect(o.foo).toBe("bar");
  22. expect(Reflect.set(o, "foo", 42)).toBeTrue();
  23. expect(o.foo).toBe(42);
  24. });
  25. test("setting configurable, non-writable property of regular object", () => {
  26. var o = {};
  27. Object.defineProperty(o, "foo", { value: 1, configurable: true, writable: false });
  28. expect(Reflect.set(o, "foo", 2)).toBeFalse();
  29. expect(o.foo).toBe(1);
  30. });
  31. test("setting non-configurable, writable property of regular object", () => {
  32. var o = {};
  33. Object.defineProperty(o, "foo", { value: 1, configurable: false, writable: true });
  34. expect(Reflect.set(o, "foo", 2)).toBeTrue();
  35. expect(o.foo).toBe(2);
  36. });
  37. test("", () => {
  38. var a = [];
  39. expect(a.length === 0);
  40. expect(Reflect.set(a, "0")).toBeTrue();
  41. expect(a.length === 1);
  42. expect(a[0]).toBeUndefined();
  43. expect(Reflect.set(a, 1, "foo")).toBeTrue();
  44. expect(a.length === 2);
  45. expect(a[0]).toBeUndefined();
  46. expect(a[1] === "foo");
  47. expect(Reflect.set(a, 4, "bar")).toBeTrue();
  48. expect(a.length === 5);
  49. expect(a[0]).toBeUndefined();
  50. expect(a[1] === "foo");
  51. expect(a[2]).toBeUndefined();
  52. expect(a[3]).toBeUndefined();
  53. expect(a[4] === "bar");
  54. });
  55. test("setting setter property of regular object", () => {
  56. const foo = {
  57. set prop(value) {
  58. this.setPropCalled = true;
  59. },
  60. };
  61. expect(foo.setPropCalled).toBeUndefined();
  62. Reflect.set(foo, "prop", 42);
  63. expect(foo.setPropCalled).toBeTrue();
  64. });
  65. test("setting setter property of regular object with different receiver", () => {
  66. const foo = {
  67. set prop(value) {
  68. this.setPropCalled = true;
  69. },
  70. };
  71. const bar = {};
  72. Object.setPrototypeOf(bar, foo);
  73. expect(foo.setPropCalled).toBeUndefined();
  74. expect(bar.setPropCalled).toBeUndefined();
  75. Reflect.set(bar, "prop", 42);
  76. expect(foo.setPropCalled).toBeUndefined();
  77. expect(bar.setPropCalled).toBeTrue();
  78. Reflect.set(bar, "prop", 42, foo);
  79. expect(foo.setPropCalled).toBeTrue();
  80. expect(bar.setPropCalled).toBeTrue();
  81. });
  82. test("native setter function", () => {
  83. const e = new Error();
  84. Reflect.set(Error.prototype, "name", "Foo", e);
  85. expect(e.name).toBe("Foo");
  86. });
  87. });