summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/classes/class-advanced-extends.js
blob: bc8dbe3d8c815688432a2fb83d39e4a0afa70d9a (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
test("extending function", () => {
    class A extends function () {
        this.foo = 10;
    } {}

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

test("extending null", () => {
    class A extends null {}

    expect(Object.getPrototypeOf(A.prototype)).toBeNull();

    expect(() => {
        new A();
    }).toThrowWithMessage(TypeError, "Super constructor is not a constructor");
});

test("extending String", () => {
    class MyString extends String {}

    const ms = new MyString("abc");
    expect(ms).toBeInstanceOf(MyString);
    expect(ms).toBeInstanceOf(String);
    expect(ms.charAt(1)).toBe("b");

    class MyString2 extends MyString {
        charAt(i) {
            return `#${super.charAt(i)}`;
        }
    }

    const ms2 = new MyString2("abc");
    expect(ms2.charAt(1)).toBe("#b");
});

test("class extends value is invalid", () => {
    expect(() => {
        class A extends 123 {}
    }).toThrowWithMessage(TypeError, "Class extends value 123 is not a constructor or null");
});

test("class extends value has invalid prototype", () => {
    function f() {}
    f.prototype = 123;
    expect(() => {
        class A extends f {}
    }).toThrowWithMessage(TypeError, "Class extends value has an invalid prototype 123");
});