@internetarchive/collection-browser 4.3.1-alpha-webdev8165.0 → 4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/index.js.map +1 -1
  2. package/dist/src/data-source/collection-browser-data-source.js.map +1 -1
  3. package/dist/src/manage/manage-bar.js +77 -77
  4. package/dist/src/manage/manage-bar.js.map +1 -1
  5. package/dist/src/models.d.ts +6 -0
  6. package/dist/src/models.js +16 -7
  7. package/dist/src/models.js.map +1 -1
  8. package/dist/src/restoration-state-handler.js +3 -1
  9. package/dist/src/restoration-state-handler.js.map +1 -1
  10. package/dist/src/tiles/base-tile-component.js.map +1 -1
  11. package/dist/src/tiles/grid/item-tile.js +138 -138
  12. package/dist/src/tiles/grid/item-tile.js.map +1 -1
  13. package/dist/src/tiles/models.js.map +1 -1
  14. package/dist/src/tiles/tile-dispatcher.js +216 -216
  15. package/dist/src/tiles/tile-dispatcher.js.map +1 -1
  16. package/dist/src/tiles/tile-display-value-provider.js.map +1 -1
  17. package/dist/test/data-source/collection-browser-data-source.test.js +2 -2
  18. package/dist/test/data-source/collection-browser-data-source.test.js.map +1 -1
  19. package/dist/test/manage/manage-bar.test.js +33 -33
  20. package/dist/test/manage/manage-bar.test.js.map +1 -1
  21. package/dist/test/restoration-state-handler.test.js +0 -70
  22. package/dist/test/restoration-state-handler.test.js.map +1 -1
  23. package/dist/test/tiles/list/tile-list-compact-header.test.js +12 -12
  24. package/dist/test/tiles/list/tile-list-compact-header.test.js.map +1 -1
  25. package/dist/test/tiles/list/tile-list.test.js +134 -134
  26. package/dist/test/tiles/list/tile-list.test.js.map +1 -1
  27. package/index.ts +28 -28
  28. package/package.json +2 -2
  29. package/src/data-source/collection-browser-data-source.ts +1465 -1465
  30. package/src/manage/manage-bar.ts +276 -276
  31. package/src/models.ts +895 -879
  32. package/src/restoration-state-handler.ts +550 -546
  33. package/src/tiles/base-tile-component.ts +65 -65
  34. package/src/tiles/grid/item-tile.ts +346 -346
  35. package/src/tiles/models.ts +8 -8
  36. package/src/tiles/tile-dispatcher.ts +527 -527
  37. package/src/tiles/tile-display-value-provider.ts +134 -134
  38. package/test/data-source/collection-browser-data-source.test.ts +193 -193
  39. package/test/manage/manage-bar.test.ts +347 -347
  40. package/test/restoration-state-handler.test.ts +480 -569
  41. package/test/tiles/list/tile-list-compact-header.test.ts +43 -43
  42. package/test/tiles/list/tile-list.test.ts +576 -576
