-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathindex.spec.js
More file actions
127 lines (104 loc) · 4.61 KB
/
index.spec.js
File metadata and controls
127 lines (104 loc) · 4.61 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
import { createElement } from 'lwc';
import LockerIntegration from 'x/lockerIntegration';
import LockerLiveComponent from 'x/lockerLiveComponent';
import LockerHooks, { hooks } from 'x/lockerHooks';
import { spyOn } from '@vitest/spy';
it('should support Locker integration which uses a wrapped LightningElement base class', () => {
const elm = createElement('x-secure-parent', { is: LockerIntegration });
document.body.appendChild(elm);
// Verifying that shadow tree was created to ensure the component class was successfully processed
const actual = elm.querySelector('div.secure');
expect(actual).toBeDefined();
});
describe('Locker hooks', () => {
let getHookSpy;
let setHookSpy;
let callHookSpy;
beforeAll(() => {
getHookSpy = spyOn(hooks, 'getHook');
setHookSpy = spyOn(hooks, 'setHook');
callHookSpy = spyOn(hooks, 'callHook');
});
afterEach(() => {
getHookSpy.mockReset();
setHookSpy.mockReset();
callHookSpy.mockReset();
});
afterAll(() => {
getHookSpy.mockRestore();
setHookSpy.mockRestore();
callHookSpy.mockRestore();
});
describe('getHook', () => {
it('invokes getHook when reading a public property', () => {
const elm = createElement('x-hooks', { is: LockerHooks });
// Testing the getter; don't need to use the return value
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
elm.publicProp;
expect(getHookSpy).toHaveBeenCalledTimes(1);
expect(getHookSpy).toHaveBeenCalledWith(expect.any(Object), 'publicProp');
});
it('invokes getHook when reading a public property via an accessor', () => {
const elm = createElement('x-hooks', { is: LockerHooks });
// Testing the getter; don't need to use the return value
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
elm.publicAccessor;
expect(getHookSpy).toHaveBeenCalledTimes(1);
expect(getHookSpy).toHaveBeenCalledWith(expect.any(Object), 'publicAccessor');
});
});
describe('setHook', () => {
it('invokes setHook when assigning a public property', () => {
const elm = createElement('x-hooks', { is: LockerHooks });
elm.publicProp = 1;
expect(setHookSpy).toHaveBeenCalledTimes(1);
expect(setHookSpy).toHaveBeenCalledWith(expect.any(Object), 'publicProp', 1);
});
it('invokes setHook when assigning a public property via an accessor', () => {
const elm = createElement('x-hooks', { is: LockerHooks });
elm.publicAccessor = 1;
expect(setHookSpy).toHaveBeenCalledTimes(1);
expect(setHookSpy).toHaveBeenCalledWith(expect.any(Object), 'publicAccessor', 1);
});
});
describe('callHook', () => {
it('invokes callHook when invoking a public method', () => {
const elm = createElement('x-hooks', { is: LockerHooks });
elm.publicMethod(1, 'foo');
expect(callHookSpy).toHaveBeenCalledTimes(1);
expect(callHookSpy).toHaveBeenCalledWith(expect.any(Object), expect.any(Function), [
1,
'foo',
]);
});
it('should invoke callHook for all the lifecycle hooks', () => {
const elm = createElement('x-hooks', { is: LockerHooks });
document.body.appendChild(elm);
document.body.removeChild(elm);
expect(callHookSpy).toHaveBeenCalledTimes(4);
const invokedMethods = callHookSpy.mock.calls.map(([_, method]) => method.name);
expect(invokedMethods).toEqual([
'connectedCallback',
'render',
'renderedCallback',
'disconnectedCallback',
]);
});
it('should invoke callHook when invoking event handlers', () => {
const elm = createElement('x-hooks', { is: LockerHooks });
document.body.appendChild(elm);
const evt = new CustomEvent('test');
elm.shadowRoot.querySelector('div').dispatchEvent(evt);
expect(callHookSpy).toHaveBeenCalledWith(expect.any(Object), expect.any(Function), [
evt,
]);
});
});
});
describe('Locker live objects', () => {
it('should report the component instance as live to support expandos', () => {
const elm = createElement('x-live', { is: LockerLiveComponent });
document.body.appendChild(elm);
expect(elm.hasMarker()).toBe(true);
});
});