summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/builtins/Reflect/Reflect.set.js
blob: 854fb23888ed3080a46fec46c03df6f2cf4f84d3 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
test("length is 3", () => {
    expect(Reflect.set).toHaveLength(3);
});

describe("errors", () => {
    test("target must be an object", () => {
        [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
            expect(() => {
                Reflect.set(value);
            }).toThrowWithMessage(TypeError, "First argument of Reflect.set() must be an object");
        });
    });
});

describe("normal behavior", () => {
    test("setting properties of regular object", () => {
        var o = {};

        expect(Reflect.set(o)).toBeTrue();
        expect(o.undefined).toBeUndefined();

        expect(Reflect.set(o, "foo")).toBeTrue();
        expect(o.foo).toBeUndefined();

        expect(Reflect.set(o, "foo", "bar")).toBeTrue();
        expect(o.foo).toBe("bar");

        expect(Reflect.set(o, "foo", 42)).toBeTrue();
        expect(o.foo).toBe(42);
    });

    test("setting configurable, non-writable property of regular object", () => {
        var o = {};
        Object.defineProperty(o, "foo", { value: 1, configurable: true, writable: false });
        expect(Reflect.set(o, "foo", 2)).toBeFalse();
        expect(o.foo).toBe(1);
    });

    test("setting non-configurable, writable property of regular object", () => {
        var o = {};
        Object.defineProperty(o, "foo", { value: 1, configurable: false, writable: true });
        expect(Reflect.set(o, "foo", 2)).toBeTrue();
        expect(o.foo).toBe(2);
    });

    test("", () => {
        var a = [];
        expect(a.length === 0);
        expect(Reflect.set(a, "0")).toBeTrue();
        expect(a.length === 1);
        expect(a[0]).toBeUndefined();
        expect(Reflect.set(a, 1, "foo")).toBeTrue();
        expect(a.length === 2);
        expect(a[0]).toBeUndefined();
        expect(a[1] === "foo");
        expect(Reflect.set(a, 4, "bar")).toBeTrue();
        expect(a.length === 5);
        expect(a[0]).toBeUndefined();
        expect(a[1] === "foo");
        expect(a[2]).toBeUndefined();
        expect(a[3]).toBeUndefined();
        expect(a[4] === "bar");
    });

    test("setting setter property of regular object", () => {
        const foo = {
            set prop(value) {
                this.setPropCalled = true;
            },
        };
        expect(foo.setPropCalled).toBeUndefined();
        Reflect.set(foo, "prop", 42);
        expect(foo.setPropCalled).toBeTrue();
    });

    test("setting setter property of regular object with different receiver", () => {
        const foo = {
            set prop(value) {
                this.setPropCalled = true;
            },
        };
        const bar = {};
        Object.setPrototypeOf(bar, foo);

        expect(foo.setPropCalled).toBeUndefined();
        expect(bar.setPropCalled).toBeUndefined();

        Reflect.set(bar, "prop", 42);
        expect(foo.setPropCalled).toBeUndefined();
        expect(bar.setPropCalled).toBeTrue();

        Reflect.set(bar, "prop", 42, foo);
        expect(foo.setPropCalled).toBeTrue();
        expect(bar.setPropCalled).toBeTrue();
    });

    test("native setter function", () => {
        const e = new Error();
        Reflect.set(Error.prototype, "name", "Foo", e);
        expect(e.name).toBe("Foo");
    });
});