@@ -1,569 +1,480 @@
1
- import { SearchType } from '@internetarchive/search-service';
2
- import { expect } from '@open-wc/testing';
3
- import { SortField, getDefaultSelectedFacets } from '../src/models';
4
- import { RestorationStateHandler } from '../src/restoration-state-handler';
5
-
6
- describe('Restoration state handler', () => {
7
- it('should restore query from URL', async () => {
8
- const handler = new RestorationStateHandler({ context: 'search' });
9
-
10
- const url = new URL(window.location.href);
11
- url.search = '?query=boop';
12
- window.history.replaceState({ path: url.href }, '', url.href);
13
-
14
- const restorationState = handler.getRestorationState();
15
- expect(restorationState.baseQuery).to.equal('boop');
16
- });
17
-
18
- it('should restore default search type from URL without valid sin', async () => {
19
- const handler = new RestorationStateHandler({ context: 'search' });
20
-
21
- const url = new URL(window.location.href);
22
- url.search = '?sin=foo';
23
- window.history.replaceState({ path: url.href }, '', url.href);
24
-
25
- const restorationState = handler.getRestorationState();
26
- expect(restorationState.searchType).to.equal(SearchType.DEFAULT);
27
- });
28
-
29
- it('should restore default search type if sin explicitly empty in URL', async () => {
30
- const handler = new RestorationStateHandler({ context: 'search' });
31
-
32
- const url = new URL(window.location.href);
33
- url.search = '?sin=';
34
- window.history.replaceState({ path: url.href }, '', url.href);
35
-
36
- const restorationState = handler.getRestorationState();
37
- expect(restorationState.searchType).to.equal(SearchType.DEFAULT);
38
- });
39
-
40
- it('should restore full text search type from URL', async () => {
41
- const handler = new RestorationStateHandler({ context: 'search' });
42
-
43
- const url = new URL(window.location.href);
44
- url.search = '?sin=TXT';
45
- window.history.replaceState({ path: url.href }, '', url.href);
46
-
47
- const restorationState = handler.getRestorationState();
48
- expect(restorationState.searchType).to.equal(SearchType.FULLTEXT);
49
- });
50
-
51
- it('should restore radio search type from URL', async () => {
52
- const handler = new RestorationStateHandler({ context: 'search' });
53
-
54
- const url = new URL(window.location.href);
55
- url.search = '?sin=RADIO';
56
- window.history.replaceState({ path: url.href }, '', url.href);
57
-
58
- const restorationState = handler.getRestorationState();
59
- expect(restorationState.searchType).to.equal(SearchType.RADIO);
60
- });
61
-
62
- it('should restore TV search type from URL', async () => {
63
- const handler = new RestorationStateHandler({ context: 'search' });
64
-
65
- const url = new URL(window.location.href);
66
- url.search = '?sin=TV';
67
- window.history.replaceState({ path: url.href }, '', url.href);
68
-
69
- const restorationState = handler.getRestorationState();
70
- expect(restorationState.searchType).to.equal(SearchType.TV);
71
- });
72
-
73
- it('should restore metadata search type from URL', async () => {
74
- const handler = new RestorationStateHandler({ context: 'search' });
75
-
76
- const url = new URL(window.location.href);
77
- url.search = '?sin=MD';
78
- window.history.replaceState({ path: url.href }, '', url.href);
79
-
80
- const restorationState = handler.getRestorationState();
81
- expect(restorationState.searchType).to.equal(SearchType.METADATA);
82
- });
83
-
84
- it('should restore page number from URL', async () => {
85
- const handler = new RestorationStateHandler({ context: 'search' });
86
-
87
- const url = new URL(window.location.href);
88
- url.search = '?page=42';
89
- window.history.replaceState({ path: url.href }, '', url.href);
90
-
91
- const restorationState = handler.getRestorationState();
92
- expect(restorationState.currentPage).to.equal(42);
93
- });
94
-
95
- it('should restore selected year facets from URL', async () => {
96
- const handler = new RestorationStateHandler({ context: 'search' });
97
-
98
- const url = new URL(window.location.href);
99
- url.search = '?and[]=year:"2018"';
100
- window.history.replaceState({ path: url.href }, '', url.href);
101
-
102
- const restorationState = handler.getRestorationState();
103
- expect(restorationState.selectedFacets.year?.['2018'].state).to.equal(
104
- 'selected',
105
- );
106
- });
107
-
108
- it('should ignore unrecognized facet types in URL', async () => {
109
- const handler = new RestorationStateHandler({ context: 'search' });
110
-
111
- const url = new URL(window.location.href);
112
- url.search = '?and[]=foo:"bar"';
113
- window.history.replaceState({ path: url.href }, '', url.href);
114
-
115
- const restorationState = handler.getRestorationState();
116
- expect(restorationState.selectedFacets).to.deep.equal(
117
- getDefaultSelectedFacets(),
118
- );
119
- });
120
-
121
- it('should restore selected date range facets from URL', async () => {
122
- const handler = new RestorationStateHandler({ context: 'search' });
123
-
124
- const url = new URL(window.location.href);
125
- url.search = '?and[]=year:"2018+TO+2021"';
126
- window.history.replaceState({ path: url.href }, '', url.href);
127
-
128
- const restorationState = handler.getRestorationState();
129
- expect(restorationState.minSelectedDate).to.equal('2018');
130
- expect(restorationState.maxSelectedDate).to.equal('2021');
131
- });
132
-
133
- it('should restore creator filter from URL', async () => {
134
- const handler = new RestorationStateHandler({ context: 'search' });
135
-
136
- const url = new URL(window.location.href);
137
- url.search = '?and[]=firstCreator:F';
138
- window.history.replaceState({ path: url.href }, '', url.href);
139
-
140
- const restorationState = handler.getRestorationState();
141
- expect(restorationState.selectedCreatorFilter).to.equal('F');
142
- });
143
-
144
- it('should restore title filter from URL', async () => {
145
- const handler = new RestorationStateHandler({ context: 'search' });
146
-
147
- const url = new URL(window.location.href);
148
- url.search = '?and[]=firstTitle:F';
149
- window.history.replaceState({ path: url.href }, '', url.href);
150
-
151
- const restorationState = handler.getRestorationState();
152
- expect(restorationState.selectedTitleFilter).to.equal('F');
153
- });
154
-
155
- it('should restore other selected facets from URL', async () => {
156
- const handler = new RestorationStateHandler({ context: 'search' });
157
-
158
- const url = new URL(window.location.href);
159
- url.search = '?and[]=subject:"foo"';
160
- window.history.replaceState({ path: url.href }, '', url.href);
161
-
162
- const restorationState = handler.getRestorationState();
163
- expect(restorationState.selectedFacets.subject?.foo.state).to.equal(
164
- 'selected',
165
- );
166
- });
167
-
168
- it('should restore negative facets from URL', async () => {
169
- const handler = new RestorationStateHandler({ context: 'search' });
170
-
171
- const url = new URL(window.location.href);
172
- url.search = '?not[]=year:2018';
173
- window.history.replaceState({ path: url.href }, '', url.href);
174
-
175
- const restorationState = handler.getRestorationState();
176
- expect(restorationState.selectedFacets.year?.['2018'].state).to.equal(
177
- 'hidden',
178
- );
179
- });
180
-
181
- it('should restore multiple selected/negative facets from URL', async () => {
182
- const handler = new RestorationStateHandler({ context: 'search' });
183
-
184
- const url = new URL(window.location.href);
185
- url.search =
186
- '?and[]=collection:"foo"&and[]=collection:"bar"&not[]=collection:"baz"&not[]=collection:"boop"';
187
- window.history.replaceState({ path: url.href }, '', url.href);
188
-
189
- const restorationState = handler.getRestorationState();
190
-
191
- expect(restorationState.selectedFacets.collection?.foo.state).to.equal(
192
- 'selected',
193
- );
194
- expect(restorationState.selectedFacets.collection?.bar.state).to.equal(
195
- 'selected',
196
- );
197
-
198
- expect(restorationState.selectedFacets.collection?.baz.state).to.equal(
199
- 'hidden',
200
- );
201
- expect(restorationState.selectedFacets.collection?.boop.state).to.equal(
202
- 'hidden',
203
- );
204
- });
205
-
206
- it('negative facets take precedence if both present in URL', async () => {
207
- const handler = new RestorationStateHandler({ context: 'search' });
208
-
209
- const url = new URL(window.location.href);
210
- url.search = '?and[]=collection:"foo"&not[]=collection:"foo"';
211
- window.history.replaceState({ path: url.href }, '', url.href);
212
-
213
- const restorationState = handler.getRestorationState();
214
- expect(restorationState.selectedFacets.collection?.foo.state).to.equal(
215
- 'hidden',
216
- );
217
- });
218
-
219
- it('should restore selected facets with numbers in the square brackets', async () => {
220
- const handler = new RestorationStateHandler({ context: 'search' });
221
-
222
- const url = new URL(window.location.href);
223
- url.search = '?and[12]=subject:"foo"';
224
- window.history.replaceState({ path: url.href }, '', url.href);
225
-
226
- const restorationState = handler.getRestorationState();
227
- expect(restorationState.selectedFacets.subject?.foo.state).to.equal(
228
- 'selected',
229
- );
230
- });
231
-
232
- it('should restore negative facets with numbers in the square brackets', async () => {
233
- const handler = new RestorationStateHandler({ context: 'search' });
234
-
235
- const url = new URL(window.location.href);
236
- url.search = '?not[12]=year:2018';
237
- window.history.replaceState({ path: url.href }, '', url.href);
238
-
239
- const restorationState = handler.getRestorationState();
240
- expect(restorationState.selectedFacets.year?.['2018'].state).to.equal(
241
- 'hidden',
242
- );
243
- });
244
-
245
- it('should restore any TV clip filters from URL', async () => {
246
- const handler = new RestorationStateHandler({ context: 'search' });
247
- const url = new URL(window.location.href);
248
-
249
- // Commercials
250
- url.search = '?only_commercials=1';
251
- window.history.replaceState({ path: url.href }, '', url.href);
252
- const commercialsRestorationState = handler.getRestorationState();
253
- expect(
254
- commercialsRestorationState.selectedFacets.clip_type?.commercial.state,
255
- ).to.equal('selected');
256
-
257
- // Fact checks
258
- url.search = '?only_factchecks=1';
259
- window.history.replaceState({ path: url.href }, '', url.href);
260
- const factchecksRestorationState = handler.getRestorationState();
261
- expect(
262
- factchecksRestorationState.selectedFacets.clip_type?.['fact check'].state,
263
- ).to.equal('selected');
264
-
265
- // Quotes
266
- url.search = '?only_quotes=1';
267
- window.history.replaceState({ path: url.href }, '', url.href);
268
- const quotesRestorationState = handler.getRestorationState();
269
- expect(
270
- quotesRestorationState.selectedFacets.clip_type?.quote.state,
271
- ).to.equal('selected');
272
-
273
- // No filter param
274
- url.search = '';
275
- window.history.replaceState({ path: url.href }, '', url.href);
276
- const unfilteredRestorationState = handler.getRestorationState();
277
- expect(unfilteredRestorationState.selectedFacets.clip_type).to.deep.equal(
278
- {},
279
- );
280
- });
281
-
282
- it('should restore sort from URL (space format)', async () => {
283
- const handler = new RestorationStateHandler({ context: 'search' });
284
-
285
- const url = new URL(window.location.href);
286
- url.search = '?sort=date+desc';
287
- window.history.replaceState({ path: url.href }, '', url.href);
288
-
289
- const restorationState = handler.getRestorationState();
290
- expect(restorationState.selectedSort).to.equal('date');
291
- expect(restorationState.sortDirection).to.equal('desc');
292
- });
293
-
294
- it('should restore sort from URL (prefix format, desc)', async () => {
295
- const handler = new RestorationStateHandler({ context: 'search' });
296
-
297
- const url = new URL(window.location.href);
298
- url.search = '?sort=-date';
299
- window.history.replaceState({ path: url.href }, '', url.href);
300
-
301
- const restorationState = handler.getRestorationState();
302
- expect(restorationState.selectedSort).to.equal('date');
303
- expect(restorationState.sortDirection).to.equal('desc');
304
- });
305
-
306
- it('should restore sort from URL (prefix format, asc)', async () => {
307
- const handler = new RestorationStateHandler({ context: 'search' });
308
-
309
- const url = new URL(window.location.href);
310
- url.search = '?sort=date';
311
- window.history.replaceState({ path: url.href }, '', url.href);
312
-
313
- const restorationState = handler.getRestorationState();
314
- expect(restorationState.selectedSort).to.equal('date');
315
- expect(restorationState.sortDirection).to.equal('asc');
316
- });
317
-
318
- it('should restore sort from URL (space format)', async () => {
319
- const handler = new RestorationStateHandler({ context: 'search' });
320
-
321
- const url = new URL(window.location.href);
322
- url.search = '?sort=foo+desc';
323
- window.history.replaceState({ path: url.href }, '', url.href);
324
-
325
- const restorationState = handler.getRestorationState();
326
- expect(restorationState.selectedSort).to.equal('unrecognized');
327
- expect(restorationState.sortDirection).to.equal('desc');
328
- });
329
-
330
- it('should restore unrecognized sort from URL (prefix format)', async () => {
331
- const handler = new RestorationStateHandler({ context: 'search' });
332
-
333
- const url = new URL(window.location.href);
334
- url.search = '?sort=-foo';
335
- window.history.replaceState({ path: url.href }, '', url.href);
336
-
337
- const restorationState = handler.getRestorationState();
338
- expect(restorationState.selectedSort).to.equal('unrecognized');
339
- expect(restorationState.sortDirection).to.equal('desc');
340
- });
341
-
342
- it('should save direction to URL even for unrecognized sort fields', async () => {
343
- const url = new URL(window.location.href);
344
- url.search = '?sort=foo';
345
- window.history.replaceState({ path: url.href }, '', url.href);
346
-
347
- const handler = new RestorationStateHandler({ context: 'search' });
348
- handler.persistState({
349
- selectedSort: SortField.unrecognized,
350
- sortDirection: 'desc',
351
- selectedFacets: getDefaultSelectedFacets(),
352
- });
353
-
354
- expect(window.location.search).to.equal('?sort=-foo');
355
- });
356
-
357
- it('should keep existing direction for unrecognized sort fields when unspecified in state', async () => {
358
- const url = new URL(window.location.href);
359
- url.search = '?sort=foo+desc';
360
- window.history.replaceState({ path: url.href }, '', url.href);
361
-
362
- const handler = new RestorationStateHandler({ context: 'search' });
363
- handler.persistState({
364
- selectedSort: SortField.unrecognized,
365
- selectedFacets: getDefaultSelectedFacets(),
366
- });
367
-
368
- expect(window.location.search).to.equal('?sort=-foo');
369
- });
370
-
371
- it('should just ignore unrecognized sort fields w/ unknown formats', async () => {
372
- const url = new URL(window.location.href);
373
- url.search = '?sort=+foo';
374
- window.history.replaceState({ path: url.href }, '', url.href);
375
-
376
- const handler = new RestorationStateHandler({ context: 'search' });
377
- handler.persistState({
378
- selectedSort: SortField.unrecognized,
379
- selectedFacets: getDefaultSelectedFacets(),
380
- });
381
-
382
- expect(window.location.search).to.equal('?sort=+foo');
383
- });
384
-
385
- it('should not save current page state to the URL for page 1', async () => {
386
- const url = new URL(window.location.href);
387
- url.search = '';
388
- window.history.replaceState({ path: url.href }, '', url.href);
389
-
390
- const handler = new RestorationStateHandler({ context: 'search' });
391
- handler.persistState({
392
- currentPage: 1,
393
- selectedFacets: getDefaultSelectedFacets(),
394
- });
395
-
396
- expect(window.location.search).to.be.empty;
397
- });
398
-
399
- it('should save current page state to the URL when page > 1', async () => {
400
- const url = new URL(window.location.href);
401
- url.search = '';
402
- window.history.replaceState({ path: url.href }, '', url.href);
403
-
404
- const handler = new RestorationStateHandler({ context: 'search' });
405
- handler.persistState({
406
- currentPage: 2,
407
- selectedFacets: getDefaultSelectedFacets(),
408
- });
409
-
410
- expect(window.location.search).to.equal('?page=2');
411
- });
412
-
413
- it('should upgrade legacy search params to new ones', async () => {
414
- const url = new URL(window.location.href);
415
- url.search = '?q=foo';
416
- window.history.replaceState({ path: url.href }, '', url.href);
417
-
418
- const handler = new RestorationStateHandler({ context: 'search' });
419
- const restorationState = handler.getRestorationState();
420
- expect(restorationState.baseQuery).to.equal('foo');
421
-
422
- handler.persistState(restorationState);
423
- expect(window.location.search).to.equal('?query=foo');
424
- });
425
-
426
- it('should remove empty sin param', async () => {
427
- const url = new URL(window.location.href);
428
- url.search = '?sin=';
429
- window.history.replaceState({ path: url.href }, '', url.href);
430
-
431
- const handler = new RestorationStateHandler({ context: 'search' });
432
-
433
- handler.persistState({ selectedFacets: getDefaultSelectedFacets() });
434
- expect(window.location.search).to.equal('');
435
- });
436
-
437
- it('should persist metadata search type only when option is true', async () => {
438
- const url = new URL(window.location.href);
439
- url.search = '?sin=';
440
- window.history.replaceState({ path: url.href }, '', url.href);
441
-
442
- const handler = new RestorationStateHandler({ context: 'search' });
443
-
444
- handler.persistState(
445
- {
446
- selectedFacets: getDefaultSelectedFacets(),
447
- searchType: SearchType.METADATA,
448
- },
449
- { persistMetadataSearchType: false },
450
- );
451
- expect(window.location.search).to.equal('');
452
-
453
- handler.persistState(
454
- {
455
- selectedFacets: getDefaultSelectedFacets(),
456
- searchType: SearchType.METADATA,
457
- },
458
- { persistMetadataSearchType: true },
459
- );
460
- expect(window.location.search).to.equal('?sin=MD');
461
- });
462
-
463
- it('should write relevance sort to URL when explicitly selected', async () => {
464
- const url = new URL(window.location.href);
465
- url.search = '';
466
- window.history.replaceState({ path: url.href }, '', url.href);
467
-
468
- const handler = new RestorationStateHandler({ context: 'search' });
469
- handler.persistState({
470
- selectedSort: SortField.relevance,
471
- selectedFacets: getDefaultSelectedFacets(),
472
- });
473
-
474
- expect(window.location.search).to.equal('?sort=relevance');
475
- });
476
-
477
- it('should write relevance sort to URL for TV search', async () => {
478
- const url = new URL(window.location.href);
479
- url.search = '';
480
- window.history.replaceState({ path: url.href }, '', url.href);
481
-
482
- const handler = new RestorationStateHandler({ context: 'search' });
483
- handler.persistState({
484
- searchType: SearchType.TV,
485
- selectedSort: SortField.relevance,
486
- selectedFacets: getDefaultSelectedFacets(),
487
- });
488
-
489
- expect(window.location.search).to.include('sort=relevance');
490
- });
491
-
492
- it('should not write sort param for SortField.default', async () => {
493
- const url = new URL(window.location.href);
494
- url.search = '';
495
- window.history.replaceState({ path: url.href }, '', url.href);
496
-
497
- const handler = new RestorationStateHandler({ context: 'search' });
498
- handler.persistState({
499
- selectedSort: SortField.default,
500
- selectedFacets: getDefaultSelectedFacets(),
501
- });
502
-
503
- expect(window.location.search).to.not.include('sort');
504
- });
505
-
506
- it('should write datefavorited sort to URL when explicitly selected', async () => {
507
- const url = new URL(window.location.href);
508
- url.search = '';
509
- window.history.replaceState({ path: url.href }, '', url.href);
510
-
511
- const handler = new RestorationStateHandler({ context: 'search' });
512
- handler.persistState({
513
- selectedSort: SortField.datefavorited,
514
- sortDirection: 'desc',
515
- selectedFacets: getDefaultSelectedFacets(),
516
- });
517
-
518
- expect(window.location.search).to.equal('?sort=-favoritedate');
519
- });
520
-
521
- it('should round-trip relevance sort via URL', async () => {
522
- // 1. Set the URL to include the new canonical relevance sort param
523
- const url = new URL(window.location.href);
524
- url.search = '?sort=relevance';
525
- window.history.replaceState({ path: url.href }, '', url.href);
526
-
527
- const handler = new RestorationStateHandler({ context: 'search' });
528
-
529
- // 2. Load state from the URL and verify it resolves to relevance sort
530
- const restorationState = handler.getRestorationState();
531
- expect(restorationState.selectedSort).to.equal(SortField.relevance);
532
-
533
- // 3. Persist the state back and verify the sort param is preserved
534
- handler.persistState(restorationState);
535
- expect(window.location.search).to.include('sort=relevance');
536
- });
537
-
538
- it('should still resolve legacy _score URL param to relevance sort', async () => {
539
- const url = new URL(window.location.href);
540
- url.search = '?sort=_score';
541
- window.history.replaceState({ path: url.href }, '', url.href);
542
-
543
- const handler = new RestorationStateHandler({ context: 'search' });
544
- const restorationState = handler.getRestorationState();
545
- expect(restorationState.selectedSort).to.equal(SortField.relevance);
546
-
547
- // Re-persisting upgrades the URL to the canonical form
548
- handler.persistState(restorationState);
549
- expect(window.location.search).to.include('sort=relevance');
550
- });
551
-
552
- it('round trip load/persist should erase numbers in square brackets', async () => {
553
- const handler = new RestorationStateHandler({ context: 'search' });
554
-
555
- const url = new URL(window.location.href);
556
- url.search = '?and[0]=subject:"foo"';
557
- window.history.replaceState({ path: url.href }, '', url.href);
558
-
559
- // Load state from the URL and immediately persist it back to the URL
560
- const restorationState = handler.getRestorationState();
561
- handler.persistState(restorationState);
562
-
563
- // Ensure the new URL includes the "normalized" facet parameter and not the numbered one
564
- expect(decodeURIComponent(window.location.search)).to.include(
565
- 'and[]=subject:"foo"',
566
- );
567
- expect(new URL(window.location.href).searchParams.get('and[0]')).to.be.null;
568
- });
569
- });
1
+ import { SearchType } from '@internetarchive/search-service';
2
+ import { expect } from '@open-wc/testing';
3
+ import { SortField, getDefaultSelectedFacets } from '../src/models';
4
+ import { RestorationStateHandler } from '../src/restoration-state-handler';
5
+
6
+ describe('Restoration state handler', () => {
7
+ it('should restore query from URL', async () => {
8
+ const handler = new RestorationStateHandler({ context: 'search' });
9
+
10
+ const url = new URL(window.location.href);
11
+ url.search = '?query=boop';
12
+ window.history.replaceState({ path: url.href }, '', url.href);
13
+
14
+ const restorationState = handler.getRestorationState();
15
+ expect(restorationState.baseQuery).to.equal('boop');
16
+ });
17
+
18
+ it('should restore default search type from URL without valid sin', async () => {
19
+ const handler = new RestorationStateHandler({ context: 'search' });
20
+
21
+ const url = new URL(window.location.href);
22
+ url.search = '?sin=foo';
23
+ window.history.replaceState({ path: url.href }, '', url.href);
24
+
25
+ const restorationState = handler.getRestorationState();
26
+ expect(restorationState.searchType).to.equal(SearchType.DEFAULT);
27
+ });
28
+
29
+ it('should restore default search type if sin explicitly empty in URL', async () => {
30
+ const handler = new RestorationStateHandler({ context: 'search' });
31
+
32
+ const url = new URL(window.location.href);
33
+ url.search = '?sin=';
34
+ window.history.replaceState({ path: url.href }, '', url.href);
35
+
36
+ const restorationState = handler.getRestorationState();
37
+ expect(restorationState.searchType).to.equal(SearchType.DEFAULT);
38
+ });
39
+
40
+ it('should restore full text search type from URL', async () => {
41
+ const handler = new RestorationStateHandler({ context: 'search' });
42
+
43
+ const url = new URL(window.location.href);
44
+ url.search = '?sin=TXT';
45
+ window.history.replaceState({ path: url.href }, '', url.href);
46
+
47
+ const restorationState = handler.getRestorationState();
48
+ expect(restorationState.searchType).to.equal(SearchType.FULLTEXT);
49
+ });
50
+
51
+ it('should restore radio search type from URL', async () => {
52
+ const handler = new RestorationStateHandler({ context: 'search' });
53
+
54
+ const url = new URL(window.location.href);
55
+ url.search = '?sin=RADIO';
56
+ window.history.replaceState({ path: url.href }, '', url.href);
57
+
58
+ const restorationState = handler.getRestorationState();
59
+ expect(restorationState.searchType).to.equal(SearchType.RADIO);
60
+ });
61
+
62
+ it('should restore TV search type from URL', async () => {
63
+ const handler = new RestorationStateHandler({ context: 'search' });
64
+
65
+ const url = new URL(window.location.href);
66
+ url.search = '?sin=TV';
67
+ window.history.replaceState({ path: url.href }, '', url.href);
68
+
69
+ const restorationState = handler.getRestorationState();
70
+ expect(restorationState.searchType).to.equal(SearchType.TV);
71
+ });
72
+
73
+ it('should restore metadata search type from URL', async () => {
74
+ const handler = new RestorationStateHandler({ context: 'search' });
75
+
76
+ const url = new URL(window.location.href);
77
+ url.search = '?sin=MD';
78
+ window.history.replaceState({ path: url.href }, '', url.href);
79
+
80
+ const restorationState = handler.getRestorationState();
81
+ expect(restorationState.searchType).to.equal(SearchType.METADATA);
82
+ });
83
+
84
+ it('should restore page number from URL', async () => {
85
+ const handler = new RestorationStateHandler({ context: 'search' });
86
+
87
+ const url = new URL(window.location.href);
88
+ url.search = '?page=42';
89
+ window.history.replaceState({ path: url.href }, '', url.href);
90
+
91
+ const restorationState = handler.getRestorationState();
92
+ expect(restorationState.currentPage).to.equal(42);
93
+ });
94
+
95
+ it('should restore selected year facets from URL', async () => {
96
+ const handler = new RestorationStateHandler({ context: 'search' });
97
+
98
+ const url = new URL(window.location.href);
99
+ url.search = '?and[]=year:"2018"';
100
+ window.history.replaceState({ path: url.href }, '', url.href);
101
+
102
+ const restorationState = handler.getRestorationState();
103
+ expect(restorationState.selectedFacets.year?.['2018'].state).to.equal(
104
+ 'selected',
105
+ );
106
+ });
107
+
108
+ it('should ignore unrecognized facet types in URL', async () => {
109
+ const handler = new RestorationStateHandler({ context: 'search' });
110
+
111
+ const url = new URL(window.location.href);
112
+ url.search = '?and[]=foo:"bar"';
113
+ window.history.replaceState({ path: url.href }, '', url.href);
114
+
115
+ const restorationState = handler.getRestorationState();
116
+ expect(restorationState.selectedFacets).to.deep.equal(
117
+ getDefaultSelectedFacets(),
118
+ );
119
+ });
120
+
121
+ it('should restore selected date range facets from URL', async () => {
122
+ const handler = new RestorationStateHandler({ context: 'search' });
123
+
124
+ const url = new URL(window.location.href);
125
+ url.search = '?and[]=year:"2018+TO+2021"';
126
+ window.history.replaceState({ path: url.href }, '', url.href);
127
+
128
+ const restorationState = handler.getRestorationState();
129
+ expect(restorationState.minSelectedDate).to.equal('2018');
130
+ expect(restorationState.maxSelectedDate).to.equal('2021');
131
+ });
132
+
133
+ it('should restore creator filter from URL', async () => {
134
+ const handler = new RestorationStateHandler({ context: 'search' });
135
+
136
+ const url = new URL(window.location.href);
137
+ url.search = '?and[]=firstCreator:F';
138
+ window.history.replaceState({ path: url.href }, '', url.href);
139
+
140
+ const restorationState = handler.getRestorationState();
141
+ expect(restorationState.selectedCreatorFilter).to.equal('F');
142
+ });
143
+
144
+ it('should restore title filter from URL', async () => {
145
+ const handler = new RestorationStateHandler({ context: 'search' });
146
+
147
+ const url = new URL(window.location.href);
148
+ url.search = '?and[]=firstTitle:F';
149
+ window.history.replaceState({ path: url.href }, '', url.href);
150
+
151
+ const restorationState = handler.getRestorationState();
152
+ expect(restorationState.selectedTitleFilter).to.equal('F');
153
+ });
154
+
155
+ it('should restore other selected facets from URL', async () => {
156
+ const handler = new RestorationStateHandler({ context: 'search' });
157
+
158
+ const url = new URL(window.location.href);
159
+ url.search = '?and[]=subject:"foo"';
160
+ window.history.replaceState({ path: url.href }, '', url.href);
161
+
162
+ const restorationState = handler.getRestorationState();
163
+ expect(restorationState.selectedFacets.subject?.foo.state).to.equal(
164
+ 'selected',
165
+ );
166
+ });
167
+
168
+ it('should restore negative facets from URL', async () => {
169
+ const handler = new RestorationStateHandler({ context: 'search' });
170
+
171
+ const url = new URL(window.location.href);
172
+ url.search = '?not[]=year:2018';
173
+ window.history.replaceState({ path: url.href }, '', url.href);
174
+
175
+ const restorationState = handler.getRestorationState();
176
+ expect(restorationState.selectedFacets.year?.['2018'].state).to.equal(
177
+ 'hidden',
178
+ );
179
+ });
180
+
181
+ it('should restore multiple selected/negative facets from URL', async () => {
182
+ const handler = new RestorationStateHandler({ context: 'search' });
183
+
184
+ const url = new URL(window.location.href);
185
+ url.search =
186
+ '?and[]=collection:"foo"&and[]=collection:"bar"&not[]=collection:"baz"&not[]=collection:"boop"';
187
+ window.history.replaceState({ path: url.href }, '', url.href);
188
+
189
+ const restorationState = handler.getRestorationState();
190
+
191
+ expect(restorationState.selectedFacets.collection?.foo.state).to.equal(
192
+ 'selected',
193
+ );
194
+ expect(restorationState.selectedFacets.collection?.bar.state).to.equal(
195
+ 'selected',
196
+ );
197
+
198
+ expect(restorationState.selectedFacets.collection?.baz.state).to.equal(
199
+ 'hidden',
200
+ );
201
+ expect(restorationState.selectedFacets.collection?.boop.state).to.equal(
202
+ 'hidden',
203
+ );
204
+ });
205
+
206
+ it('negative facets take precedence if both present in URL', async () => {
207
+ const handler = new RestorationStateHandler({ context: 'search' });
208
+
209
+ const url = new URL(window.location.href);
210
+ url.search = '?and[]=collection:"foo"&not[]=collection:"foo"';
211
+ window.history.replaceState({ path: url.href }, '', url.href);
212
+
213
+ const restorationState = handler.getRestorationState();
214
+ expect(restorationState.selectedFacets.collection?.foo.state).to.equal(
215
+ 'hidden',
216
+ );
217
+ });
218
+
219
+ it('should restore selected facets with numbers in the square brackets', async () => {
220
+ const handler = new RestorationStateHandler({ context: 'search' });
221
+
222
+ const url = new URL(window.location.href);
223
+ url.search = '?and[12]=subject:"foo"';
224
+ window.history.replaceState({ path: url.href }, '', url.href);
225
+
226
+ const restorationState = handler.getRestorationState();
227
+ expect(restorationState.selectedFacets.subject?.foo.state).to.equal(
228
+ 'selected',
229
+ );
230
+ });
231
+
232
+ it('should restore negative facets with numbers in the square brackets', async () => {
233
+ const handler = new RestorationStateHandler({ context: 'search' });
234
+
235
+ const url = new URL(window.location.href);
236
+ url.search = '?not[12]=year:2018';
237
+ window.history.replaceState({ path: url.href }, '', url.href);
238
+
239
+ const restorationState = handler.getRestorationState();
240
+ expect(restorationState.selectedFacets.year?.['2018'].state).to.equal(
241
+ 'hidden',
242
+ );
243
+ });
244
+
245
+ it('should restore any TV clip filters from URL', async () => {
246
+ const handler = new RestorationStateHandler({ context: 'search' });
247
+ const url = new URL(window.location.href);
248
+
249
+ // Commercials
250
+ url.search = '?only_commercials=1';
251
+ window.history.replaceState({ path: url.href }, '', url.href);
252
+ const commercialsRestorationState = handler.getRestorationState();
253
+ expect(
254
+ commercialsRestorationState.selectedFacets.clip_type?.commercial.state,
255
+ ).to.equal('selected');
256
+
257
+ // Fact checks
258
+ url.search = '?only_factchecks=1';
259
+ window.history.replaceState({ path: url.href }, '', url.href);
260
+ const factchecksRestorationState = handler.getRestorationState();
261
+ expect(
262
+ factchecksRestorationState.selectedFacets.clip_type?.['fact check'].state,
263
+ ).to.equal('selected');
264
+
265
+ // Quotes
266
+ url.search = '?only_quotes=1';
267
+ window.history.replaceState({ path: url.href }, '', url.href);
268
+ const quotesRestorationState = handler.getRestorationState();
269
+ expect(
270
+ quotesRestorationState.selectedFacets.clip_type?.quote.state,
271
+ ).to.equal('selected');
272
+
273
+ // No filter param
274
+ url.search = '';
275
+ window.history.replaceState({ path: url.href }, '', url.href);
276
+ const unfilteredRestorationState = handler.getRestorationState();
277
+ expect(unfilteredRestorationState.selectedFacets.clip_type).to.deep.equal(
278
+ {},
279
+ );
280
+ });
281
+
282
+ it('should restore sort from URL (space format)', async () => {
283
+ const handler = new RestorationStateHandler({ context: 'search' });
284
+
285
+ const url = new URL(window.location.href);
286
+ url.search = '?sort=date+desc';
287
+ window.history.replaceState({ path: url.href }, '', url.href);
288
+
289
+ const restorationState = handler.getRestorationState();
290
+ expect(restorationState.selectedSort).to.equal('date');
291
+ expect(restorationState.sortDirection).to.equal('desc');
292
+ });
293
+
294
+ it('should restore sort from URL (prefix format, desc)', async () => {
295
+ const handler = new RestorationStateHandler({ context: 'search' });
296
+
297
+ const url = new URL(window.location.href);
298
+ url.search = '?sort=-date';
299
+ window.history.replaceState({ path: url.href }, '', url.href);
300
+
301
+ const restorationState = handler.getRestorationState();
302
+ expect(restorationState.selectedSort).to.equal('date');
303
+ expect(restorationState.sortDirection).to.equal('desc');
304
+ });
305
+
306
+ it('should restore sort from URL (prefix format, asc)', async () => {
307
+ const handler = new RestorationStateHandler({ context: 'search' });
308
+
309
+ const url = new URL(window.location.href);
310
+ url.search = '?sort=date';
311
+ window.history.replaceState({ path: url.href }, '', url.href);
312
+
313
+ const restorationState = handler.getRestorationState();
314
+ expect(restorationState.selectedSort).to.equal('date');
315
+ expect(restorationState.sortDirection).to.equal('asc');
316
+ });
317
+
318
+ it('should restore sort from URL (space format)', async () => {
319
+ const handler = new RestorationStateHandler({ context: 'search' });
320
+
321
+ const url = new URL(window.location.href);
322
+ url.search = '?sort=foo+desc';
323
+ window.history.replaceState({ path: url.href }, '', url.href);
324
+
325
+ const restorationState = handler.getRestorationState();
326
+ expect(restorationState.selectedSort).to.equal('unrecognized');
327
+ expect(restorationState.sortDirection).to.equal('desc');
328
+ });
329
+
330
+ it('should restore unrecognized sort from URL (prefix format)', async () => {
331
+ const handler = new RestorationStateHandler({ context: 'search' });
332
+
333
+ const url = new URL(window.location.href);
334
+ url.search = '?sort=-foo';
335
+ window.history.replaceState({ path: url.href }, '', url.href);
336
+
337
+ const restorationState = handler.getRestorationState();
338
+ expect(restorationState.selectedSort).to.equal('unrecognized');
339
+ expect(restorationState.sortDirection).to.equal('desc');
340
+ });
341
+
342
+ it('should save direction to URL even for unrecognized sort fields', async () => {
343
+ const url = new URL(window.location.href);
344
+ url.search = '?sort=foo';
345
+ window.history.replaceState({ path: url.href }, '', url.href);
346
+
347
+ const handler = new RestorationStateHandler({ context: 'search' });
348
+ handler.persistState({
349
+ selectedSort: SortField.unrecognized,
350
+ sortDirection: 'desc',
351
+ selectedFacets: getDefaultSelectedFacets(),
352
+ });
353
+
354
+ expect(window.location.search).to.equal('?sort=-foo');
355
+ });
356
+
357
+ it('should keep existing direction for unrecognized sort fields when unspecified in state', async () => {
358
+ const url = new URL(window.location.href);
359
+ url.search = '?sort=foo+desc';
360
+ window.history.replaceState({ path: url.href }, '', url.href);
361
+
362
+ const handler = new RestorationStateHandler({ context: 'search' });
363
+ handler.persistState({
364
+ selectedSort: SortField.unrecognized,
365
+ selectedFacets: getDefaultSelectedFacets(),
366
+ });
367
+
368
+ expect(window.location.search).to.equal('?sort=-foo');
369
+ });
370
+
371
+ it('should just ignore unrecognized sort fields w/ unknown formats', async () => {
372
+ const url = new URL(window.location.href);
373
+ url.search = '?sort=+foo';
374
+ window.history.replaceState({ path: url.href }, '', url.href);
375
+
376
+ const handler = new RestorationStateHandler({ context: 'search' });
377
+ handler.persistState({
378
+ selectedSort: SortField.unrecognized,
379
+ selectedFacets: getDefaultSelectedFacets(),
380
+ });
381
+
382
+ expect(window.location.search).to.equal('?sort=+foo');
383
+ });
384
+
385
+ it('should not save current page state to the URL for page 1', async () => {
386
+ const url = new URL(window.location.href);
387
+ url.search = '';
388
+ window.history.replaceState({ path: url.href }, '', url.href);
389
+
390
+ const handler = new RestorationStateHandler({ context: 'search' });
391
+ handler.persistState({
392
+ currentPage: 1,
393
+ selectedFacets: getDefaultSelectedFacets(),
394
+ });
395
+
396
+ expect(window.location.search).to.be.empty;
397
+ });
398
+
399
+ it('should save current page state to the URL when page > 1', async () => {
400
+ const url = new URL(window.location.href);
401
+ url.search = '';
402
+ window.history.replaceState({ path: url.href }, '', url.href);
403
+
404
+ const handler = new RestorationStateHandler({ context: 'search' });
405
+ handler.persistState({
406
+ currentPage: 2,
407
+ selectedFacets: getDefaultSelectedFacets(),
408
+ });
409
+
410
+ expect(window.location.search).to.equal('?page=2');
411
+ });
412
+
413
+ it('should upgrade legacy search params to new ones', async () => {
414
+ const url = new URL(window.location.href);
415
+ url.search = '?q=foo';
416
+ window.history.replaceState({ path: url.href }, '', url.href);
417
+
418
+ const handler = new RestorationStateHandler({ context: 'search' });
419
+ const restorationState = handler.getRestorationState();
420
+ expect(restorationState.baseQuery).to.equal('foo');
421
+
422
+ handler.persistState(restorationState);
423
+ expect(window.location.search).to.equal('?query=foo');
424
+ });
425
+
426
+ it('should remove empty sin param', async () => {
427
+ const url = new URL(window.location.href);
428
+ url.search = '?sin=';
429
+ window.history.replaceState({ path: url.href }, '', url.href);
430
+
431
+ const handler = new RestorationStateHandler({ context: 'search' });
432
+
433
+ handler.persistState({ selectedFacets: getDefaultSelectedFacets() });
434
+ expect(window.location.search).to.equal('');
435
+ });
436
+
437
+ it('should persist metadata search type only when option is true', async () => {
438
+ const url = new URL(window.location.href);
439
+ url.search = '?sin=';
440
+ window.history.replaceState({ path: url.href }, '', url.href);
441
+
442
+ const handler = new RestorationStateHandler({ context: 'search' });
443
+
444
+ handler.persistState(
445
+ {
446
+ selectedFacets: getDefaultSelectedFacets(),
447
+ searchType: SearchType.METADATA,
448
+ },
449
+ { persistMetadataSearchType: false },
450
+ );
451
+ expect(window.location.search).to.equal('');
452
+
453
+ handler.persistState(
454
+ {
455
+ selectedFacets: getDefaultSelectedFacets(),
456
+ searchType: SearchType.METADATA,
457
+ },
458
+ { persistMetadataSearchType: true },
459
+ );
460
+ expect(window.location.search).to.equal('?sin=MD');
461
+ });
462
+
463
+ it('round trip load/persist should erase numbers in square brackets', async () => {
464
+ const handler = new RestorationStateHandler({ context: 'search' });
465
+
466
+ const url = new URL(window.location.href);
467
+ url.search = '?and[0]=subject:"foo"';
468
+ window.history.replaceState({ path: url.href }, '', url.href);
469
+
470
+ // Load state from the URL and immediately persist it back to the URL
471
+ const restorationState = handler.getRestorationState();
472
+ handler.persistState(restorationState);
473
+
474
+ // Ensure the new URL includes the "normalized" facet parameter and not the numbered one
475
+ expect(decodeURIComponent(window.location.search)).to.include(
476
+ 'and[]=subject:"foo"',
477
+ );
478
+ expect(new URL(window.location.href).searchParams.get('and[0]')).to.be.null;
479
+ });
480
+ });