Reflect.defineProperty.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. test("length is 3", () => {
  2. expect(Reflect.defineProperty).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.defineProperty(value);
  9. }).toThrowWithMessage(TypeError, "First argument of Reflect.defineProperty() must be an object");
  10. });
  11. });
  12. test("descriptor must be an object", () => {
  13. [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
  14. expect(() => {
  15. Reflect.defineProperty({}, "foo", value);
  16. }).toThrowWithMessage(TypeError, "Descriptor argument is not an object");
  17. });
  18. });
  19. });
  20. describe("normal behavior", () => {
  21. test("initial value and non-writable", () => {
  22. var o = {};
  23. expect(o.foo).toBeUndefined();
  24. expect(Reflect.defineProperty(o, "foo", { value: 1, writable: false })).toBeTrue();
  25. expect(o.foo).toBe(1);
  26. o.foo = 2;
  27. expect(o.foo).toBe(1);
  28. });
  29. test("initial value and writable", () => {
  30. var o = {};
  31. expect(o.foo).toBeUndefined();
  32. expect(Reflect.defineProperty(o, "foo", { value: 1, writable: true })).toBeTrue();
  33. expect(o.foo).toBe(1);
  34. o.foo = 2;
  35. expect(o.foo).toBe(2);
  36. });
  37. test("can redefine value of configurable, writable property", () => {
  38. var o = {};
  39. expect(o.foo).toBeUndefined();
  40. expect(Reflect.defineProperty(o, "foo", { value: 1, configurable: true, writable: true })).toBeTrue();
  41. expect(o.foo).toBe(1);
  42. expect(Reflect.defineProperty(o, "foo", { value: 2 })).toBeTrue();
  43. expect(o.foo).toBe(2);
  44. });
  45. test("can redefine value of configurable, non-writable property", () => {
  46. var o = {};
  47. expect(o.foo).toBeUndefined();
  48. expect(Reflect.defineProperty(o, "foo", { value: 1, configurable: true, writable: false })).toBeTrue();
  49. expect(o.foo).toBe(1);
  50. expect(Reflect.defineProperty(o, "foo", { value: 2 })).toBeTrue();
  51. expect(o.foo).toBe(2);
  52. });
  53. test("cannot redefine value of non-configurable, non-writable property", () => {
  54. var o = {};
  55. expect(o.foo).toBeUndefined();
  56. expect(Reflect.defineProperty(o, "foo", { value: 1, configurable: false, writable: false })).toBeTrue();
  57. expect(o.foo).toBe(1);
  58. expect(Reflect.defineProperty(o, "foo", { value: 2 })).toBeFalse();
  59. expect(o.foo).toBe(1);
  60. });
  61. });