Reflect.defineProperty.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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, `${value} is not 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, `${value} 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(
  41. Reflect.defineProperty(o, "foo", { value: 1, configurable: true, writable: true })
  42. ).toBeTrue();
  43. expect(o.foo).toBe(1);
  44. expect(Reflect.defineProperty(o, "foo", { value: 2 })).toBeTrue();
  45. expect(o.foo).toBe(2);
  46. });
  47. test("can redefine value of configurable, non-writable property", () => {
  48. var o = {};
  49. expect(o.foo).toBeUndefined();
  50. expect(
  51. Reflect.defineProperty(o, "foo", { value: 1, configurable: true, writable: false })
  52. ).toBeTrue();
  53. expect(o.foo).toBe(1);
  54. expect(Reflect.defineProperty(o, "foo", { value: 2 })).toBeTrue();
  55. expect(o.foo).toBe(2);
  56. });
  57. test("cannot redefine value of non-configurable, non-writable property", () => {
  58. var o = {};
  59. expect(o.foo).toBeUndefined();
  60. expect(
  61. Reflect.defineProperty(o, "foo", { value: 1, configurable: false, writable: false })
  62. ).toBeTrue();
  63. expect(o.foo).toBe(1);
  64. expect(Reflect.defineProperty(o, "foo", { value: 2 })).toBeFalse();
  65. expect(o.foo).toBe(1);
  66. });
  67. });