-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathnavigation.test.js
More file actions
501 lines (414 loc) · 19.2 KB
/
Copy pathnavigation.test.js
File metadata and controls
501 lines (414 loc) · 19.2 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import "../inject/inject";
import Pattern from "./navigation";
import Registry from "../../core/registry";
import utils from "../../core/utils";
describe("1 - Navigation pattern tests", function () {
beforeEach(function () {
document.body.innerHTML = `
<div id="page_wrapper">
<article id="injection_content">test content</article>
<div id="injection_area"></div>
<ul class="pat-navigation nav1">
<li class="w1">
<a
href="#injection_content"
class="pat-inject a1"
data-pat-inject="target: #injection_area">link a1</a>
<ul class="nav11">
<li class="w11">
<a
href="#injection_content"
class="pat-inject a11"
data-pat-inject="target: #injection_area">link a11</a>
</li>
</ul>
</li>
</ul>
<nav
class="pat-navigation nav2"
data-pat-navigation="item-wrapper: div; in-path-class: in-path; current-class: active">
<div class="w2">
<a
href="#injection_content"
class="pat-inject a2"
data-pat-inject="target: #injection_area">link a2</a>
<div class="w21">
<a
href="#injection_content"
class="pat-inject a21"
data-pat-inject="target: #injection_area">link a21</a>
</div>
</div>
</nav>
</div>
`;
});
afterEach(function () {
document.body.innerHTML = "";
});
it("Test 1: Test roundtrip", async () => {
const injection_area = document.querySelector("#injection_area");
const nav1 = document.querySelector(".nav1");
const w1 = nav1.querySelector(".w1");
const a1 = nav1.querySelector(".a1");
const w11 = nav1.querySelector(".w11");
const a11 = nav1.querySelector(".a11");
const nav2 = document.querySelector(".nav2");
const w2 = nav2.querySelector(".w2");
const a2 = nav2.querySelector(".a2");
const w21 = nav2.querySelector(".w21");
const a21 = nav2.querySelector(".a21");
Registry.scan("body");
await utils.timeout(1); // wait a tick for async to settle.
a1.click();
await utils.timeout(1); // wait a tick for async to settle.
expect(injection_area.textContent === "test content").toBeTruthy();
expect(w1.classList.contains("current")).toBeTruthy();
expect(w1.classList.contains("navigation-in-path")).toBeFalsy();
expect(a1.classList.contains("current")).toBeTruthy();
expect(a1.classList.contains("navigation-in-path")).toBeFalsy();
expect(w11.classList.contains("current")).toBeFalsy();
expect(w11.classList.contains("navigation-in-path")).toBeFalsy();
expect(a11.classList.contains("current")).toBeFalsy();
expect(a11.classList.contains("navigation-in-path")).toBeFalsy();
injection_area.innerHTML = "";
a11.click();
await utils.timeout(1); // wait a tick for async to settle.
expect(injection_area.textContent === "test content").toBeTruthy();
expect(w1.classList.contains("current")).toBeFalsy();
expect(w1.classList.contains("navigation-in-path")).toBeTruthy();
expect(a1.classList.contains("current")).toBeFalsy();
expect(a1.classList.contains("navigation-in-path")).toBeTruthy();
expect(w11.classList.contains("current")).toBeTruthy();
expect(w11.classList.contains("navigation-in-path")).toBeFalsy();
expect(a11.classList.contains("current")).toBeTruthy();
expect(a11.classList.contains("navigation-in-path")).toBeFalsy();
injection_area.innerHTML = "";
a2.click();
await utils.timeout(1); // wait a tick for async to settle.
expect(injection_area.textContent === "test content").toBeTruthy();
expect(w2.classList.contains("active")).toBeTruthy();
expect(w2.classList.contains("in-path")).toBeFalsy();
expect(a2.classList.contains("active")).toBeTruthy();
expect(a2.classList.contains("in-path")).toBeFalsy();
expect(w21.classList.contains("active")).toBeFalsy();
expect(w21.classList.contains("in-path")).toBeFalsy();
expect(a21.classList.contains("active")).toBeFalsy();
expect(a21.classList.contains("in-path")).toBeFalsy();
injection_area.innerHTML = "";
a21.click();
await utils.timeout(1); // wait a tick for async to settle.
expect(injection_area.textContent === "test content").toBeTruthy();
expect(w2.classList.contains("active")).toBeFalsy();
expect(w2.classList.contains("in-path")).toBeTruthy();
expect(a2.classList.contains("active")).toBeFalsy();
expect(a2.classList.contains("in-path")).toBeTruthy();
expect(w21.classList.contains("active")).toBeTruthy();
expect(w21.classList.contains("in-path")).toBeFalsy();
expect(a21.classList.contains("active")).toBeTruthy();
expect(a21.classList.contains("in-path")).toBeFalsy();
});
it("Test 2: Auto load current", async () => {
const injection_area = document.querySelector("#injection_area");
const nav2 = document.querySelector(".nav2");
nav2.classList.add("navigation-load-current");
const w2 = nav2.querySelector(".w2");
const a2 = nav2.querySelector(".a2");
const w21 = nav2.querySelector(".w21");
const a21 = nav2.querySelector(".a21");
a21.classList.add("active");
Registry.scan("body");
await utils.timeout(1); // wait a tick for async to settle.
expect(injection_area.textContent === "test content").toBeTruthy();
expect(w2.classList.contains("active")).toBeFalsy();
expect(w2.classList.contains("in-path")).toBeTruthy();
expect(a2.classList.contains("active")).toBeFalsy();
expect(a2.classList.contains("in-path")).toBeTruthy();
expect(w21.classList.contains("active")).toBeTruthy();
expect(w21.classList.contains("in-path")).toBeFalsy();
expect(a21.classList.contains("active")).toBeTruthy();
expect(a21.classList.contains("in-path")).toBeFalsy();
});
});
describe("2 - Navigation pattern tests - mark after navigation injection", function () {
let _window_location;
beforeEach(() => {
_window_location = global.window.location;
delete global.window.location;
document.body.innerHTML = "";
});
afterEach(() => {
global.window.location = _window_location;
document.body.innerHTML = "";
});
const set_url = (url, portal_url) => {
global.window.location = {
href: url,
};
portal_url = portal_url || url;
document.body.dataset.portalUrl = portal_url;
};
it("navigation roundtrip", async () => {
document.body.innerHTML = `
<div id="injected_nav">
<div class="w1">
<a href="/path/to" class="a1">link a1</a>
<div class="w11">
<a href="/path/to/test" class="a11">link a11</a>
</div>
</div>
</div>
<a
href="#injected_nav"
class="pat-inject load-nav"
data-pat-inject="target: #injection_target">load navigation</a>
<nav
id="injection_target"
class="pat-navigation nav"
data-pat-navigation="item-wrapper: div">
</nav>
`;
set_url("https://patternslib.com/path/to/test");
Registry.scan(document.body);
const nav = document.querySelector("nav");
const load_nav = document.querySelector(".load-nav");
load_nav.click();
await utils.timeout(1); // wait for MutationObserver
const w1 = nav.querySelector(".w1");
const a1 = nav.querySelector(".a1");
const w11 = nav.querySelector(".w11");
const a11 = nav.querySelector(".a11");
expect(w1.classList.contains("current")).toBeFalsy();
expect(w1.classList.contains("navigation-in-path")).toBeTruthy();
expect(a1.classList.contains("current")).toBeFalsy();
expect(a1.classList.contains("navigation-in-path")).toBeTruthy();
expect(w11.classList.contains("current")).toBeTruthy();
expect(w11.classList.contains("navigation-in-path")).toBeFalsy();
expect(a11.classList.contains("current")).toBeTruthy();
expect(a11.classList.contains("navigation-in-path")).toBeFalsy();
});
});
describe("3 - Navigation pattern tests - Mark items based on URL", () => {
let _window_location;
beforeEach(() => {
_window_location = global.window.location;
delete global.window.location;
document.body.innerHTML = "";
});
afterEach(() => {
global.window.location = _window_location;
});
const set_url = (url, portal_url) => {
global.window.location = {
href: url,
};
portal_url = portal_url || url;
document.body.dataset.portalUrl = portal_url;
};
it("navigation roundtrip", () => {
document.body.innerHTML = `
<nav class="pat-navigation"
data-pat-navigation="in-path-class: inPath">
<ul>
<li>
<a href="/">Home</a>
</li>
<li>
<a href="/path1">p1</a>
</li>
<li>
<a href="/path2">p2</a>
<ul>
<li>
<a href="/path2/path2.1">p2.1</a>
</li>
<li>
<a href="/path2/path2.2">p2.2</a>
<ul>
<li>
<a href="/path2/path2.2/path2.2.1">p2.2.1</a>
</li>
<li>
<a href="/path2/path2.2/path2.2.2">p2.2.2</a>
</li>
</ul>
</li>
<li>
<a href="/i-do-not-math-the-url-path-hierachy">p2n</a>
</li>
</ul>
</li>
<li>
<a href="../../path3">p1</a>
</li>
<li>
<a href="https://patternslib.com/path4">p1</a>
</li>
</ul>
</nav>
`;
set_url("https://patternslib.com/?a=1&b=2#hash");
const instance = new Pattern(document.querySelector(".pat-navigation"));
const it0 = document.querySelector("a[href='/']");
const it1 = document.querySelector("a[href='/path1']");
const it2 = document.querySelector("a[href='/path2']");
const it21 = document.querySelector("a[href='/path2/path2.1']");
const it22 = document.querySelector("a[href='/path2/path2.2']");
const it221 = document.querySelector("a[href='/path2/path2.2/path2.2.1']");
const it222 = document.querySelector("a[href='/path2/path2.2/path2.2.2']");
const it2n = document.querySelector("a[href='/i-do-not-math-the-url-path-hierachy']"); // prettier-ignore
const it3 = document.querySelector("a[href='../../path3']");
const it4 = document.querySelector("a[href='https://patternslib.com/path4']");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it0);
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path1");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it1);
// Check query string
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path1?a=1&b=2");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it1);
// Check hash
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path1/#hash");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it1);
// Check query string and hash
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path1?a=1&b=2#hash");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it1);
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path2");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it2);
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path2/path2.1");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(2);
expect(document.querySelector(".current a")).toBe(it21);
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path2/path2.2");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(2);
expect(document.querySelector(".current a")).toBe(it22);
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path2/path2.2/path2.2.1");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(4);
expect(document.querySelector(".current a")).toBe(it221);
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path2/path2.2/path2.2.2");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(4);
expect(document.querySelector(".current a")).toBe(it222);
instance.clear_items();
instance.mark_items_url(
"https://patternslib.com/i-do-not-math-the-url-path-hierachy"
);
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(2);
expect(document.querySelector(".current a")).toBe(it2n);
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path3");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it3);
instance.clear_items();
instance.mark_items_url("https://patternslib.com/path4");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it4);
});
});
describe("4 - Navigation pattern tests - Mark items based based clicking without pat-inject.", () => {
beforeEach(() => {
document.body.innerHTML = "";
});
afterEach(() => {
document.body.innerHTML = "";
});
it("navigation roundtrip", () => {
document.body.innerHTML = `
<nav class="pat-navigation"
data-pat-navigation="in-path-class: inPath">
<ul>
<li class="current">
<a class="current" href="/home">Home</a>
</li>
<li>
<a href="/path1">p1</a>
</li>
<li>
<a href="/path2">p2</a>
<ul>
<li>
<a href="/path2/path2.1">p2.1</a>
</li>
<li>
<a href="/path2/path2.2">p2.2</a>
<ul>
<li>
<a href="/path2/path2.2/path2.2.1">p2.2.1</a>
</li>
<li>
<a href="/path2/path2.2/path2.2.2">p2.2.2</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
`;
const instance = new Pattern(document.querySelector(".pat-navigation"));
const it0 = document.querySelector("a[href='/home']");
const it1 = document.querySelector("a[href='/path1']");
const it2 = document.querySelector("a[href='/path2']");
const it21 = document.querySelector("a[href='/path2/path2.1']");
const it22 = document.querySelector("a[href='/path2/path2.2']");
const it221 = document.querySelector("a[href='/path2/path2.2/path2.2.1']");
const it222 = document.querySelector("a[href='/path2/path2.2/path2.2.2']");
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it0);
instance.clear_items();
it1.click();
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it1);
instance.clear_items();
it2.click();
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(0);
expect(document.querySelector(".current a")).toBe(it2);
instance.clear_items();
it21.click();
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(2);
expect(document.querySelector(".current a")).toBe(it21);
instance.clear_items();
it22.click();
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(2);
expect(document.querySelector(".current a")).toBe(it22);
instance.clear_items();
it221.click();
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(4);
expect(document.querySelector(".current a")).toBe(it221);
instance.clear_items();
it222.click();
expect(document.querySelectorAll(".current").length).toBe(2);
expect(document.querySelectorAll(".inPath").length).toBe(4);
expect(document.querySelector(".current a")).toBe(it222);
});
});