-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
92 lines (78 loc) · 3.2 KB
/
index.spec.js
File metadata and controls
92 lines (78 loc) · 3.2 KB
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
import { LightningElement, createElement } from 'lwc';
import NotInvokingSuper from 'x/notInvokingSuper';
import NotReturningThis from 'x/notReturningThis';
import ParentThrowingBeforeSuper from 'x/parentThrowingBeforeSuper';
import DefinedComponent from 'x/definedComponent';
import UndefinedComponent from 'x/undefinedComponent';
import ReturningBad from 'x/returningBad';
it('should throw when trying to invoke the constructor manually', () => {
const func = () => {
new LightningElement();
};
expect(func).toThrowError(TypeError);
expect(func).toThrowError(/Illegal constructor/);
});
it('should throw when trying to `new` a subclass of LightningElement manually', () => {
const func = () => {
class Test extends LightningElement {}
new Test();
};
expect(func).toThrowError(TypeError);
expect(func).toThrowError(/Illegal constructor/);
});
it('should throw when trying to `new` a compiled subclass of LightningElement', () => {
const func = () => {
new UndefinedComponent();
};
expect(func).toThrowError(TypeError);
expect(func).toThrowError(/Illegal constructor/);
});
// TODO [#2970]: component constructor cannot be new-ed even after being defined
it('should throw when trying to `new` a compiled subclass of LightningElement after definition', () => {
createElement('x-defined-component', { is: DefinedComponent });
const func = () => {
new DefinedComponent();
};
expect(func).toThrowError(TypeError);
expect(func).toThrowError(/Illegal constructor/);
});
it('should have no property enumerable on the component instance', () => {
let enumerableProperties = [];
class Test extends LightningElement {
connectedCallback() {
enumerableProperties = Object.keys(this);
}
}
const elm = createElement('x-test', { is: Test });
document.body.appendChild(elm);
expect(enumerableProperties).toEqual([]);
});
it("should fail when the constructor doesn't invoke super()", () => {
expect(() => {
createElement('x-not-invoking-super', { is: NotInvokingSuper });
}).toThrowError(ReferenceError);
});
it('should fail when the constructor return an instance of LightningElement', () => {
expect(() => {
createElement('x-not-returning-this', { is: NotReturningThis });
}).toThrowError(
TypeError,
'Invalid component constructor, the class should extend LightningElement.'
);
});
it("[W-6981076] shouldn't throw when a component with an invalid child in unmounted", () => {
const elm = createElement('x-parent-throwing-before-super', { is: ParentThrowingBeforeSuper });
expect(() => document.body.appendChild(elm)).toThrowCallbackReactionError(
/Throwing before calling super/
);
expect(() => document.body.removeChild(elm)).not.toThrow();
});
// TODO [W-17769475]: Restore this test when we can reliably detect Locker enabled
it('should fail when the constructor returns something other than an instance of itself', () => {
expect(() => {
createElement('x-returning-bad', { is: ReturningBad });
}).toThrowError(
TypeError,
'Invalid component constructor, the class should extend LightningElement.'
);
});