summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/classes/class-static-setters.js
blob: 38cbbe23940204767559aaf15637300f453efd98 (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
103
104
105
106
107
108
109
110
111
112
describe("correct behavior", () => {
    test("basic functionality", () => {
        class A {
            static get x() {
                return this._x;
            }

            static set x(value) {
                this._x = value * 2;
            }
        }

        expect(A.x).toBeUndefined();
        expect(A).not.toHaveProperty("_x");
        A.x = 3;
        expect(A.x).toBe(6);
        expect(A).toHaveProperty("_x", 6);
    });

    test("name", () => {
        class A {
            static set x(v) {}
        }

        const d = Object.getOwnPropertyDescriptor(A, "x");
        expect(d.set.name).toBe("set x");
    });

    test("extended name syntax", () => {
        const s = Symbol("foo");

        class A {
            static set "method with space"(value) {
                this.a = value;
            }

            static set 12(value) {
                this.b = value;
            }

            static set [`he${"llo"}`](value) {
                this.c = value;
            }

            static set [s](value) {
                this.d = value;
            }
        }

        A["method with space"] = 1;
        A[12] = 2;
        A.hello = 3;
        A[s] = 4;
        expect(A.a).toBe(1);
        expect(A.b).toBe(2);
        expect(A.c).toBe(3);
        expect(A.d).toBe(4);
    });

    test("inherited static setter", () => {
        class Parent {
            static get x() {
                return this._x;
            }

            static set x(value) {
                this._x = value * 2;
            }
        }

        class Child extends Parent {}

        expect(Child.x).toBeUndefined();
        Child.x = 10;
        expect(Child.x).toBe(20);
    });

    test("inherited static setter overriding", () => {
        class Parent {
            static get x() {
                return this._x;
            }

            static set x(value) {
                this._x = value * 2;
            }
        }

        class Child extends Parent {
            static get x() {
                return this._x;
            }

            static set x(value) {
                this._x = value * 3;
            }
        }

        expect(Child.x).toBeUndefined();
        Child.x = 10;
        expect(Child.x).toBe(30);
    });
});

describe("errors", () => {
    test('"set static" is a syntax error', () => {
        expect(`
    class A {
      set static foo(value) {}
    }`).not.toEval();
    });
});