Reflect.deleteProperty.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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(TypeError, `${value} is not an object`);
  10. });
  11. });
  12. });
  13. describe("normal behavior", () => {
  14. test("deleting non-existent property", () => {
  15. expect(Reflect.deleteProperty({})).toBeTrue();
  16. expect(Reflect.deleteProperty({}, "foo")).toBeTrue();
  17. });
  18. test("deleting existent property", () => {
  19. var o = { foo: 1 };
  20. expect(o.foo).toBe(1);
  21. expect(Reflect.deleteProperty(o, "foo")).toBeTrue();
  22. expect(o.foo).toBeUndefined();
  23. expect(Reflect.deleteProperty(o, "foo")).toBeTrue();
  24. expect(o.foo).toBeUndefined();
  25. });
  26. test("deleting existent, configurable, non-writable property", () => {
  27. var o = {};
  28. Object.defineProperty(o, "foo", { value: 1, configurable: true, writable: false });
  29. expect(Reflect.deleteProperty(o, "foo")).toBeTrue();
  30. expect(o.foo).toBeUndefined();
  31. });
  32. test("deleting existent, non-configurable, writable property", () => {
  33. var o = {};
  34. Object.defineProperty(o, "foo", { value: 1, configurable: false, writable: true });
  35. expect(Reflect.deleteProperty(o, "foo")).toBeFalse();
  36. expect(o.foo).toBe(1);
  37. });
  38. test("deleting existent, non-configurable, non-writable property", () => {
  39. var o = {};
  40. Object.defineProperty(o, "foo", { value: 1, configurable: false, writable: false });
  41. expect(Reflect.deleteProperty(o, "foo")).toBeFalse();
  42. expect(o.foo).toBe(1);
  43. });
  44. test("deleting array index", () => {
  45. var a = [1, 2, 3];
  46. expect(a).toHaveLength(3);
  47. expect(a[0]).toBe(1);
  48. expect(a[1]).toBe(2);
  49. expect(a[2]).toBe(3);
  50. expect(Reflect.deleteProperty(a, 1)).toBeTrue();
  51. expect(a).toHaveLength(3);
  52. expect(a[0]).toBe(1);
  53. expect(a[1]).toBeUndefined();
  54. expect(a[2]).toBe(3);
  55. });
  56. });