summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/object-getter-setter-shorthand.js
blob: 02b513800bf35f8fbe658a1813b85daf3179bc7d (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
test("normal methods named get and set", () => {
    let o = {
        get() {
            return 5;
        },
        set() {
            return 10;
        },
    };
    expect(o.get()).toBe(5);
    expect(o.set()).toBe(10);
});

test("basic get and set", () => {
    let o = {
        get x() {
            return 5;
        },
        set x(_) {},
    };
    expect(o.x).toBe(5);
    o.x = 10;
    expect(o.x).toBe(5);
});

test("get and set with 'this'", () => {
    let o = {
        get x() {
            return this._x + 1;
        },
        set x(value) {
            this._x = value + 1;
        },
    };

    expect(o.x).toBeNaN();
    o.x = 10;
    expect(o.x).toBe(12);
    o.x = 20;
    expect(o.x).toBe(22);
});

test("multiple getters", () => {
    let o = {
        get x() {
            return 5;
        },
        get x() {
            return 10;
        },
    };
    expect(o.x).toBe(10);
});

test("setter return value", () => {
    o = {
        set x(value) {
            return 10;
        },
    };
    expect((o.x = 20)).toBe(20);
});