Reflect.deleteProperty.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. test("length is 2", () => {
  2. expect(Reflect.deleteProperty).toHaveLength(2);
  3. });
  4. describe("errors", () => {
  5. test("target must be an object", () => {
  6. [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
  7. expect(() => {
  8. Reflect.deleteProperty(value);
  9. }).toThrowWithMessage(
  10. TypeError,
  11. "First argument of Reflect.deleteProperty() must be an object"
  12. );
  13. });
  14. });
  15. });
  16. describe("normal behavior", () => {
  17. test("deleting non-existent property", () => {
  18. expect(Reflect.deleteProperty({})).toBeTrue();
  19. expect(Reflect.deleteProperty({}, "foo")).toBeTrue();
  20. });
  21. test("deleting existent property", () => {
  22. var o = { foo: 1 };
  23. expect(o.foo).toBe(1);
  24. expect(Reflect.deleteProperty(o, "foo")).toBeTrue();
  25. expect(o.foo).toBeUndefined();
  26. expect(Reflect.deleteProperty(o, "foo")).toBeTrue();
  27. expect(o.foo).toBeUndefined();
  28. });
  29. test("deleting existent, configurable, non-writable property", () => {
  30. var o = {};
  31. Object.defineProperty(o, "foo", { value: 1, configurable: true, writable: false });
  32. expect(Reflect.deleteProperty(o, "foo")).toBeTrue();
  33. expect(o.foo).toBeUndefined();
  34. });
  35. test("deleting existent, non-configurable, writable property", () => {
  36. var o = {};
  37. Object.defineProperty(o, "foo", { value: 1, configurable: false, writable: true });
  38. expect(Reflect.deleteProperty(o, "foo")).toBeFalse();
  39. expect(o.foo).toBe(1);
  40. });
  41. test("deleting existent, non-configurable, non-writable property", () => {
  42. var o = {};
  43. Object.defineProperty(o, "foo", { value: 1, configurable: false, writable: false });
  44. expect(Reflect.deleteProperty(o, "foo")).toBeFalse();
  45. expect(o.foo).toBe(1);
  46. });
  47. test("deleting array index", () => {
  48. var a = [1, 2, 3];
  49. expect(a).toHaveLength(3);
  50. expect(a[0]).toBe(1);
  51. expect(a[1]).toBe(2);
  52. expect(a[2]).toBe(3);
  53. expect(Reflect.deleteProperty(a, 1)).toBeTrue();
  54. expect(a).toHaveLength(3);
  55. expect(a[0]).toBe(1);
  56. expect(a[1]).toBeUndefined();
  57. expect(a[2]).toBe(3);
  58. });
  59. });