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
120 lines (106 loc) · 4.7 KB
/
index.spec.js
File metadata and controls
120 lines (106 loc) · 4.7 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 { createElement } from 'lwc';
import ShadowDomCmp from 'ai/shadowDom';
import SyntheticShadowDomCmp from 'ai/syntheticShadowDom';
import LightDomCmp from 'ai/lightDom';
import BasicCmp from 'ai/basic';
import {
ENABLE_ELEMENT_INTERNALS_AND_FACE,
IS_SYNTHETIC_SHADOW_LOADED,
} from '../../../../helpers/constants.js';
import { customElementCallbackReactionErrorListener } from '../../../../helpers/utils.js';
const testConnectedCallbackError = (elm, msg) => {
const error = customElementCallbackReactionErrorListener(() => {
document.body.appendChild(elm);
});
expect(error).not.toBeUndefined();
expect(error.message).toBe(msg);
};
const createElementsThroughLwcAndCustomElementConstructor = (tagName, ctor) => [
createElement(`ai-${tagName}`, { is: ctor }),
createCustomElementUsingCec(`cec-ai-${tagName}`, ctor.CustomElementConstructor),
];
const createCustomElementUsingCec = (tagName, ctor) => {
if (!customElements.get(tagName)) {
customElements.define(tagName, ctor);
}
return document.createElement(tagName);
};
const attachInternalsSanityTest = (tagName, ctor) => {
createElementsThroughLwcAndCustomElementConstructor(
`${tagName}-element-internal-enabled`,
ctor
).forEach((elm) => {
beforeAll(() => {
document.body.appendChild(elm);
});
afterAll(() => {
elm.remove();
});
it('should be able to create ElementInternals object', () => {
expect(elm.hasElementInternalsBeenSet()).toBeTruthy();
});
it('should throw an error when called twice on the same element', () => {
// The error type is different between browsers
const chrome = 'ElementInternals for the specified element was already attached';
const safari = 'There is already an existing ElementInternals';
const firefox = 'AttachInternals\\(\\) has already been called';
expect(() => elm.callAttachInternals()).toThrowError(
new RegExp(`(${chrome}|${safari}|${firefox})`)
);
});
});
};
describe.runIf(ENABLE_ELEMENT_INTERNALS_AND_FACE)('ElementInternals', () => {
// ElementInternals API is supported in the browser
const elementInternalsSupported = typeof ElementInternals !== 'undefined';
describe.runIf(elementInternalsSupported)('supported in browser', () => {
describe.runIf(process.env.NATIVE_SHADOW)('native shadow', () => {
attachInternalsSanityTest('native-shadow', ShadowDomCmp);
});
describe.skipIf(process.env.NATIVE_SHADOW)('synthetic shadow', () => {
attachInternalsSanityTest('synthetic-shadow', SyntheticShadowDomCmp);
});
describe('light DOM', () => {
attachInternalsSanityTest('light-dom', LightDomCmp);
});
});
// ElementInternals API is not supported in the browser
// Because of the order error messages are thrown, this error only appears when synthetic shadow
// is disabled. Otherwise, 'attachInternals API is not supported in synthetic shadow.'
// is thrown instead.
it.runIf(!elementInternalsSupported && !IS_SYNTHETIC_SHADOW_LOADED)(
'should throw an error when used with unsupported browser environments',
() => {
createElementsThroughLwcAndCustomElementConstructor(
'unsupported-env-component',
ShadowDomCmp
).forEach((elm) => {
testConnectedCallbackError(
elm,
'attachInternals API is not supported in this browser environment.'
);
});
}
);
});
it.skipIf(ENABLE_ELEMENT_INTERNALS_AND_FACE)(`should throw an error when api version < 61`, () => {
const elm = createElement('unsupported-api-version-component', { is: ShadowDomCmp });
// Note CustomElementConstructor is not upgraded by LWC and inherits directly from HTMLElement which means it calls the native
// attachInternals API.
expect(() => document.body.appendChild(elm)).toThrowCallbackReactionError(
/The attachInternals API is only supported in API version 61 and above/
);
});
it('should not be callable outside a component', () => {
createElementsThroughLwcAndCustomElementConstructor('element-internal', BasicCmp).forEach(
(elm) => {
if (process.env.NODE_ENV === 'production') {
expect(elm.attachInternals).toBeUndefined();
} else {
expect(() => expect(elm.attachInternals).toBeUndefined()).toLogWarningDev(
/attachInternals cannot be accessed outside of a component\. Use this.attachInternals instead\./
);
}
}
);
});