Object.seal.js 2.2 KB

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