forked from salesforce/lwc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.spec.js
More file actions
214 lines (184 loc) · 8.36 KB
/
index.spec.js
File metadata and controls
214 lines (184 loc) · 8.36 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { createElement } from 'lwc';
import Container from 'x/container';
import FormAssociated from 'x/formAssociated';
import NotFormAssociated from 'x/notFormAssociated';
import LightDomFormAssociated from 'x/lightDomFormAssociated';
import LightDomNotFormAssociated from 'x/lightDomNotFormAssociated';
import { ENABLE_ELEMENT_INTERNALS_AND_FACE } from '../../../helpers/constants.js';
const createFormElement = () => {
const container = createElement('face-container', { is: Container });
document.body.appendChild(container);
return container.shadowRoot.querySelector('form');
};
const createFaceTests = (tagName, ctor, callback) => {
const scenarios = ['lwc.createElement', 'CustomElementConstructor'];
scenarios.forEach((scenario) => {
describe(scenario, () => {
const createFace = () => {
if (scenario === 'lwc.createElement') {
return createFaceUsingLwcCreateElement(`face-${tagName}`, ctor);
} else {
return createFaceUsingCec(`cec-face-${tagName}`, ctor.CustomElementConstructor);
}
};
callback(createFace, scenario);
});
});
};
const createFaceUsingLwcCreateElement = (tagName, ctor) => {
let elm;
const doCreate = () => {
elm = createElement(tagName, { is: ctor });
};
if (ENABLE_ELEMENT_INTERNALS_AND_FACE) {
doCreate();
} else {
expect(doCreate).toLogWarningDev(
/set static formAssociated to true, but form association is not enabled/
);
}
return elm;
};
const createFaceUsingCec = (tagName, ctor) => {
if (!customElements.get(tagName)) {
customElements.define(tagName, ctor);
}
return document.createElement(tagName);
};
const faceSanityTest = (tagName, ctor) => {
createFaceTests(`${tagName}-form-associated`, ctor, (createFace) => {
let form;
let face;
beforeEach(() => {
face = createFace();
form = createFormElement();
form.appendChild(face);
});
it('cannot access formAssociated outside of a component', () => {
expect(() => face.formAssociated).toLogWarningDev(
/formAssociated cannot be accessed outside of a component. Set the value within the component class./
);
});
it('calls face lifecycle methods', () => {
testFaceLifecycleMethodsCallable(() => face);
});
it('is associated with the correct form', () => {
const form2 = document.createElement('form');
document.body.appendChild(form2);
form2.setAttribute('class', 'form2');
const face2 = createElement('face-form-associated-2', { is: ctor });
form2.appendChild(face2);
const container = document.body.querySelector('face-container');
container.shadowRoot.appendChild(form2);
if (ENABLE_ELEMENT_INTERNALS_AND_FACE) {
expect(face.internals.form.className).toEqual('form1');
expect(face2.internals.form.className).toEqual('form2');
}
});
});
};
const testFaceLifecycleMethodsCallable = (createFace) => {
const face = createFace();
const form = createFormElement();
form.appendChild(face);
// formAssociatedCallback
expect(face.formAssociatedCallbackHasBeenCalled).toBeTruthy();
expect(face.formAssociatedCallbackHasBeenCalledWith).toBe(form);
// formDisabledCallback
face.setAttribute('disabled', 'true');
expect(face.formDisabledCallbackHasBeenCalled).toBeTruthy();
expect(face.formDisabledCallbackHasBeenCalledWith).toBe(true);
// formDisabledCallback - re-enable
face.removeAttribute('disabled');
expect(face.formDisabledCallbackHasBeenCalled).toBeTruthy();
expect(face.formDisabledCallbackHasBeenCalledWith).toBe(false);
// formResetCallback
form.reset();
expect(face.formResetCallbackHasBeenCalled).toBeTruthy();
expect(face.formResetCallbackHasBeenCalledWith).toBeUndefined();
// Note there is no good way to test formStateRestoreCallback in karma tests
};
const notFormAssociatedSanityTest = (tagName, ctor) => {
createFaceTests(`${tagName}-not-form-associated`, ctor, (createFace) => {
it(`doesn't call face lifecycle methods when not form associated`, () => {
testFaceLifecycleMethodsNotCallable(createFace);
});
});
};
const testFaceLifecycleMethodsNotCallable = (createFace) => {
const face = createFace();
const form = createFormElement();
form.appendChild(face);
// formAssociatedCallback
expect(face.formAssociatedCallbackHasBeenCalled).toBeFalsy();
// formDisabledCallback
face.setAttribute('disabled', true);
expect(face.formDisabledCallbackHasBeenCalled).toBeFalsy();
// formResetCallback
form.reset();
expect(face.formResetCallbackHasBeenCalled).toBeFalsy();
// Note there is no good way to test formStateRestoreCallback in karma tests
};
describe.runIf(typeof ElementInternals !== 'undefined')('ElementInternals', () => {
// native lifecycle enabled
describe.runIf(ENABLE_ELEMENT_INTERNALS_AND_FACE)('ElementInternals/FACE enabled', () => {
describe.runIf(process.env.NATIVE_SHADOW)('native shadow', () => {
faceSanityTest('native-shadow', FormAssociated);
notFormAssociatedSanityTest('native-shadow', NotFormAssociated);
});
describe.skipIf(process.env.NATIVE_SHADOW)('synthetic shadow', () => {
faceSanityTest('synthetic-shadow', FormAssociated);
notFormAssociatedSanityTest('synthetic-shadow', NotFormAssociated);
});
describe('light DOM', () => {
faceSanityTest('light-dom', LightDomFormAssociated);
notFormAssociatedSanityTest('light-dom', LightDomNotFormAssociated);
});
});
describe.skipIf(ENABLE_ELEMENT_INTERNALS_AND_FACE)('ElementInternals/FACE disabled', () => {
[
{ name: 'shadow DOM', tagName: 'synthetic-lifecycle-shadow', ctor: FormAssociated },
{
name: 'light DOM',
tagName: 'synthetic-lifecycle-light',
ctor: LightDomFormAssociated,
},
].forEach(({ name, tagName, ctor }) => {
createFaceTests(tagName, ctor, (createFace, scenario) => {
it.runIf(scenario === 'lwc.createElement')(
`${name} does not call face lifecycle methods when upgraded by LWC`,
() => {
testFaceLifecycleMethodsNotCallable(createFace);
}
);
describe.skipIf(scenario === 'lwc.createElement')(name, () => {
const lightOrNativeShadow = name === 'light DOM' || process.env.NATIVE_SHADOW;
// Face throws error message when synthetic shadow is enabled
it.runIf(lightOrNativeShadow)(
`${name} calls face lifecycle methods when using CustomElementConstructor`,
() => {
// CustomElementConstructor is to be upgraded independently of LWC, it will always use native lifecycle
testFaceLifecycleMethodsCallable(createFace);
}
);
// synthetic shadow mode
it.skipIf(lightOrNativeShadow)(
`${name} cannot call face lifecycle methods when using CustomElementConstructor`,
() => {
// this is always a callback reaction error, even in "synthetic lifecycle" mode,
// because synthetic lifecycle mode only includes connected/disconnected callbacks,
// not the FACE callbacks
expect(() => {
const face = createFace();
const form = createFormElement();
form.appendChild(face);
}).toThrowCallbackReactionErrorEvenInSyntheticLifecycleMode(
'Form associated lifecycle methods are not available in synthetic shadow. Please use native shadow or light DOM.'
);
}
);
});
});
});
});
});