-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmodal.test.js
340 lines (290 loc) · 14.4 KB
/
modal.test.js
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import $ from "jquery";
import pattern from "./modal";
import utils from "../../core/utils";
import { jest } from "@jest/globals";
describe("pat-modal", function () {
let deferred;
const answer = function (html) {
deferred.resolve(html, "ok", { responseText: html });
};
beforeEach(function () {
deferred = new $.Deferred();
document.body.innerHTML = `<div id="lab"></div>`;
});
afterEach(function () {
document.body.innerHTML = "";
document.body.classList.remove("modal-active");
jest.restoreAllMocks();
});
describe("init", function () {
it("Modal with single element", function () {
$("#lab").html(
[
'<div class="pat-modal" id="modal">',
" <p>Modal content</p>",
"</div>",
].join("\n")
);
expect($("body").hasClass("modal-active")).toBeFalsy();
var $modal = $("#modal");
pattern.init($modal);
expect($modal.find(".header").length).toBeTruthy();
expect($modal.find(".header").text()).toBe("Close");
expect($modal.find(".header .close-panel").length).toBeTruthy();
expect($modal.find(".panel-content").length).toBeTruthy();
expect($modal.find(".panel-content").text()).toBe("Modal content");
expect($("body").hasClass("modal-active")).toBeTruthy();
});
it("Modal with header ", function () {
$("#lab").html(
[
'<div class="pat-modal" id="modal">',
" <h3>Modal header</h3>",
" <p>Modal content</p>",
"</div>",
].join("\n")
);
expect($("body").hasClass("modal-active")).toBeFalsy();
var $modal = $("#modal");
pattern.init($modal);
expect($modal.find(".header").text()).toBe("Modal headerClose");
expect($modal.find(".header .close-panel").length).toBeTruthy();
expect($modal.find(".panel-content").text()).toBe("Modal content");
expect($("body").hasClass("modal-active")).toBeTruthy();
});
it("Modal with multiple content items ", function () {
$("#lab").html(
[
'<div class="pat-modal" id="modal">',
" <h3>Modal header</h3>",
" <p>Modal content</p>",
" <h4>Subheader</h4>",
" <p>More content</p>",
"</div>",
].join("\n")
);
expect($("body").hasClass("modal-active")).toBeFalsy();
var $modal = $("#modal");
pattern.init($modal);
expect($modal.find(".header").text()).toBe("Modal headerClose");
expect($modal.find(".header .close-panel").length).toBeTruthy();
expect($modal.find(".panel-content p").length).toBe(2);
expect($modal.find(".panel-content h4").length).toBe(1);
expect($("body").hasClass("modal-active")).toBeTruthy();
});
it("Modal with a form that has the pat-modal CSS class", function () {
var $modal, // main modal container
$modalLink; // link that triggers the modal
$("#lab").html(
[
'<a id="modalLink" class="pat-modal"',
' href="some-page-with-modal.html">Open a modal',
" containing a form with pat-modal CSS class.</a>",
].join("/n")
);
$modalLink = $("#modalLink");
pattern.init($modalLink);
// najprej #modalLink ne obstaja
$modal = $("div#pat-modal");
expect($modal.length).toBe(0);
$modalLink.click(); // trigger panel loading
// TODO: we need to insert a delay here so that the asynchronous
// AJAX injection of the potentially problematic HTML finishes ...
// and only AFTER THAT we test that everything is still OK
// (read: that the modal still exists in DOM)
// NOTE: If AJAX request in inject.execute() is performed in a
// synchronous way, a delay is not need and this test detects a
// bug in modal._init_inject1()
// div#pat-modal should still exist even after the click has
// loaded and injected problematic HTML
$modal = $("div#pat-modal");
expect($modal.length).toBeGreaterThan(0);
});
it("Modal with single element that specifies a custom close button string", function () {
$("#lab").html(
[
'<div class="pat-modal" id="modal" data-pat-modal="close-text: Shutdown">',
" <p>Modal content</p>",
"</div>",
].join("\n")
);
expect($("body").hasClass("modal-active")).toBeFalsy();
var $modal = $("#modal");
pattern.init($modal);
expect($modal.find(".header").length).toBeTruthy();
expect($modal.find(".header").text()).toBe("Shutdown");
expect($modal.find(".header .close-panel").length).toBeTruthy();
expect($("body").hasClass("modal-active")).toBeTruthy();
});
it("Dispatch pat-modal-ready with a div-modal.", async function () {
document.body.innerHTML = `
<div class="pat-modal" id="modal">
<p>Modal content</p>
</div>
`;
const callback = jest.fn();
const modal_el = document.querySelector(".pat-modal");
modal_el.addEventListener("pat-modal-ready", callback);
pattern.init(modal_el);
await utils.timeout(1); // wait a tick for async to settle.
// Opened immediately
expect(callback).toHaveBeenCalled();
});
it("Dispatch pat-modal-ready with a inject-modal.", async function () {
jest.spyOn($, "ajax").mockImplementation(() => deferred);
document.body.innerHTML = `
<a
class="pat-modal"
href="test.html">Open modal</a>
`;
const callback = jest.fn();
const modal_el = document.querySelector(".pat-modal");
modal_el.addEventListener("pat-modal-ready", callback);
pattern.init(modal_el);
await utils.timeout(1); // wait a tick for async to settle.
expect(callback).not.toHaveBeenCalled();
answer("<html><body><div></div></body></html>");
modal_el.click();
await utils.timeout(1); // wait a tick for async to settle.
expect(callback).toHaveBeenCalled();
});
it("Submit form, do injection and close overlay.", async function () {
await import("../inject/inject");
const registry = (await import("../../core/registry")).default;
jest.spyOn($, "ajax").mockImplementation(() => deferred);
answer(
`<html><body><div id="source">hello.</div><div id="source2">there</div></body></html>`
);
document.body.innerHTML = `
<div class="pat-modal">
<form
class="pat-inject"
action="test.html"
data-pat-inject="source: #source; target: #target && source: #source2; target: #target2">
<button type="submit" class="close-panel">submit</button>
</form>
</div>
<div id="target">
</div>
<div id="target2">
</div>
`;
registry.scan(document.body);
await utils.timeout(1); // wait a tick for async to settle.
await utils.timeout(2); // wait a tick for async to settle.
document.querySelector("button.close-panel[type=submit]").click();
await utils.timeout(1); // wait a tick for pat-inject to settle.
await utils.timeout(1); // wait a tick for pat-modal destroy to settle.
expect(document.querySelector(".pat-modal")).toBe(null);
expect(document.querySelector("#target").textContent).toBe("hello.");
expect(document.querySelector("#target2").textContent).toBe("there");
});
it("Submit form, do injection and close overlay with multiple forms.", async function () {
await import("../inject/inject");
const registry = (await import("../../core/registry")).default;
jest.spyOn($, "ajax").mockImplementation(() => deferred);
answer(
`<html><body><div id="source">hello.</div><div id="source2">there</div></body></html>`
);
document.body.innerHTML = `
<div class="pat-modal">
<form
class="form1 pat-inject"
action="test.html"
data-pat-inject="source: #source; target: #target">
<button type="submit" class="close-panel">submit</button>
</form>
<form
class="form2 pat-inject"
action="test.html"
data-pat-inject="source: #source; target: #target">
<button type="submit" class="close-panel">submit</button>
</form>
</div>
<div id="target">
</div>
`;
registry.scan(document.body);
await utils.timeout(1); // wait a tick for async to settle.
await utils.timeout(2); // wait a tick for async to settle.
document.querySelector(".form2 button.close-panel[type=submit]").click();
await utils.timeout(1); // wait a tick for pat-inject to settle.
await utils.timeout(1); // wait a tick for pat-modal destroy to settle.
expect(document.querySelector(".pat-modal")).toBe(null);
expect(document.querySelector("#target").textContent).toBe("hello.");
});
it("Initialize modal also when modal contents change.", async function () {
document.body.innerHTML = `
<div class="pat-modal">
</div>
`;
const instance = new pattern(document.querySelector(".pat-modal"));
const spy_init_handlers = jest.spyOn(instance, "_init_handlers");
expect(spy_init_handlers).toHaveBeenCalledTimes(0);
// first call is invoked after some ticks to allow any other
// patterns to modify the dom before the handlers are initialized.
await utils.timeout(2); // wait for init to happen.
expect(spy_init_handlers).toHaveBeenCalledTimes(1);
// Provoke a DOM subtree change and the MutationObserver to kick in
document.querySelector(".pat-modal").innerHTML = "<div/>";
await utils.timeout(1); // wait a tick for async to settle.
expect(spy_init_handlers).toHaveBeenCalledTimes(2);
});
it("Ensure destroy callback isn't called multiple times.", async function () {
document.body.innerHTML = `
<div id="pat-modal" class="pat-modal">
<button id="close-modal" class="close-panel">close</button>
</div>
`;
const instance = new pattern(document.querySelector(".pat-modal"));
await utils.timeout(1); // wait a tick for async to settle.
await utils.timeout(2); // wait a tick for async to settle.
const spy_destroy = jest.spyOn(instance, "destroy");
// ``destroy`` was already initialized with instantiating the pattern above.
// Call init again for new instantiation.
await instance.init($(".pat-modal"));
document.querySelector("#close-modal").click();
await utils.timeout(1); // wait a tick for pat-modal destroy to settle.
expect(spy_destroy).toHaveBeenCalledTimes(1);
});
it("Ensure destroy callback isn't called multiple times with forms and injection.", async function () {
const markup = `
<div id="pat-modal" class="pat-modal">
<form action="." class="pat-inject" data-pat-inject="source: body; target: body">
<button id="close-modal" class="close-panel" type="submit">close</button>
</form>
</div>
`;
// Set up inject mocks
const pattern_inject = (await import("../inject/inject")).default;
jest.spyOn($, "ajax").mockImplementation(() => deferred);
answer("<html><body><p>OK</p></body></html>"); // empties body
document.body.innerHTML = markup;
pattern_inject.init($(".pat-inject"));
const instance = new pattern(document.querySelector(".pat-modal"));
await utils.timeout(1); // wait a tick for async to settle.
await utils.timeout(2); // wait a tick for async to settle.
const spy_destroy = jest.spyOn(instance, "destroy");
const spy_destroy_inject = jest.spyOn(instance, "destroy_inject");
// ``destroy`` was already initialized with instantiating the pattern above.
// Call init again for new instantiation.
await instance.init($(".pat-modal"));
document.querySelector("#close-modal").click();
await utils.timeout(1); // wait a tick for pat-inject to settle.
expect(spy_destroy_inject).toHaveBeenCalledTimes(1);
expect(spy_destroy).toHaveBeenCalledTimes(1);
// re-attach and re-initialize. Event handler should only be active once.
document.body.innerHTML = markup;
pattern_inject.init($(".pat-inject"));
new pattern(document.querySelector(".pat-modal"));
await utils.timeout(1); // wait a tick for async to settle.
await utils.timeout(2); // wait a tick for async to settle.
document.querySelector("#close-modal").click();
await utils.timeout(1); // wait a tick for pat-inject to settle.
// Previous mocks still active.
// Handlers executed exactly once.
expect(spy_destroy_inject).toHaveBeenCalledTimes(1);
expect(spy_destroy).toHaveBeenCalledTimes(1);
});
});
});