-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
117 lines (99 loc) · 4.18 KB
/
index.spec.js
File metadata and controls
117 lines (99 loc) · 4.18 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
import { createElement, LightningElement } from 'lwc';
import Test from 'x/test';
import ShadowRootGetter from 'x/shadowRootGetter';
import {
isNativeShadowRootInstance,
isSyntheticShadowRootInstance,
} from '../../../helpers/utils.js';
function testInvalidOptions(type, option) {
it(`throws a TypeError if option is a ${type}`, () => {
expect(() => createElement('x-component', option)).toThrowError(
TypeError,
/"createElement" function expects an object as second parameter but received/
);
});
}
testInvalidOptions('undefined', undefined);
testInvalidOptions('null', null);
testInvalidOptions('String', 'x-component');
testInvalidOptions('Class not extending LightningElement', class Component {});
function testInvalidIsValue(type, isValue) {
it(`throws a TypeError if option.is is a ${type}`, () => {
expect(() => createElement('x-component', { is: isValue })).toThrowError(
TypeError,
'"createElement" function expects an "is" option with a valid component constructor.'
);
});
}
testInvalidIsValue('undefined', undefined);
testInvalidIsValue('null', null);
testInvalidIsValue('String', 'x-component');
function testInvalidComponentConstructor(type, isValue) {
it(`throws a TypeError if option.is is a ${type}`, () => {
expect(() => createElement('x-component', { is: isValue })).toThrowError(
TypeError,
/.+ is not a valid component, or does not extends LightningElement from "lwc". You probably forgot to add the extend clause on the class declaration./
);
});
}
testInvalidComponentConstructor('Function', function () {});
testInvalidComponentConstructor('Class not extending LightningElement', class Component {});
it('returns an HTMLElement', () => {
const elm = createElement('x-component', { is: Test });
expect(elm instanceof HTMLElement).toBe(true);
});
it.skipIf(process.env.NATIVE_SHADOW)(
'should create an element with a synthetic shadow root by default',
() => {
const elm = createElement('x-component', { is: Test });
expect(isSyntheticShadowRootInstance(elm.shadowRoot)).toBeTrue();
expect(elm.isSynthetic()).toBeTrue();
}
);
it('supports component constructors in circular dependency', () => {
function Circular() {
return Test;
}
Circular.__circular__ = true;
const elm = createElement('x-component', { is: Circular });
expect(elm instanceof HTMLElement).toBe(true);
});
describe.runIf(process.env.NATIVE_SHADOW)('native shadow', () => {
it('should create an element with a native shadow root if fallback is false', () => {
const elm = createElement('x-component', { is: Test });
expect(isNativeShadowRootInstance(elm.shadowRoot)).toBeTrue();
expect(elm.isSynthetic()).toBeFalse();
});
it('should create a shadowRoot in open mode when mode in not specified', () => {
const elm = createElement('x-component', {
is: Test,
});
expect(elm.shadowRoot.mode).toBe('open');
});
it('should create a shadowRoot in closed mode if the mode is passed as closed', () => {
const elm = createElement('x-shadow-root-getter', {
is: ShadowRootGetter,
mode: 'closed',
});
// Since the shadowRoot property is not attached to the element in closed mode, we need to
// retrieve it by accessing the template property from the class.
const shadowRoot = elm.getShadowRoot();
expect(shadowRoot).toBeInstanceOf(ShadowRoot);
expect(shadowRoot.mode).toBe('closed');
});
});
describe('locker integration', () => {
it('should support component class that extend a mirror of the LightningElement', () => {
function SecureBaseClass() {
if (this instanceof SecureBaseClass) {
LightningElement.prototype.constructor.call(this);
} else {
return SecureBaseClass;
}
}
SecureBaseClass.__circular__ = true;
class Component extends SecureBaseClass {}
const elm = createElement('x-component', { is: Component });
expect(elm instanceof HTMLElement).toBe(true);
});
});