-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHTMLApp.spec.js
More file actions
226 lines (163 loc) · 6.31 KB
/
Copy pathHTMLApp.spec.js
File metadata and controls
226 lines (163 loc) · 6.31 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
215
216
217
218
219
220
221
222
223
224
225
226
import { getNewEl, dispatchEvent } from './__mocks__/dom';
import { CHILD_ATTR, ROOT_ATTR } from './constants';
import { bindEventHandlers } from './events';
import HTMLApp from './HTMLApp';
jest.mock('./events');
describe('HTMLApp', () => {
describe('No root node in the DOM', () => {
it('Should throw an error when no root element is in the DOM when initialised', () => {
expect(() => new HTMLApp()).toThrowError();
});
});
describe('Root node in the DOM', () => {
let rootNode;
beforeEach(() => {
jest.spyOn(window, 'addEventListener');
rootNode = getNewEl({
attributes: [
['id', 'temp-div'],
[ROOT_ATTR]
]
});
document.body.appendChild(rootNode);
});
afterEach(() => {
jest.clearAllMocks();
if (rootNode) {
rootNode.remove();
}
});
describe('On load', () => {
it('Should bind a window load event listener when initialised', () => {
new HTMLApp();
expect(window.addEventListener).toHaveBeenCalledTimes(2);
const firstEventListener = window.addEventListener.mock.calls[0];
expect(firstEventListener[0]).toBe('load');
expect(typeof firstEventListener[1]).toBe('function');
});
it('Should call onLoadApp if provided when the window is loaded', () => {
const onLoadApp = jest.fn();
new HTMLApp({ onLoadApp });
dispatchEvent(window, 'load');
expect(onLoadApp).toHaveBeenCalledTimes(1);
expect(onLoadApp.mock.calls[0][0]).toEqual(
expect.objectContaining({ el: rootNode })
);
expect(onLoadApp.mock.calls[0][1]).toEqual([]);
expect(onLoadApp.mock.calls[0][2]).toEqual(
expect.objectContaining({
__enhancedRootNode: expect.objectContaining({ el: rootNode })
})
);
});
it('Should call onLoadApp with all child nodes when the window is loaded', () => {
rootNode.innerHTML =
`<button ${CHILD_ATTR}="button1"></button>` +
'<button></button>' +
`<button ${CHILD_ATTR}="button2"></button>`;
const onLoadApp = jest.fn();
new HTMLApp({ onLoadApp });
dispatchEvent(window, 'load');
expect(onLoadApp).toHaveBeenCalledTimes(1);
expect(onLoadApp.mock.calls[0][0]).toEqual(
expect.objectContaining({ el: rootNode })
);
expect(onLoadApp.mock.calls[0][1]).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: 'button1' }),
expect.objectContaining({ id: 'button2' })
])
);
expect(onLoadApp.mock.calls[0][2]).toEqual(
expect.objectContaining({
__enhancedRootNode: expect.objectContaining({ el: rootNode })
})
);
});
});
describe('On unload', () => {
it('Should bind a window beforeunload event listener when initialised', () => {
new HTMLApp();
expect(window.addEventListener).toHaveBeenCalledTimes(2);
const secondEventListener = window.addEventListener.mock.calls[1];
expect(secondEventListener[0]).toBe('beforeunload');
expect(typeof secondEventListener[1]).toBe('function');
});
it('Should call onUnloadApp is provided before the window unloads', () => {
const onUnloadApp = jest.fn();
new HTMLApp({ onUnloadApp });
dispatchEvent(window, 'beforeunload');
expect(onUnloadApp).toHaveBeenCalledTimes(1);
});
});
describe('Binding event handlers', () => {
it('Should not call bindEventHandlers when no eventHandlers are provided', () => {
new HTMLApp();
dispatchEvent(window, 'load');
expect(bindEventHandlers).toHaveBeenCalledTimes(0);
});
it('Should not call bindEventHandlers when no eventHandlers have matching child nodes', () => {
new HTMLApp({
eventHandlers: [
{ id: 'unknownNode1', onClick: () => undefined },
{ id: 'unknownNode2', onClick: () => undefined }
]
});
dispatchEvent(window, 'load');
expect(bindEventHandlers).toHaveBeenCalledTimes(0);
});
it('Should call bindEventHandlers with enhanced rootNode and event handlers', () => {
rootNode.innerHTML =
`<button ${CHILD_ATTR}="button1"></button>` +
`<button ${CHILD_ATTR}="button2"></button>`;
const button1 = rootNode.querySelector(`[${CHILD_ATTR}="button1"]`);
const button2 = rootNode.querySelector(`[${CHILD_ATTR}="button2"]`);
const eventHandlers = [
{ id: 'button1', onClick: () => undefined },
{ id: 'button2', onClick: () => undefined }
];
new HTMLApp({ eventHandlers });
dispatchEvent(window, 'load');
expect(bindEventHandlers).toHaveBeenCalledTimes(1);
expect(bindEventHandlers.mock.calls[0][0].el).toEqual(rootNode);
expect(bindEventHandlers.mock.calls[0][1][0].enhancedEl.el).toEqual(button1);
expect(bindEventHandlers.mock.calls[0][1][1].enhancedEl.el).toEqual(button2);
});
});
describe('getEl', () => {
it('Should return the matching enhanced child', () => {
rootNode.innerHTML =
`<button ${CHILD_ATTR}="button1"></button>` +
`<button ${CHILD_ATTR}="button2"></button>`;
const button1 = rootNode.querySelector(`[${CHILD_ATTR}="button1"]`);
const instance = new HTMLApp();
const result = instance.getEl('button1');
expect(result).toEqual(
expect.objectContaining({
id: 'button1',
el: button1
})
);
});
it('Should return undefined when no matching enhanced child node is found', () => {
rootNode.innerHTML =
`<button ${CHILD_ATTR}="button1"></button>` +
`<button ${CHILD_ATTR}="button2"></button>`;
const instance = new HTMLApp();
const result = instance.getEl('button5');
expect(result).toBeUndefined();
});
});
describe('getRootEl', () => {
it('Should return the enhanced root node', () => {
const instance = new HTMLApp();
const result = instance.getRootEl();
expect(result).toEqual(
expect.objectContaining({
el: rootNode
})
);
});
});
});
});