Object.seal.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. expect(Object.defineProperty(o, "foo", {})).toBe(o);
  29. expect(Object.defineProperty(o, "foo", { configurable: false })).toBe(o);
  30. expect(() => {
  31. Object.defineProperty(o, "foo", { configurable: true });
  32. }).toThrowWithMessage(TypeError, "Object's [[DefineOwnProperty]] method returned false");
  33. });
  34. test("doesn't prevent changing value of existing properties", () => {
  35. const o = { foo: "bar" };
  36. expect(o.foo).toBe("bar");
  37. Object.seal(o);
  38. o.foo = "baz";
  39. expect(o.foo).toBe("baz");
  40. });
  41. // #6469
  42. test("works with indexed properties", () => {
  43. const a = ["foo"];
  44. expect(a[0]).toBe("foo");
  45. Object.seal(a);
  46. a[0] = "bar";
  47. a[1] = "baz";
  48. expect(a[0]).toBe("bar");
  49. expect(a[1]).toBeUndefined();
  50. });
  51. test("works with properties that are already non-configurable", () => {
  52. const o = {};
  53. Object.defineProperty(o, "foo", {
  54. value: "bar",
  55. configurable: false,
  56. writable: true,
  57. enumerable: true,
  58. });
  59. expect(o.foo).toBe("bar");
  60. Object.seal(o);
  61. o.foo = "baz";
  62. expect(o.foo).toBe("baz");
  63. });
  64. });