@daffodil/search 0.0.0-PLACEHOLDER

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 (146) hide show
  1. package/.eslintrc.js +52 -0
  2. package/README.md +11 -0
  3. package/driver/federated/README.md +3 -0
  4. package/driver/federated/ng-package.json +8 -0
  5. package/driver/federated/package.json +3 -0
  6. package/driver/federated/src/config/default.ts +6 -0
  7. package/driver/federated/src/config/interface.ts +21 -0
  8. package/driver/federated/src/config/public_api.ts +5 -0
  9. package/driver/federated/src/index.ts +1 -0
  10. package/driver/federated/src/injection-tokens/drivers.token.spec.ts +64 -0
  11. package/driver/federated/src/injection-tokens/drivers.token.ts +37 -0
  12. package/driver/federated/src/public_api.ts +4 -0
  13. package/driver/federated/src/search-driver.module.ts +62 -0
  14. package/driver/federated/src/search.service.spec.ts +86 -0
  15. package/driver/federated/src/search.service.ts +49 -0
  16. package/driver/in-memory/ng-package.json +8 -0
  17. package/driver/in-memory/package.json +3 -0
  18. package/driver/in-memory/src/backend/search.service.spec.ts +111 -0
  19. package/driver/in-memory/src/backend/search.service.ts +83 -0
  20. package/driver/in-memory/src/driver/search-driver.module.ts +33 -0
  21. package/driver/in-memory/src/driver/search.service.spec.ts +75 -0
  22. package/driver/in-memory/src/driver/search.service.ts +33 -0
  23. package/driver/in-memory/src/index.ts +1 -0
  24. package/driver/in-memory/src/injection-tokens/backends.token.spec.ts +69 -0
  25. package/driver/in-memory/src/injection-tokens/backends.token.ts +36 -0
  26. package/driver/in-memory/src/interfaces/get-backend.interface.ts +17 -0
  27. package/driver/in-memory/src/interfaces/public_api.ts +1 -0
  28. package/driver/in-memory/src/public_api.ts +7 -0
  29. package/driver/ng-package.json +8 -0
  30. package/driver/package.json +3 -0
  31. package/driver/src/errors/invalid-api-response.ts +17 -0
  32. package/driver/src/errors/map.ts +16 -0
  33. package/driver/src/errors/public_api.ts +9 -0
  34. package/driver/src/errors/query-too-short.ts +17 -0
  35. package/driver/src/index.ts +1 -0
  36. package/driver/src/interfaces/response.interface.ts +18 -0
  37. package/driver/src/interfaces/search-service-kinded.interface.ts +17 -0
  38. package/driver/src/interfaces/search-service.interface.ts +42 -0
  39. package/driver/src/public_api.ts +9 -0
  40. package/driver/testing/ng-package.json +8 -0
  41. package/driver/testing/package.json +3 -0
  42. package/driver/testing/src/drivers/public_api.ts +2 -0
  43. package/driver/testing/src/drivers/search-driver.module.ts +31 -0
  44. package/driver/testing/src/drivers/search.service.spec.ts +29 -0
  45. package/driver/testing/src/drivers/search.service.ts +37 -0
  46. package/driver/testing/src/index.ts +1 -0
  47. package/driver/testing/src/public_api.ts +1 -0
  48. package/karma.conf.js +11 -0
  49. package/ng-package.json +7 -0
  50. package/ng-package.prod.json +7 -0
  51. package/package.json +40 -0
  52. package/routing/ng-package.json +8 -0
  53. package/routing/package.json +3 -0
  54. package/routing/src/index.ts +1 -0
  55. package/routing/src/injection-tokens/public_api.ts +6 -0
  56. package/routing/src/injection-tokens/search-options/builder.token.ts +10 -0
  57. package/routing/src/injection-tokens/search-options/builders.token.spec.ts +35 -0
  58. package/routing/src/injection-tokens/search-options/builders.token.ts +42 -0
  59. package/routing/src/module.ts +26 -0
  60. package/routing/src/public_api.ts +3 -0
  61. package/routing/src/resolvers/public_api.ts +1 -0
  62. package/routing/src/resolvers/search/search.resolver.spec.ts +166 -0
  63. package/routing/src/resolvers/search/search.resolver.ts +65 -0
  64. package/src/index.ts +1 -0
  65. package/src/injection-tokens/error-matcher.token.ts +12 -0
  66. package/src/injection-tokens/public_api.ts +1 -0
  67. package/src/models/public_api.ts +2 -0
  68. package/src/models/search-result-collection.type.ts +6 -0
  69. package/src/models/search-result.type.ts +10 -0
  70. package/src/public_api.ts +3 -0
  71. package/src/transforms/public_api.ts +1 -0
  72. package/src/transforms/results-to-collection.spec.ts +28 -0
  73. package/src/transforms/results-to-collection.ts +14 -0
  74. package/state/ng-package.json +8 -0
  75. package/state/package.json +3 -0
  76. package/state/src/actions/search.actions.ts +93 -0
  77. package/state/src/config/default.ts +10 -0
  78. package/state/src/config/interface.ts +20 -0
  79. package/state/src/config/public_api.ts +3 -0
  80. package/state/src/config/token.ts +11 -0
  81. package/state/src/effects/incremental.effects.spec.ts +205 -0
  82. package/state/src/effects/incremental.effects.ts +74 -0
  83. package/state/src/effects/page.effects.spec.ts +179 -0
  84. package/state/src/effects/page.effects.ts +57 -0
  85. package/state/src/facades/public_api.ts +4 -0
  86. package/state/src/facades/search/base.facade.spec.ts +148 -0
  87. package/state/src/facades/search/base.facade.ts +53 -0
  88. package/state/src/facades/search/incremental.facade.ts +25 -0
  89. package/state/src/facades/search/page.facade.ts +25 -0
  90. package/state/src/facades/search/search-facade.interface.ts +38 -0
  91. package/state/src/incremental.module.ts +35 -0
  92. package/state/src/index.ts +1 -0
  93. package/state/src/page.module.ts +15 -0
  94. package/state/src/public_api.ts +9 -0
  95. package/state/src/reducers/incremental/public_api.ts +1 -0
  96. package/state/src/reducers/incremental/reducer.spec.ts +142 -0
  97. package/state/src/reducers/incremental/reducer.ts +34 -0
  98. package/state/src/reducers/page/public_api.ts +1 -0
  99. package/state/src/reducers/page/reducer.spec.ts +142 -0
  100. package/state/src/reducers/page/reducer.ts +34 -0
  101. package/state/src/reducers/public_api.ts +11 -0
  102. package/state/src/reducers/reducers.interface.ts +19 -0
  103. package/state/src/reducers/reducers.spec.ts +16 -0
  104. package/state/src/reducers/reducers.ts +13 -0
  105. package/state/src/reducers/search/adapter.spec.ts +130 -0
  106. package/state/src/reducers/search/adapter.ts +71 -0
  107. package/state/src/reducers/search/initial-state.ts +11 -0
  108. package/state/src/reducers/search/interface.ts +26 -0
  109. package/state/src/reducers/search/public_api.ts +3 -0
  110. package/state/src/reducers/store-feature-key.ts +4 -0
  111. package/state/src/reducers/token/extra.token.spec.ts +35 -0
  112. package/state/src/reducers/token/extra.token.ts +44 -0
  113. package/state/src/reducers/token/public_api.ts +4 -0
  114. package/state/src/reducers/token/reducers.token.spec.ts +51 -0
  115. package/state/src/reducers/token/reducers.token.ts +33 -0
  116. package/state/src/search.module.ts +15 -0
  117. package/state/src/selectors/incremental/selectors.ts +21 -0
  118. package/state/src/selectors/page/selectors.ts +21 -0
  119. package/state/src/selectors/public_api.ts +4 -0
  120. package/state/src/selectors/search-feature.selector.ts +27 -0
  121. package/state/src/selectors/search.selector.spec.ts +137 -0
  122. package/state/src/selectors/search.selector.ts +78 -0
  123. package/state/testing/ng-package.json +8 -0
  124. package/state/testing/package.json +3 -0
  125. package/state/testing/src/index.ts +1 -0
  126. package/state/testing/src/mock-search-facade.ts +24 -0
  127. package/state/testing/src/public_api.ts +2 -0
  128. package/state/testing/src/search-testing.module.ts +19 -0
  129. package/test.ts +23 -0
  130. package/testing/ng-package.json +8 -0
  131. package/testing/package.json +3 -0
  132. package/testing/src/factories/kind.factory.spec.ts +57 -0
  133. package/testing/src/factories/kind.factory.ts +37 -0
  134. package/testing/src/factories/public_api.ts +2 -0
  135. package/testing/src/factories/search-result.factory.spec.ts +39 -0
  136. package/testing/src/factories/search-result.factory.ts +26 -0
  137. package/testing/src/index.ts +1 -0
  138. package/testing/src/injection-tokens/factories/kind.token.spec.ts +51 -0
  139. package/testing/src/injection-tokens/factories/kind.token.ts +43 -0
  140. package/testing/src/injection-tokens/public_api.ts +4 -0
  141. package/testing/src/public_api.ts +4 -0
  142. package/testing/src/testing.module.ts +18 -0
  143. package/tsconfig.json +38 -0
  144. package/tsconfig.lib.json +28 -0
  145. package/tsconfig.lib.prod.json +9 -0
  146. package/tsconfig.spec.json +17 -0
