@internetarchive/collection-browser 0.4.15-alpha.8 → 0.4.15

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.
@@ -7,18 +7,36 @@ import type {
7
7
  SearchType,
8
8
  } from '@internetarchive/search-service';
9
9
  import {
10
- mockSuccessSingleResult,
11
- mockSuccessMultipleResults,
10
+ getMockSuccessSingleResult,
11
+ getMockSuccessMultipleResults,
12
12
  getMockSuccessSingleResultWithSort,
13
- mockSuccessLoggedInResult,
14
- mockSuccessNoPreviewResult,
15
- mockSuccessLoggedInAndNoPreviewResult,
16
- mockSuccessWithYearHistogramAggs,
17
- mockSuccessMultiLineDescription,
18
- mockSuccessFirstTitleResult,
19
- mockSuccessFirstCreatorResult,
13
+ getMockSuccessLoggedInResult,
14
+ getMockSuccessNoPreviewResult,
15
+ getMockSuccessLoggedInAndNoPreviewResult,
16
+ getMockSuccessWithYearHistogramAggs,
17
+ getMockSuccessMultiLineDescription,
18
+ getMockSuccessFirstTitleResult,
19
+ getMockSuccessFirstCreatorResult,
20
+ getMockErrorResult,
21
+ getMockMalformedResult,
20
22
  } from './mock-search-responses';
21
23
 
24
+ const responses: Record<
25
+ string,
26
+ () => Result<SearchResponse, SearchServiceError>
27
+ > = {
28
+ 'single-result': getMockSuccessSingleResult,
29
+ years: getMockSuccessWithYearHistogramAggs,
30
+ 'multi-line-description': getMockSuccessMultiLineDescription,
31
+ loggedin: getMockSuccessLoggedInResult,
32
+ 'no-preview': getMockSuccessNoPreviewResult,
33
+ 'loggedin-no-preview': getMockSuccessLoggedInAndNoPreviewResult,
34
+ 'first-title': getMockSuccessFirstTitleResult,
35
+ 'first-creator': getMockSuccessFirstCreatorResult,
36
+ error: getMockErrorResult,
37
+ malformed: getMockMalformedResult,
38
+ };
39
+
22
40
  export class MockSearchService implements SearchServiceInterface {
23
41
  searchParams?: SearchParams;
24
42
 
@@ -26,10 +44,17 @@ export class MockSearchService implements SearchServiceInterface {
26
44
 
27
45
  asyncResponse: boolean;
28
46
 
47
+ asyncResponseDelay: number;
48
+
29
49
  resultsSpy: Function;
30
50
 
31
- constructor({ asyncResponse = false, resultsSpy = () => {} } = {}) {
51
+ constructor({
52
+ asyncResponse = false,
53
+ asyncResponseDelay = 0,
54
+ resultsSpy = () => {},
55
+ } = {}) {
32
56
  this.asyncResponse = asyncResponse;
57
+ this.asyncResponseDelay = asyncResponseDelay;
33
58
  this.resultsSpy = resultsSpy;
34
59
  }
35
60
 
@@ -43,31 +68,24 @@ export class MockSearchService implements SearchServiceInterface {
43
68
  if (this.asyncResponse) {
44
69
  // Add an artificial 1-tick delay
45
70
  await new Promise(res => {
46
- setTimeout(res, 0);
71
+ setTimeout(res, this.asyncResponseDelay);
47
72
  });
48
73
  }
49
74
 
50
- switch (this.searchParams?.query) {
51
- case 'single-result':
52
- return mockSuccessSingleResult;
53
- case 'years':
54
- return mockSuccessWithYearHistogramAggs;
55
- case 'multi-line-description':
56
- return mockSuccessMultiLineDescription;
57
- case 'loggedin':
58
- return mockSuccessLoggedInResult;
59
- case 'no-preview':
60
- return mockSuccessNoPreviewResult;
61
- case 'loggedin-no-preview':
62
- return mockSuccessLoggedInAndNoPreviewResult;
63
- case 'first-title':
64
- return mockSuccessFirstTitleResult;
65
- case 'first-creator':
66
- return mockSuccessFirstCreatorResult;
67
- case 'with-sort':
68
- return getMockSuccessSingleResultWithSort(this.resultsSpy);
69
- default:
70
- return mockSuccessMultipleResults;
75
+ const resultFn: () => Result<SearchResponse, SearchServiceError> =
76
+ responses[this.searchParams.query] ?? getMockSuccessMultipleResults;
77
+ let result = resultFn();
78
+
79
+ // with-sort query has special handling
80
+ if (this.searchParams.query === 'with-sort') {
81
+ result = getMockSuccessSingleResultWithSort(this.resultsSpy);
82
+ }
83
+
84
+ // Apply any uid param from the request
85
+ if (result.success) {
86
+ (result.success.request.clientParameters as any).uid = params.uid;
71
87
  }
88
+
89
+ return result;
72
90
  }
73
91
  }
@@ -15,24 +15,13 @@ describe('Restoration state handler', () => {
15
15
  expect(restorationState.baseQuery).to.equal('boop');
16
16
  });
17
17
 
18
- it('should not restore any search type from URL without valid sin', async () => {
18
+ it('should restore metadata search type from URL without valid sin', async () => {
19
19
  const handler = new RestorationStateHandler({ context: 'search' });
20
20
 
21
21
  const url = new URL(window.location.href);
22
22
  url.search = '?sin=foo';
23
23
  window.history.replaceState({ path: url.href }, '', url.href);
24
24
 
25
- const restorationState = handler.getRestorationState();
26
- expect(restorationState.searchType).to.not.exist;
27
- });
28
-
29
- it('should restore metadata 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
25
  const restorationState = handler.getRestorationState();
37
26
  expect(restorationState.searchType).to.equal(SearchType.METADATA);
38
27
  });