1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 13:07:46 +00:00

LibJS: Implement Proxy.revocable()

This commit is contained in:
Linus Groh 2021-06-08 21:48:43 +01:00 committed by Andreas Kling
parent e39dd65cf0
commit 9b35231453
5 changed files with 121 additions and 10 deletions

View file

@ -0,0 +1,72 @@
test("length is 2", () => {
expect(Proxy.revocable).toHaveLength(2);
});
describe("errors", () => {
test("constructor argument count", () => {
expect(() => {
Proxy.revocable();
}).toThrowWithMessage(
TypeError,
"Expected target argument of Proxy constructor to be object, got undefined"
);
expect(() => {
Proxy.revocable({});
}).toThrowWithMessage(
TypeError,
"Expected handler argument of Proxy constructor to be object, got undefined"
);
});
test("constructor requires objects", () => {
expect(() => {
Proxy.revocable(1, {});
}).toThrowWithMessage(
TypeError,
"Expected target argument of Proxy constructor to be object, got 1"
);
expect(() => {
Proxy.revocable({}, 1);
}).toThrowWithMessage(
TypeError,
"Expected handler argument of Proxy constructor to be object, got 1"
);
});
});
describe("normal behavior", () => {
test("returns object with 'proxy' and 'revoke' properties", () => {
const revocable = Proxy.revocable(
{},
{
get() {
return 42;
},
}
);
expect(typeof revocable).toBe("object");
expect(Object.getPrototypeOf(revocable)).toBe(Object.prototype);
expect(revocable.hasOwnProperty("proxy")).toBeTrue();
expect(revocable.hasOwnProperty("revoke")).toBeTrue();
expect(typeof revocable.revoke).toBe("function");
// Can't `instanceof Proxy`, but this should do the trick :^)
expect(revocable.proxy.foo).toBe(42);
});
test("'revoke' function revokes Proxy", () => {
const revocable = Proxy.revocable({}, {});
expect(revocable.proxy.foo).toBeUndefined();
expect(revocable.revoke()).toBeUndefined();
expect(() => {
revocable.proxy.foo;
}).toThrowWithMessage(TypeError, "An operation was performed on a revoked Proxy object");
});
test("'revoke' called multiple times is a noop", () => {
const revocable = Proxy.revocable({}, {});
expect(revocable.revoke()).toBeUndefined();
expect(revocable.revoke()).toBeUndefined();
});
});