summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/classes/class-constructor.js
blob: 54a601e10c2c4395708d070f0f57b8dbbdac39bc (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
test("property initialization", () => {
    class A {
        constructor() {
            this.x = 3;
        }
    }

    expect(new A().x).toBe(3);
});

test("method initialization", () => {
    class A {
        constructor() {
            this.x = () => 10;
        }
    }

    expect(new A().x()).toBe(10);
});

test("initialize to class method", () => {
    class A {
        constructor() {
            this.x = this.method;
        }

        method() {
            return 10;
        }
    }

    expect(new A().x()).toBe(10);
});

test("constructor length affects class length", () => {
    class A {
        constructor() {}
    }

    expect(A).toHaveLength(0);

    class B {
        constructor(a, b, c = 2) {}
    }

    expect(B).toHaveLength(2);
});

test("must be invoked with 'new'", () => {
    class A {
        constructor() {}
    }

    expect(() => {
        A();
    }).toThrowWithMessage(TypeError, "Class constructor A must be called with 'new'");

    expect(() => {
        A.prototype.constructor();
    }).toThrowWithMessage(TypeError, "Class constructor A must be called with 'new'");
});

test("implicit constructor", () => {
    class A {}

    expect(new A()).toBeInstanceOf(A);
});