-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
120 lines (103 loc) · 4.41 KB
/
index.spec.js
File metadata and controls
120 lines (103 loc) · 4.41 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { LightningElement, createElement, setFeatureFlagForTest } 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();
});
it('should throw when the constructor returns something other than LightningElement', () => {
expect(() => {
createElement('x-returning-bad', { is: ReturningBad });
}).toThrowError(
TypeError,
'Invalid component constructor, the class should extend LightningElement.'
);
});
it('should succeed when the constructor returns something other than LightningElement when DISABLE_CONSTRUCTOR_INVOCATION_VALIDATION is true', () => {
setFeatureFlagForTest('DISABLE_CONSTRUCTOR_INVOCATION_VALIDATION', true);
expect(() => {
createElement('x-returning-bad', { is: ReturningBad });
}).not.toThrow();
setFeatureFlagForTest('DISABLE_CONSTRUCTOR_INVOCATION_VALIDATION', false);
});
it('should throw when calling LightningElement constructor as a function (e.g. .call/.apply)', () => {
const func = () => {
const fakeThis = {};
LightningElement.call(fakeThis);
};
expect(func).toThrowError(TypeError);
expect(func).toThrowError(/Cannot call LightningElement constructor/);
});
it('should throw Illegal constructor (not Cannot call) when DISABLE_CONSTRUCTOR_INVOCATION_VALIDATION is true', () => {
setFeatureFlagForTest('DISABLE_CONSTRUCTOR_INVOCATION_VALIDATION', true);
const func = () => {
const fakeThis = {};
LightningElement.call(fakeThis);
};
expect(func).toThrowError(TypeError);
expect(func).toThrowError(/Illegal constructor/);
expect(func).not.toThrowError(/Cannot call LightningElement constructor/);
setFeatureFlagForTest('DISABLE_CONSTRUCTOR_INVOCATION_VALIDATION', false);
});