Object.seal.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. test("length is 1", () => {
  2. expect(Object.seal).toHaveLength(1);
  3. });
  4. describe("normal behavior", () => {
  5. test("returns given argument", () => {
  6. const o = {};
  7. expect(Object.seal(42)).toBe(42);
  8. expect(Object.seal("foobar")).toBe("foobar");
  9. expect(Object.seal(o)).toBe(o);
  10. });
  11. test("prevents addition of new properties", () => {
  12. const o = {};
  13. expect(o.foo).toBeUndefined();
  14. Object.seal(o);
  15. o.foo = "bar";
  16. expect(o.foo).toBeUndefined();
  17. });
  18. test("prevents deletion of existing properties", () => {
  19. const o = { foo: "bar" };
  20. expect(o.foo).toBe("bar");
  21. Object.seal(o);
  22. delete o.foo;
  23. expect(o.foo).toBe("bar");
  24. });
  25. test("prevents changing attributes of existing properties", () => {
  26. const o = { foo: "bar" };
  27. Object.seal(o);
  28. // FIXME: These don't change anything and should not throw!
  29. // expect(Object.defineProperty(o, "foo", {})).toBe(o);
  30. // expect(Object.defineProperty(o, "foo", { configurable: false })).toBe(o);
  31. expect(() => {
  32. Object.defineProperty(o, "foo", { configurable: true });
  33. }).toThrowWithMessage(
  34. TypeError,
  35. "Cannot change attributes of non-configurable property 'foo'"
  36. );
  37. });
  38. test("doesn't prevent changing value of existing properties", () => {
  39. const o = { foo: "bar" };
  40. expect(o.foo).toBe("bar");
  41. Object.seal(o);
  42. o.foo = "baz";
  43. expect(o.foo).toBe("baz");
  44. });
  45. // #6469
  46. test("works with indexed properties", () => {
  47. const a = ["foo"];
  48. expect(a[0]).toBe("foo");
  49. Object.seal(a);
  50. a[0] = "bar";
  51. a[1] = "baz";
  52. expect(a[0]).toBe("bar");
  53. expect(a[1]).toBeUndefined();
  54. });
  55. test("works with properties that are already non-configurable", () => {
  56. const o = {};
  57. Object.defineProperty(o, "foo", {
  58. value: "bar",
  59. configurable: false,
  60. writable: true,
  61. enumerable: true,
  62. });
  63. expect(o.foo).toBe("bar");
  64. Object.seal(o);
  65. o.foo = "baz";
  66. expect(o.foo).toBe("baz");
  67. });
  68. });