mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:57:44 +00:00
LibJS: Implement Proxy.revocable()
This commit is contained in:
parent
e39dd65cf0
commit
9b35231453
5 changed files with 121 additions and 10 deletions
|
@ -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();
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue