summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/builtins/Proxy/Proxy.handler-has.js
blob: aa1f965ce4ada5019a4547dfad0ace6cedd658b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
describe("[[Has]] trap normal behavior", () => {
    test("forwarding when not defined in handler", () => {
        expect("foo" in new Proxy({}, { has: null })).toBe(false);
        expect("foo" in new Proxy({}, { has: undefined})).toBe(false);
        expect("foo" in new Proxy({}, {})).toBe(false);
    });

    test("correct arguments supplied to trap", () => {
        let o = {};
        let p = new Proxy(o, {
            has(target, prop) {
                expect(target).toBe(o);
                expect(prop).toBe("foo");
                return true;
            }
        });

        "foo" in p;
    });

    test("conditional return", () => {
        let o = {};
        let p = new Proxy(o, {
            has(target, prop) {
                if (target.checkedFoo)
                    return true;
                if (prop === "foo")
                    target.checkedFoo = true;
                return false;
            }
        });

        expect("foo" in p).toBe(false);
        expect("foo" in p).toBe(true);
    });
});

describe("[[Has]] invariants", () => {
    test("cannot return false if the property exists and is non-configurable", () => {
        let o = {};
        Object.defineProperty(o, "foo", { configurable: false });

        p = new Proxy(o, {
            has() {
                return false;
            }
        });

        expect(() => {
            "foo" in p;
        }).toThrowWithMessage(TypeError, "Proxy handler's has trap violates invariant: a property cannot be reported as non-existent if it exists on the target as a non-configurable property");
    });

    test("cannot return false if the property exists and the target is non-extensible", () => {
        let o = {};
        Object.defineProperty(o, "foo", { value: 10, configurable: true });

        let p = new Proxy(o, {
            has() {
                return false;
            }
        });

        Object.preventExtensions(o);

        expect(() => {
            "foo" in p;
        }).toThrowWithMessage(TypeError, "Proxy handler's has trap violates invariant: a property cannot be reported as non-existent if it exists on the target and the target is non-extensible");
    });
});