summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/classes/class-expressions.js
blob: d714b37f6327ee01c56d605784368de66e462a85 (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
// This file must not be formatted by prettier. Make sure your IDE
// respects the .prettierignore file!

test("basic functionality", () => {
    const A = class {
        constructor(x) {
           this.x = x;
        }

        getX() {
           return this.x * 2;
        }
    };

    expect(new A(10).getX()).toBe(20);
});

test("inline instantiation", () => {
    const a = new class {
        constructor() {
            this.x = 10;
        }

        getX() {
            return this.x * 2;
        }
    };

    expect(a.getX()).toBe(20);
});

test("inline instantiation with argument", () => {
    const a = new class {
        constructor(x) {
            this.x = x;
        }

        getX() {
            return this.x * 2;
        }
    }(10);

    expect(a.getX()).toBe(20);
});

test("extending class expressions", () => {
    class A extends class {
        constructor(x) {
            this.x = x;
        }
    } {
        constructor(y) {
            super(y);
            this.y = y * 2;
        }
    };

    const a = new A(10);
    expect(a.x).toBe(10);
    expect(a.y).toBe(20);
});

test("class expression name", () => {
    let A = class {}
    expect(A.name).toBe("A");

    let B = class C {}
    expect(B.name).toBe("C");
});