@@ -0,0 +1,53 @@
1
+ import {
2
+ Action,
3
+ Store,
4
+ select,
5
+ } from '@ngrx/store';
6
+ import { Observable } from 'rxjs';
7
+
8
+ import { DaffStateError } from '@daffodil/core/state';
9
+ import { DaffSearchResult } from '@daffodil/search';
10
+
11
+ import { DaffSearchStateRootSlice } from '../../reducers/public_api';
12
+ import { DaffSearchSelectors } from '../../selectors/public_api';
13
+ import { DaffSearchFacadeInterface } from './search-facade.interface';
14
+
15
+ /**
16
+ * @inheritdoc
17
+ *
18
+ * Serves as the base for a search facade.
19
+ * Extend this class and pass in the selectors for the specific state you wish to target.
20
+ */
21
+ export abstract class DaffSearchFacadeBase<T extends DaffSearchResult = DaffSearchResult, R extends DaffSearchStateRootSlice<T> = DaffSearchStateRootSlice<T>> implements DaffSearchFacadeInterface<T> {
22
+ loading$: Observable<boolean>;
23
+ errors$: Observable<DaffStateError[]>;
24
+
25
+ searchResultIds$: Observable<Record<T['kind'], T['id'][]>>;
26
+ recent$: Observable<string[]>;
27
+ resultCount$: Observable<number>;
28
+
29
+ constructor(
30
+ private store: Store<R>,
31
+ selectors: DaffSearchSelectors<T, R>,
32
+ ) {
33
+ const {
34
+ selectSearchLoading,
35
+ selectSearchErrors,
36
+
37
+ selectSearchResultIds,
38
+ selectRecent,
39
+ selectResultCount,
40
+ } = selectors;
41
+
42
+ this.loading$ = this.store.pipe(select(selectSearchLoading));
43
+ this.errors$ = this.store.pipe(select(selectSearchErrors));
44
+
45
+ this.searchResultIds$ = this.store.pipe(select(selectSearchResultIds));
46
+ this.recent$ = this.store.pipe(select(selectRecent));
47
+ this.resultCount$ = this.store.pipe(select(selectResultCount));
48
+ }
49
+
50
+ dispatch(action: Action) {
51
+ this.store.dispatch(action);
52
+ }
53
+ }
@@ -0,0 +1,25 @@
1
+ import { Injectable } from '@angular/core';
2
+ import { Store } from '@ngrx/store';
3
+
4
+ import { DaffSearchResult } from '@daffodil/search';
5
+
6
+ import { DaffSearchStateRootSlice } from '../../reducers/public_api';
7
+ import { daffSearchGetIncrementalSelectors } from '../../selectors/public_api';
8
+ import { DaffSearchFacadeBase } from './base.facade';
9
+ import { DaffSearchFacadeInterface } from './search-facade.interface';
10
+
11
+ /**
12
+ * @inheritdoc
13
+ *
14
+ * A facade for the incremental search state.
15
+ */
16
+ @Injectable({
17
+ providedIn: 'root',
18
+ })
19
+ export class DaffSearchIncrementalFacade<T extends DaffSearchResult = DaffSearchResult, R extends DaffSearchStateRootSlice<T> = DaffSearchStateRootSlice<T>> extends DaffSearchFacadeBase<T, R> implements DaffSearchFacadeInterface<T> {
20
+ constructor(
21
+ store: Store<R>,
22
+ ) {
23
+ super(store, daffSearchGetIncrementalSelectors());
24
+ }
25
+ }
@@ -0,0 +1,25 @@
1
+ import { Injectable } from '@angular/core';
2
+ import { Store } from '@ngrx/store';
3
+
4
+ import { DaffSearchResult } from '@daffodil/search';
5
+
6
+ import { DaffSearchStateRootSlice } from '../../reducers/public_api';
7
+ import { daffSearchGetPageSelectors } from '../../selectors/public_api';
8
+ import { DaffSearchFacadeBase } from './base.facade';
9
+ import { DaffSearchFacadeInterface } from './search-facade.interface';
10
+
11
+ /**
12
+ * @inheritdoc
13
+ *
14
+ * A facade for the search page state.
15
+ */
16
+ @Injectable({
17
+ providedIn: 'root',
18
+ })
19
+ export class DaffSearchPageFacade<T extends DaffSearchResult = DaffSearchResult, R extends DaffSearchStateRootSlice<T> = DaffSearchStateRootSlice<T>> extends DaffSearchFacadeBase<T, R> implements DaffSearchFacadeInterface<T> {
20
+ constructor(
21
+ store: Store<R>,
22
+ ) {
23
+ super(store, daffSearchGetPageSelectors());
24
+ }
25
+ }
@@ -0,0 +1,38 @@
1
+ import { Dictionary } from '@ngrx/entity';
2
+ import { Action } from '@ngrx/store';
3
+ import { Observable } from 'rxjs';
4
+
5
+ import {
6
+ DaffStoreFacade,
7
+ DaffStateError,
8
+ } from '@daffodil/core/state';
9
+ import {
10
+ DaffSearchResult,
11
+ DaffSearchResultCollection,
12
+ } from '@daffodil/search';
13
+
14
+ /**
15
+ * Exposes the search state selectors.
16
+ */
17
+ export interface DaffSearchFacadeInterface<T extends DaffSearchResult = DaffSearchResult> extends DaffStoreFacade<Action> {
18
+ /**
19
+ * Whether there is a pending search operation.
20
+ */
21
+ loading$: Observable<boolean>;
22
+ /**
23
+ * A list of search errors, if any.
24
+ */
25
+ errors$: Observable<DaffStateError[]>;
26
+ /**
27
+ * The recent search queries.
28
+ */
29
+ recent$: Observable<string[]>;
30
+ /**
31
+ * The result IDs of the most recent search.
32
+ */
33
+ searchResultIds$: Observable<Record<T['kind'], T['id'][]>>;
34
+ /**
35
+ * The number of results in the most recent search.
36
+ */
37
+ resultCount$: Observable<number>;
38
+ }
@@ -0,0 +1,35 @@
1
+ import {
2
+ ModuleWithProviders,
3
+ NgModule,
4
+ } from '@angular/core';
5
+ import { EffectsModule } from '@ngrx/effects';
6
+
7
+ import {
8
+ DaffSearchStateConfig,
9
+ DAFF_SEARCH_STATE_CONFIG,
10
+ DAFF_SEARCH_STATE_CONFIG_DEFAULT,
11
+ } from './config/public_api';
12
+ import { DaffSearchIncrementalEffects } from './effects/incremental.effects';
13
+ import { DaffSearchStateModule } from './search.module';
14
+
15
+ @NgModule({
16
+ imports: [
17
+ EffectsModule.forFeature([
18
+ DaffSearchIncrementalEffects,
19
+ ]),
20
+ DaffSearchStateModule,
21
+ ],
22
+ })
23
+ export class DaffSearchIncrementalStateModule {
24
+ static withConfig(config: DaffSearchStateConfig = DAFF_SEARCH_STATE_CONFIG_DEFAULT): ModuleWithProviders<DaffSearchIncrementalStateModule> {
25
+ return {
26
+ ngModule: DaffSearchIncrementalStateModule,
27
+ providers: [
28
+ {
29
+ provide: DAFF_SEARCH_STATE_CONFIG,
30
+ useValue: config,
31
+ },
32
+ ],
33
+ };
34
+ }
35
+ }
@@ -0,0 +1 @@
1
+ export * from './public_api';
@@ -0,0 +1,15 @@
1
+ import { NgModule } from '@angular/core';
2
+ import { EffectsModule } from '@ngrx/effects';
3
+
4
+ import { DaffSearchPageEffects } from './effects/page.effects';
5
+ import { DaffSearchStateModule } from './search.module';
6
+
7
+ @NgModule({
8
+ imports: [
9
+ EffectsModule.forFeature([
10
+ DaffSearchPageEffects,
11
+ ]),
12
+ DaffSearchStateModule,
13
+ ],
14
+ })
15
+ export class DaffSearchPageStateModule {}
@@ -0,0 +1,9 @@
1
+ export * from './actions/search.actions';
2
+ export * from './reducers/public_api';
3
+ export * from './selectors/public_api';
4
+ export * from './facades/public_api';
5
+ export * from './config/public_api';
6
+
7
+ export * from './search.module';
8
+ export * from './incremental.module';
9
+ export * from './page.module';
@@ -0,0 +1 @@
1
+ export { daffSearchIncrementalReducer } from './reducer';
@@ -0,0 +1,142 @@
1
+ import { DaffStateError } from '@daffodil/core/state';
2
+ import {
3
+ DaffSearchResult,
4
+ daffSearchTransformResultsToCollection,
5
+ } from '@daffodil/search';
6
+ import {
7
+ DaffSearchIncremental,
8
+ DaffSearchIncrementalSuccess,
9
+ DaffSearchIncrementalFailure,
10
+ daffSearchInitialState as initialState,
11
+ DaffSearchReducerState,
12
+ } from '@daffodil/search/state';
13
+ import { DaffSearchResultFactory } from '@daffodil/search/testing';
14
+
15
+ import { daffSearchIncrementalReducer as reducer } from './reducer';
16
+
17
+ describe('@daffodil/search/state | daffSearchIncrementalReducer', () => {
18
+ let searchResultFactory: DaffSearchResultFactory;
19
+
20
+ beforeEach(() => {
21
+ searchResultFactory = new DaffSearchResultFactory();
22
+ });
23
+
24
+ describe('when an unknown action is triggered', () => {
25
+ it('should return the current state', () => {
26
+ const action = <any>{};
27
+
28
+ const result = reducer(initialState, action);
29
+
30
+ expect(result).toBe(initialState);
31
+ });
32
+ });
33
+
34
+ describe('when SearchIncrementalAction is triggered', () => {
35
+ let result: DaffSearchReducerState;
36
+ let query: string;
37
+
38
+ beforeEach(() => {
39
+ query = 'query';
40
+ const searchResultIncrementalAction = new DaffSearchIncremental(query);
41
+
42
+ result = reducer({
43
+ ...initialState,
44
+ recent: [
45
+ 'some other recent search query',
46
+ ],
47
+ }, searchResultIncrementalAction);
48
+ });
49
+
50
+ it('should add the query to the list of recent searches', () => {
51
+ expect(result.recent).toContain(query);
52
+ });
53
+
54
+ it('sets loading state to true', () => {
55
+ expect(result.loading).toEqual(true);
56
+ });
57
+ });
58
+
59
+ describe('when SearchIncrementalSuccessAction is triggered', () => {
60
+ let mockError: DaffStateError;
61
+ let mockSearchResult: DaffSearchResult;
62
+ let result: DaffSearchReducerState;
63
+ let state: DaffSearchReducerState;
64
+
65
+ beforeEach(() => {
66
+ mockError = {
67
+ code: 'error code',
68
+ message: 'error message',
69
+ };
70
+ state = {
71
+ ...initialState,
72
+ loading: true,
73
+ errors: [mockError],
74
+ results: {
75
+ resultKind: ['testId1'],
76
+ },
77
+ };
78
+ mockSearchResult = searchResultFactory.create({
79
+ kind: 'resultKind',
80
+ });
81
+
82
+ const searchResultIncrementalSuccess = new DaffSearchIncrementalSuccess({
83
+ collection: daffSearchTransformResultsToCollection([mockSearchResult]),
84
+ metadata: {},
85
+ });
86
+
87
+ result = reducer(state, searchResultIncrementalSuccess);
88
+ });
89
+
90
+ it('sets loading to false', () => {
91
+ expect(result.loading).toEqual(false);
92
+ });
93
+
94
+ it('should reset errors', () => {
95
+ expect(result.errors).toEqual([]);
96
+ });
97
+
98
+ it('should set the search result IDs to the results dict', () => {
99
+ expect(result.results.resultKind).toEqual([mockSearchResult.id]);
100
+ });
101
+ });
102
+
103
+ describe('when SearchIncrementalFailureAction is triggered', () => {
104
+ let result;
105
+ let state: DaffSearchReducerState;
106
+ let mockError: DaffStateError;
107
+
108
+ beforeEach(() => {
109
+ mockError = {
110
+ code: 'error code',
111
+ message: 'error message',
112
+ };
113
+ state = {
114
+ ...initialState,
115
+ loading: true,
116
+ errors: [
117
+ { code: 'firstErrorCode', message: 'firstErrorMessage' },
118
+ ],
119
+ results: {
120
+ type: ['result'],
121
+ },
122
+ };
123
+
124
+ const searchResultIncrementalFailureAction = new DaffSearchIncrementalFailure(mockError);
125
+
126
+ result = reducer(state, searchResultIncrementalFailureAction);
127
+ });
128
+
129
+ it('adds the error in action.payload to state.errors', () => {
130
+ expect(result.errors).toContain(mockError);
131
+ expect(result.errors.length).toEqual(2);
132
+ });
133
+
134
+ it('sets loading to false', () => {
135
+ expect(result.loading).toEqual(false);
136
+ });
137
+
138
+ it('clears search results', () => {
139
+ expect(result.results).toEqual({});
140
+ });
141
+ });
142
+ });
@@ -0,0 +1,34 @@
1
+ import { DaffSearchResult } from '@daffodil/search';
2
+
3
+ import {
4
+ DaffSearchActions,
5
+ DaffSearchActionTypes,
6
+ } from '../../actions/search.actions';
7
+ import {
8
+ daffSearchInitialState,
9
+ DaffSearchReducerState,
10
+ DaffSearchStateReducerAdapter,
11
+ } from '../search/public_api';
12
+
13
+ /**
14
+ * The reducer for the incremental part of search state, see {@link DaffSearchReducerState}.
15
+ */
16
+ export function daffSearchIncrementalReducer<T extends DaffSearchResult = DaffSearchResult>(
17
+ state = daffSearchInitialState,
18
+ action: DaffSearchActions<T>,
19
+ ): DaffSearchReducerState {
20
+ const adapter = new DaffSearchStateReducerAdapter(state);
21
+ switch (action.type) {
22
+ case DaffSearchActionTypes.SearchIncrementalAction:
23
+ return adapter.search(action.query);
24
+
25
+ case DaffSearchActionTypes.SearchIncrementalSuccessAction:
26
+ return adapter.storeResults(action.payload.collection);
27
+
28
+ case DaffSearchActionTypes.SearchIncrementalFailureAction:
29
+ return adapter.storeError(action.payload);
30
+
31
+ default:
32
+ return state;
33
+ }
34
+ }
@@ -0,0 +1 @@
1
+ export { daffSearchPageReducer } from './reducer';
@@ -0,0 +1,142 @@
1
+ import { DaffStateError } from '@daffodil/core/state';
2
+ import {
3
+ DaffSearchResult,
4
+ daffSearchTransformResultsToCollection,
5
+ } from '@daffodil/search';
6
+ import {
7
+ DaffSearchLoad,
8
+ DaffSearchLoadSuccess,
9
+ DaffSearchLoadFailure,
10
+ daffSearchInitialState as initialState,
11
+ DaffSearchReducerState,
12
+ } from '@daffodil/search/state';
13
+ import { DaffSearchResultFactory } from '@daffodil/search/testing';
14
+
15
+ import { daffSearchPageReducer as reducer } from './reducer';
16
+
17
+ describe('@daffodil/search/state | daffSearchPageReducer', () => {
18
+ let searchResultFactory: DaffSearchResultFactory;
19
+
20
+ beforeEach(() => {
21
+ searchResultFactory = new DaffSearchResultFactory();
22
+ });
23
+
24
+ describe('when an unknown action is triggered', () => {
25
+ it('should return the current state', () => {
26
+ const action = <any>{};
27
+
28
+ const result = reducer(initialState, action);
29
+
30
+ expect(result).toBe(initialState);
31
+ });
32
+ });
33
+
34
+ describe('when SearchLoadAction is triggered', () => {
35
+ let result: DaffSearchReducerState;
36
+ let query: string;
37
+
38
+ beforeEach(() => {
39
+ query = 'query';
40
+ const searchResultLoadAction = new DaffSearchLoad(query);
41
+
42
+ result = reducer({
43
+ ...initialState,
44
+ recent: [
45
+ 'some other recent search query',
46
+ ],
47
+ }, searchResultLoadAction);
48
+ });
49
+
50
+ it('should add the query to the list of recent searches', () => {
51
+ expect(result.recent).toContain(query);
52
+ });
53
+
54
+ it('sets loading state to true', () => {
55
+ expect(result.loading).toEqual(true);
56
+ });
57
+ });
58
+
59
+ describe('when SearchLoadSuccessAction is triggered', () => {
60
+ let mockError: DaffStateError;
61
+ let mockSearchResult: DaffSearchResult;
62
+ let result: DaffSearchReducerState;
63
+ let state: DaffSearchReducerState;
64
+
65
+ beforeEach(() => {
66
+ mockError = {
67
+ code: 'error code',
68
+ message: 'error message',
69
+ };
70
+ state = {
71
+ ...initialState,
72
+ loading: true,
73
+ errors: [mockError],
74
+ results: {
75
+ resultKind: ['testId1'],
76
+ },
77
+ };
78
+ mockSearchResult = searchResultFactory.create({
79
+ kind: 'resultKind',
80
+ });
81
+
82
+ const searchResultLoadSuccess = new DaffSearchLoadSuccess({
83
+ collection: daffSearchTransformResultsToCollection([mockSearchResult]),
84
+ metadata: {},
85
+ });
86
+
87
+ result = reducer(state, searchResultLoadSuccess);
88
+ });
89
+
90
+ it('sets loading to false', () => {
91
+ expect(result.loading).toEqual(false);
92
+ });
93
+
94
+ it('should reset errors', () => {
95
+ expect(result.errors).toEqual([]);
96
+ });
97
+
98
+ it('should set the search result IDs to the results dict', () => {
99
+ expect(result.results.resultKind).toEqual([mockSearchResult.id]);
100
+ });
101
+ });
102
+
103
+ describe('when SearchLoadFailureAction is triggered', () => {
104
+ let result;
105
+ let state: DaffSearchReducerState;
106
+ let mockError: DaffStateError;
107
+
108
+ beforeEach(() => {
109
+ mockError = {
110
+ code: 'error code',
111
+ message: 'error message',
112
+ };
113
+ state = {
114
+ ...initialState,
115
+ loading: true,
116
+ errors: [
117
+ { code: 'firstErrorCode', message: 'firstErrorMessage' },
118
+ ],
119
+ results: {
120
+ type: ['result'],
121
+ },
122
+ };
123
+
124
+ const searchResultLoadFailureAction = new DaffSearchLoadFailure(mockError);
125
+
126
+ result = reducer(state, searchResultLoadFailureAction);
127
+ });
128
+
129
+ it('adds the error in action.payload to state.errors', () => {
130
+ expect(result.errors).toContain(mockError);
131
+ expect(result.errors.length).toEqual(2);
132
+ });
133
+
134
+ it('sets loading to false', () => {
135
+ expect(result.loading).toEqual(false);
136
+ });
137
+
138
+ it('clears search results', () => {
139
+ expect(result.results).toEqual({});
140
+ });
141
+ });
142
+ });
@@ -0,0 +1,34 @@
1
+ import { DaffSearchResult } from '@daffodil/search';
2
+
3
+ import {
4
+ DaffSearchActions,
5
+ DaffSearchActionTypes,
6
+ } from '../../actions/search.actions';
7
+ import {
8
+ daffSearchInitialState,
9
+ DaffSearchReducerState,
10
+ DaffSearchStateReducerAdapter,
11
+ } from '../search/public_api';
12
+
13
+ /**
14
+ * The reducer for the search page state, see {@link DaffSearchReducerState}.
15
+ */
16
+ export function daffSearchPageReducer<T extends DaffSearchResult = DaffSearchResult>(
17
+ state = daffSearchInitialState,
18
+ action: DaffSearchActions<T>,
19
+ ): DaffSearchReducerState {
20
+ const adapter = new DaffSearchStateReducerAdapter(state);
21
+ switch (action.type) {
22
+ case DaffSearchActionTypes.SearchLoadAction:
23
+ return adapter.search(action.query);
24
+
25
+ case DaffSearchActionTypes.SearchLoadSuccessAction:
26
+ return adapter.storeResults(action.payload.collection);
27
+
28
+ case DaffSearchActionTypes.SearchLoadFailureAction:
29
+ return adapter.storeError(action.payload);
30
+
31
+ default:
32
+ return state;
33
+ }
34
+ }
@@ -0,0 +1,11 @@
1
+ export { daffSearchReducers } from './reducers';
2
+ export {
3
+ DaffSearchReducersState,
4
+ DaffSearchStateRootSlice,
5
+ } from './reducers.interface';
6
+ export { DAFF_SEARCH_STORE_FEATURE_KEY } from './store-feature-key';
7
+
8
+ export * from './page/public_api';
9
+ export * from './incremental/public_api';
10
+ export * from './token/public_api';
11
+ export * from './search/public_api';
@@ -0,0 +1,19 @@
1
+ import { DaffSearchResult } from '@daffodil/search';
2
+
3
+ import { DaffSearchReducerState } from './search/public_api';
4
+ import { DAFF_SEARCH_STORE_FEATURE_KEY } from './store-feature-key';
5
+
6
+ /**
7
+ * The feature state for search.
8
+ */
9
+ export interface DaffSearchReducersState<T extends DaffSearchResult = DaffSearchResult> {
10
+ search: DaffSearchReducerState<T>;
11
+ incremental: DaffSearchReducerState;
12
+ }
13
+
14
+ /**
15
+ * The footprint of search feature state in the root application state.
16
+ */
17
+ export interface DaffSearchStateRootSlice<T extends DaffSearchResult = DaffSearchResult> {
18
+ [DAFF_SEARCH_STORE_FEATURE_KEY]: DaffSearchReducersState<T>;
19
+ }
@@ -0,0 +1,16 @@
1
+ import {
2
+ daffSearchPageReducer,
3
+ daffSearchIncrementalReducer,
4
+ } from '@daffodil/search/state';
5
+
6
+ import { daffSearchReducers } from './reducers';
7
+
8
+ describe('@daffodil/search/state | daffSearchReducers', () => {
9
+ it('should return a reducer map with daffSearchIncrementalReducer', () => {
10
+ expect(daffSearchReducers.incremental).toEqual(daffSearchIncrementalReducer);
11
+ });
12
+
13
+ it('should return a reducer map with daffSearchPageReducer', () => {
14
+ expect(daffSearchReducers.search).toEqual(daffSearchPageReducer);
15
+ });
16
+ });
@@ -0,0 +1,13 @@
1
+ import { ActionReducerMap } from '@ngrx/store';
2
+
3
+ import { daffSearchIncrementalReducer } from './incremental/public_api';
4
+ import { daffSearchPageReducer } from './page/public_api';
5
+ import { DaffSearchReducersState } from './reducers.interface';
6
+
7
+ /**
8
+ * The reducers for the entire search feature state.
9
+ */
10
+ export const daffSearchReducers: ActionReducerMap<DaffSearchReducersState> = {
11
+ search: daffSearchPageReducer,
12
+ incremental: daffSearchIncrementalReducer,
13
+ };