ladybird/Userland/Libraries/LibJS/Tests/builtins/Object/Object.seal.js
Linus Groh ac3e7ef791 LibJS: Fix crash in Object.{freeze,seal}() with indexed properties
This was failing to take two things into account:

- When constructing a PropertyName from a value, it won't automatically
  convert to Type::Number for something like string "0", even though
  that's how things work internally, since indexed properties are stored
  separately. This will be improved in a future patch, it's a footgun
  and should happen automatically.
- Those can't be looked up on the shape, we have to go through the
  indexed properties instead.

Additionally it now operates on the shape or indexed properties directly
as define_property() was overly strict and would throw if a property was
already non-configurable.

Fixes #6469.
2021-04-20 09:38:22 +02:00

75 lines
2.2 KiB
JavaScript

test("length is 1", () => {
expect(Object.seal).toHaveLength(1);
});
describe("normal behavior", () => {
test("returns given argument", () => {
const o = {};
expect(Object.seal(42)).toBe(42);
expect(Object.seal("foobar")).toBe("foobar");
expect(Object.seal(o)).toBe(o);
});
test("prevents addition of new properties", () => {
const o = {};
expect(o.foo).toBeUndefined();
Object.seal(o);
o.foo = "bar";
expect(o.foo).toBeUndefined();
});
test("prevents deletion of existing properties", () => {
const o = { foo: "bar" };
expect(o.foo).toBe("bar");
Object.seal(o);
delete o.foo;
expect(o.foo).toBe("bar");
});
test("prevents changing attributes of existing properties", () => {
const o = { foo: "bar" };
Object.seal(o);
// FIXME: These don't change anything and should not throw!
// expect(Object.defineProperty(o, "foo", {})).toBe(o);
// expect(Object.defineProperty(o, "foo", { configurable: false })).toBe(o);
expect(() => {
Object.defineProperty(o, "foo", { configurable: true });
}).toThrowWithMessage(
TypeError,
"Cannot change attributes of non-configurable property 'foo'"
);
});
test("doesn't prevent changing value of existing properties", () => {
const o = { foo: "bar" };
expect(o.foo).toBe("bar");
Object.seal(o);
o.foo = "baz";
expect(o.foo).toBe("baz");
});
// #6469
test("works with indexed properties", () => {
const a = ["foo"];
expect(a[0]).toBe("foo");
Object.seal(a);
a[0] = "bar";
a[1] = "baz";
expect(a[0]).toBe("bar");
expect(a[1]).toBeUndefined();
});
test("works with properties that are already non-configurable", () => {
const o = {};
Object.defineProperty(o, "foo", {
value: "bar",
configurable: false,
writable: true,
enumerable: true,
});
expect(o.foo).toBe("bar");
Object.seal(o);
o.foo = "baz";
expect(o.foo).toBe("baz");
});
});