@@ -200,3 +200,142 @@ describe("Navigation pattern tests - no predefined structure", function () {
200
200
expect ( a11 . classList . contains ( "navigation-in-path" ) ) . toBeFalsy ( ) ;
201
201
} ) ;
202
202
} ) ;
203
+
204
+ describe ( "Navigation pattern tests - Mark items based on URL" , ( ) => {
205
+ let _window_location ;
206
+
207
+ beforeEach ( ( ) => {
208
+ _window_location = global . window . location ;
209
+ delete global . window . location ;
210
+ document . body . innerHTML = "" ;
211
+ } ) ;
212
+
213
+ afterEach ( ( ) => {
214
+ global . window . location = _window_location ;
215
+ } ) ;
216
+
217
+ const set_url = ( url , portal_url ) => {
218
+ global . window . location = {
219
+ href : url ,
220
+ } ;
221
+
222
+ portal_url = portal_url || url ;
223
+
224
+ document . body . dataset . portalUrl = portal_url ;
225
+ } ;
226
+
227
+ it ( "navigation roundtrip" , ( ) => {
228
+ document . body . innerHTML = `
229
+ <nav class="pat-navigation"
230
+ data-pat-navigation="in-path-class: inPath">
231
+ <ul>
232
+ <li>
233
+ <a href="/">Home</a>
234
+ </li>
235
+ <li>
236
+ <a href="/path1">p1</a>
237
+ </li>
238
+ <li>
239
+ <a href="/path2">p2</a>
240
+ <ul>
241
+ <li>
242
+ <a href="/path2/path2.1">p2.1</a>
243
+ </li>
244
+ <li>
245
+ <a href="/path2/path2.2">p2.2</a>
246
+ <ul>
247
+ <li>
248
+ <a href="/path2/path2.2/path2.2.1">p2.2.1</a>
249
+ </li>
250
+ <li>
251
+ <a href="/path2/path2.2/path2.2.2">p2.2.2</a>
252
+ </li>
253
+ </ul>
254
+ </li>
255
+ </ul>
256
+ </li>
257
+ <li>
258
+ <a href="../../path3">p1</a>
259
+ </li>
260
+ <li>
261
+ <a href="https://patternslib.com/path4">p1</a>
262
+ </li>
263
+ </ul>
264
+ </nav>
265
+ ` ;
266
+
267
+ set_url ( "https://patternslib.com/" ) ;
268
+
269
+ const instance = new Pattern ( document . querySelector ( ".pat-navigation" ) ) ;
270
+
271
+ const it0 = document . querySelector ( "a[href='/']" ) ;
272
+ const it1 = document . querySelector ( "a[href='/path1']" ) ;
273
+ const it2 = document . querySelector ( "a[href='/path2']" ) ;
274
+ const it21 = document . querySelector ( "a[href='/path2/path2.1']" ) ;
275
+ const it22 = document . querySelector ( "a[href='/path2/path2.2']" ) ;
276
+ const it221 = document . querySelector ( "a[href='/path2/path2.2/path2.2.1']" ) ;
277
+ const it222 = document . querySelector ( "a[href='/path2/path2.2/path2.2.2']" ) ;
278
+ const it3 = document . querySelector ( "a[href='../../path3']" ) ;
279
+ const it4 = document . querySelector ( "a[href='https://patternslib.com/path4']" ) ;
280
+
281
+ expect ( document . querySelectorAll ( ".current" ) . length ) . toBe ( 2 ) ;
282
+ expect ( document . querySelectorAll ( ".inPath" ) . length ) . toBe ( 0 ) ;
283
+ expect ( document . querySelector ( ".current a" ) ) . toBe ( it0 ) ;
284
+
285
+ instance . clear_items ( ) ;
286
+ instance . mark_items_url ( "https://patternslib.com/path1" ) ;
287
+
288
+ expect ( document . querySelectorAll ( ".current" ) . length ) . toBe ( 2 ) ;
289
+ expect ( document . querySelectorAll ( ".inPath" ) . length ) . toBe ( 0 ) ;
290
+ expect ( document . querySelector ( ".current a" ) ) . toBe ( it1 ) ;
291
+
292
+ instance . clear_items ( ) ;
293
+ instance . mark_items_url ( "https://patternslib.com/path2" ) ;
294
+
295
+ expect ( document . querySelectorAll ( ".current" ) . length ) . toBe ( 2 ) ;
296
+ expect ( document . querySelectorAll ( ".inPath" ) . length ) . toBe ( 0 ) ;
297
+ expect ( document . querySelector ( ".current a" ) ) . toBe ( it2 ) ;
298
+
299
+ instance . clear_items ( ) ;
300
+ instance . mark_items_url ( "https://patternslib.com/path2/path2.1" ) ;
301
+
302
+ expect ( document . querySelectorAll ( ".current" ) . length ) . toBe ( 2 ) ;
303
+ expect ( document . querySelectorAll ( ".inPath" ) . length ) . toBe ( 2 ) ;
304
+ expect ( document . querySelector ( ".current a" ) ) . toBe ( it21 ) ;
305
+
306
+ instance . clear_items ( ) ;
307
+ instance . mark_items_url ( "https://patternslib.com/path2/path2.2" ) ;
308
+
309
+ expect ( document . querySelectorAll ( ".current" ) . length ) . toBe ( 2 ) ;
310
+ expect ( document . querySelectorAll ( ".inPath" ) . length ) . toBe ( 2 ) ;
311
+ expect ( document . querySelector ( ".current a" ) ) . toBe ( it22 ) ;
312
+
313
+ instance . clear_items ( ) ;
314
+ instance . mark_items_url ( "https://patternslib.com/path2/path2.2/path2.2.1" ) ;
315
+
316
+ expect ( document . querySelectorAll ( ".current" ) . length ) . toBe ( 2 ) ;
317
+ expect ( document . querySelectorAll ( ".inPath" ) . length ) . toBe ( 4 ) ;
318
+ expect ( document . querySelector ( ".current a" ) ) . toBe ( it221 ) ;
319
+
320
+ instance . clear_items ( ) ;
321
+ instance . mark_items_url ( "https://patternslib.com/path2/path2.2/path2.2.2" ) ;
322
+
323
+ expect ( document . querySelectorAll ( ".current" ) . length ) . toBe ( 2 ) ;
324
+ expect ( document . querySelectorAll ( ".inPath" ) . length ) . toBe ( 4 ) ;
325
+ expect ( document . querySelector ( ".current a" ) ) . toBe ( it222 ) ;
326
+
327
+ instance . clear_items ( ) ;
328
+ instance . mark_items_url ( "https://patternslib.com/path3" ) ;
329
+
330
+ expect ( document . querySelectorAll ( ".current" ) . length ) . toBe ( 2 ) ;
331
+ expect ( document . querySelectorAll ( ".inPath" ) . length ) . toBe ( 0 ) ;
332
+ expect ( document . querySelector ( ".current a" ) ) . toBe ( it3 ) ;
333
+
334
+ instance . clear_items ( ) ;
335
+ instance . mark_items_url ( "https://patternslib.com/path4" ) ;
336
+
337
+ expect ( document . querySelectorAll ( ".current" ) . length ) . toBe ( 2 ) ;
338
+ expect ( document . querySelectorAll ( ".inPath" ) . length ) . toBe ( 0 ) ;
339
+ expect ( document . querySelector ( ".current a" ) ) . toBe ( it4 ) ;
340
+ } ) ;
341
+ } ) ;
0 commit comments