Object.freeze.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. test("length is 1", () => {
  2. expect(Object.freeze).toHaveLength(1);
  3. });
  4. describe("normal behavior", () => {
  5. test("returns given argument", () => {
  6. const o = {};
  7. expect(Object.freeze(42)).toBe(42);
  8. expect(Object.freeze("foobar")).toBe("foobar");
  9. expect(Object.freeze(o)).toBe(o);
  10. });
  11. test("prevents addition of new properties", () => {
  12. const o = {};
  13. expect(o.foo).toBeUndefined();
  14. Object.freeze(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.freeze(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.freeze(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("prevents changing value of existing properties", () => {
  35. const o = { foo: "bar" };
  36. expect(o.foo).toBe("bar");
  37. Object.freeze(o);
  38. o.foo = "baz";
  39. expect(o.foo).toBe("bar");
  40. });
  41. // #6469
  42. test("works with indexed properties", () => {
  43. const a = ["foo"];
  44. expect(a[0]).toBe("foo");
  45. Object.freeze(a);
  46. a[0] = "bar";
  47. expect(a[0]).toBe("foo");
  48. });
  49. test("works with properties that are already non-configurable", () => {
  50. const o = {};
  51. Object.defineProperty(o, "foo", {
  52. value: "bar",
  53. configurable: false,
  54. writable: true,
  55. enumerable: true,
  56. });
  57. expect(o.foo).toBe("bar");
  58. Object.freeze(o);
  59. o.foo = "baz";
  60. expect(o.foo).toBe("bar");
  61. });
  62. });