Reflect.defineProperty.js 2.6 KB

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