@sourceloop/search-client 9.0.1 → 11.0.0

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.
@@ -0,0 +1,306 @@
1
+ import {ComponentFixture, TestBed} from '@angular/core/testing';
2
+ import {SearchComponent} from './search.component';
3
+ import {IReturnType, SEARCH_SERVICE_TOKEN} from '../types';
4
+ import {
5
+ emittedValue,
6
+ IReturnMockValue,
7
+ ISearchMockValue,
8
+ mockCategory,
9
+ mockConfig,
10
+ mockEvent,
11
+ mockFunction,
12
+ mockIModel,
13
+ mockNoEvent,
14
+ mockOnChange,
15
+ MockSearchService,
16
+ mockSuggestion,
17
+ mockSuggestionUrl,
18
+ recentSearch,
19
+ url,
20
+ } from './mocks/search.component.mock';
21
+ import {of, throwError} from 'rxjs';
22
+ import {By} from '@angular/platform-browser';
23
+ import {SimpleChanges} from '@angular/core';
24
+ describe('SearchComponent', () => {
25
+ let component: SearchComponent<IReturnType>;
26
+ let fixture: ComponentFixture<SearchComponent<IReturnType>>;
27
+ const mockServiceSpy = jasmine.createSpyObj(MockSearchService, [
28
+ 'recentSearchApiRequest',
29
+ 'searchApiRequest',
30
+ ]);
31
+ beforeEach(() => {
32
+ TestBed.configureTestingModule({
33
+ imports: [SearchComponent],
34
+ providers: [{provide: SEARCH_SERVICE_TOKEN, useValue: mockServiceSpy}],
35
+ });
36
+
37
+ fixture = TestBed.createComponent(SearchComponent);
38
+ component = fixture.componentInstance;
39
+ fixture.componentRef.setInput('config', {
40
+ ...mockConfig,
41
+ models: [...mockConfig.models],
42
+ });
43
+ });
44
+ it('Should create component', () => {
45
+ expect(fixture).toBeDefined();
46
+ expect(component).toBeTruthy();
47
+ });
48
+ it('Should set the config', () => {
49
+ fixture.componentRef.setInput('config', mockConfig);
50
+ expect(component.config()).toEqual(mockConfig);
51
+ });
52
+ it('Should subscribe searchRequest$ and emit a response', () => {
53
+ spyOn(component.searched, 'emit');
54
+ spyOn(component, 'getSuggestions').and.callThrough();
55
+ const searchSpy = spyOn(component.searchRequest$, 'subscribe');
56
+ component.ngOnInit();
57
+ component.searchRequest$.subscribe(res => {
58
+ expect(res).toEqual(emittedValue);
59
+ expect(component.getSuggestions).toHaveBeenCalledWith(emittedValue);
60
+ });
61
+ expect(searchSpy).toHaveBeenCalled();
62
+ });
63
+ it('Should implement ControlValueAccessor write value method', () => {
64
+ component.writeValue('User');
65
+ expect(component.searchBoxInput).toEqual('User');
66
+ });
67
+ it('Should implement ControlValueAccessor register on touched method', () => {
68
+ component.registerOnTouched(mockFunction);
69
+ expect(component.onTouched).toEqual(mockFunction);
70
+ });
71
+ it('Should implement ControlValueAccessor register on change method', () => {
72
+ component.registerOnChange(mockOnChange);
73
+ expect(component.onChange).toEqual(mockOnChange);
74
+ });
75
+ it('Should not get suggestions if no input in search box input', () => {
76
+ mockServiceSpy.searchApiRequest.and.callFake(() => of(IReturnMockValue));
77
+ spyOn(component, 'getSuggestions');
78
+ component.getSuggestions(mockNoEvent);
79
+ expect(component.getSuggestions(mockNoEvent)).not.toBeDefined();
80
+ });
81
+ it('Should get suggestions on some search input', () => {
82
+ mockServiceSpy.searchApiRequest.and.callFake(() => of(IReturnMockValue));
83
+ component.getSuggestions(mockEvent);
84
+ expect(component.suggestions).toEqual(IReturnMockValue);
85
+ expect(component.searching).toBeFalse();
86
+ });
87
+ it('Should not give suggestions for some input in case of an error', () => {
88
+ mockServiceSpy.searchApiRequest.and.callFake(() => throwError(Error));
89
+ component.getSuggestions(mockEvent);
90
+ expect(component.suggestions).toEqual([]);
91
+ expect(component.searching).toBeFalse();
92
+ });
93
+ it('Should give recent searches for the input value being searched ', () => {
94
+ mockServiceSpy.recentSearchApiRequest.and.callFake(() =>
95
+ of(ISearchMockValue),
96
+ );
97
+ component.getRecentSearches();
98
+ expect(component.recentSearches).toEqual(ISearchMockValue);
99
+ });
100
+ it('Should give no search inputs in case of an error ', () => {
101
+ mockServiceSpy.recentSearchApiRequest.and.callFake(() => throwError(Error));
102
+ component.getRecentSearches();
103
+ expect(component.recentSearches).toEqual([]);
104
+ });
105
+ it('Should hit search api method ', () => {
106
+ const searchSpy = spyOn(component.searchRequest$, 'subscribe');
107
+ mockServiceSpy.recentSearchApiRequest.and.callFake(() =>
108
+ of(ISearchMockValue),
109
+ );
110
+ component.hitSearchApi();
111
+ component.searchRequest$.subscribe(res => {
112
+ expect(res).toEqual(emittedValue);
113
+ });
114
+ expect(searchSpy).toHaveBeenCalled();
115
+ });
116
+ it('Should call the focus search input element', () => {
117
+ component.searchInputElement = {
118
+ nativeElement: jasmine.createSpyObj('nativeElement', ['focus']),
119
+ };
120
+ component.focusInput();
121
+ expect(component.searchInputElement.nativeElement.focus).toHaveBeenCalled();
122
+ });
123
+ it('should return bold string', () => {
124
+ const inputString = 'User';
125
+ const result = component.boldString('User', 'Input');
126
+ expect(result).toEqual(inputString);
127
+ });
128
+ it('Should hide the suggestions', () => {
129
+ component.onTouched = jasmine.createSpy();
130
+ component.hideSuggestions();
131
+ expect(component.suggestionsDisplay).toEqual(false);
132
+ expect(component.onTouched).toHaveBeenCalled();
133
+ });
134
+ it('Should show the suggestions', () => {
135
+ const spyObj = spyOn(component, 'getRecentSearches');
136
+ component.showSuggestions();
137
+ expect(component.suggestionsDisplay).toEqual(true);
138
+ expect(spyObj).toHaveBeenCalledTimes(1);
139
+ });
140
+ it('Should show category', () => {
141
+ component.showCategory();
142
+ expect(component.categoryDisplay).toEqual(true);
143
+ });
144
+ it('Should hide category', () => {
145
+ component.hideCategory();
146
+ expect(component.categoryDisplay).toEqual(false);
147
+ });
148
+
149
+ it('Should return categoryToSourceName', () => {
150
+ const result = component._categoryToSourceName('User');
151
+ expect(result).toEqual(mockCategory);
152
+ });
153
+ it('Should set the category', () => {
154
+ const obj = spyOn(component, 'setCategory');
155
+ spyOn(component, 'getSuggestions').and.callThrough();
156
+ component.setCategory('All');
157
+ expect(obj).toHaveBeenCalledWith('All');
158
+ expect(component.category).toEqual('All');
159
+ expect(component.categoryDisplay).toEqual(false);
160
+ });
161
+ it('Should set the category in case of input in search box', () => {
162
+ component.searchBoxInput = 'User';
163
+ spyOn(component, 'hitSearchApi').and.callThrough();
164
+ spyOn(component, 'getSuggestions');
165
+ mockServiceSpy.recentSearchApiRequest.and.callFake(() =>
166
+ of(ISearchMockValue),
167
+ );
168
+ spyOn(component, 'focusInput').and.stub();
169
+ spyOn(component, 'showSuggestions').and.callThrough();
170
+ component.setCategory('All');
171
+ expect(component.hitSearchApi).toHaveBeenCalledTimes(1);
172
+ expect(component.showSuggestions).toHaveBeenCalledTimes(1);
173
+ });
174
+ it('Should reset the input', () => {
175
+ spyOn(component, 'focusInput').and.stub();
176
+ component.onChange = jasmine.createSpy();
177
+ spyOn(component, 'getRecentSearches').and.callFake(() => of([]));
178
+ mockServiceSpy.recentSearchApiRequest.and.callFake(() => of([]));
179
+ component.resetInput();
180
+ expect(component.searchBoxInput).toEqual('');
181
+ expect(component.suggestions).toEqual([]);
182
+ expect(component.suggestionsDisplay).toEqual(true);
183
+ expect(component.focusInput).toHaveBeenCalledTimes(1);
184
+ expect(component.onChange).toHaveBeenCalledTimes(1);
185
+ expect(component.getRecentSearches).toHaveBeenCalled();
186
+ });
187
+ it('Should unsubscribe searchRequest$ on calling ng on destroy', () => {
188
+ fixture.detectChanges();
189
+ let searchRequestSpy = spyOn(component.searchRequest$, 'unsubscribe');
190
+ component.ngOnDestroy();
191
+ expect(searchRequestSpy).toHaveBeenCalled();
192
+ });
193
+ it('Should get model from model name', () => {
194
+ const model = component.getModelFromModelName('User');
195
+ expect(model).toEqual(mockIModel);
196
+ });
197
+ it('Should fetch Model ImageUrl From Suggestion', () => {
198
+ const returnUrl =
199
+ component.fetchModelImageUrlFromSuggestion(mockSuggestionUrl);
200
+ expect(returnUrl).toEqual(url);
201
+ });
202
+ it('Should populate searched input value from the user', () => {
203
+ spyOn(component, 'getSuggestions');
204
+ spyOn(component.clicked, 'emit');
205
+ component.onChange = jasmine.createSpy();
206
+ component.populateValue(mockSuggestion, new MouseEvent('click'));
207
+ expect(component.searchBoxInput).toEqual('User');
208
+ expect(component.suggestionsDisplay).toEqual(false);
209
+ expect(component.onChange).toHaveBeenCalled();
210
+ expect(component.getSuggestions).toHaveBeenCalled();
211
+ expect(component.clicked.emit).toHaveBeenCalled();
212
+ });
213
+ it('Should populate value searched recently', () => {
214
+ spyOn(component, 'getSuggestions').and.callThrough();
215
+ mockServiceSpy.searchApiRequest.and.callFake(() => of(IReturnMockValue));
216
+ spyOn(component, 'showSuggestions').and.callThrough();
217
+ mockServiceSpy.recentSearchApiRequest.and.callFake(() => of([]));
218
+ component.onChange = jasmine.createSpy();
219
+ spyOn(component, 'focusInput').and.stub();
220
+ component.populateValueRecentSearch(recentSearch, new MouseEvent('click'));
221
+ expect(component.searchBoxInput).toEqual('user');
222
+ expect(component.getSuggestions).toHaveBeenCalled();
223
+ expect(component.onChange).toHaveBeenCalledOnceWith(recentSearch['match']);
224
+ expect(component.focusInput).toHaveBeenCalled();
225
+ expect(component.showSuggestions).toHaveBeenCalled();
226
+ });
227
+ it('Should fetch Model ImageUrl From Suggestion', () => {
228
+ const urlValue = undefined;
229
+ const returnUrl =
230
+ component.fetchModelImageUrlFromSuggestion(mockSuggestion);
231
+ expect(returnUrl).toEqual(urlValue);
232
+ });
233
+ it('Should get Models With Suggestions', () => {
234
+ const result = component.getModelsWithSuggestions();
235
+ expect(result).toEqual([]);
236
+ });
237
+ it('Should not show toolbar search input', () => {
238
+ fixture.componentRef.setInput('showOnlySearchResultOverlay', true);
239
+ fixture.detectChanges();
240
+ const result = fixture.debugElement.query(By.css('.toolbar-search'));
241
+ expect(result).toBeNull();
242
+ });
243
+ it('Should show toolbar search input', () => {
244
+ fixture.componentRef.setInput('showOnlySearchResultOverlay', false);
245
+ fixture.detectChanges();
246
+ const result = fixture.debugElement.query(By.css('.toolbar-search'));
247
+ expect(result).toBeTruthy();
248
+ });
249
+ it('Should return [] when use custom all label', () => {
250
+ fixture.componentRef.setInput('customAllLabel', 'Custom All Label');
251
+ const result = component._categoryToSourceName('Custom All Label');
252
+ expect(result).toEqual([]);
253
+ });
254
+ it('should return true on change of cutom search event for search value', () => {
255
+ const changes: SimpleChanges = {
256
+ customSearchEvent: {
257
+ previousValue: {searchValue: '', modelName: 'All'},
258
+ currentValue: {searchValue: 'Text', modelName: 'All'},
259
+ },
260
+ } as unknown as SimpleChanges;
261
+ const result = component['_isCustomSearchEventChange'](
262
+ changes,
263
+ 'searchValue',
264
+ );
265
+ expect(result).toBeTruthy();
266
+ });
267
+ it('should return false on change of custom search event for search value', () => {
268
+ const changes: SimpleChanges = {
269
+ customSearchEvent: {
270
+ previousValue: {searchValue: 'Text', modelName: 'All'},
271
+ currentValue: {searchValue: 'Text', modelName: 'All'},
272
+ },
273
+ } as unknown as SimpleChanges;
274
+ const result = component['_isCustomSearchEventChange'](
275
+ changes,
276
+ 'searchValue',
277
+ );
278
+ expect(result).toBeFalsy();
279
+ });
280
+ it('should return true on change of custom search event for model name', () => {
281
+ const changes: SimpleChanges = {
282
+ customSearchEvent: {
283
+ previousValue: {searchValue: '', modelName: 'All'},
284
+ currentValue: {searchValue: 'Text', modelName: 'Other'},
285
+ },
286
+ } as unknown as SimpleChanges;
287
+ const result = component['_isCustomSearchEventChange'](
288
+ changes,
289
+ 'modelName',
290
+ );
291
+ expect(result).toBeTruthy();
292
+ });
293
+ it('should return false on change of custom search event for model name', () => {
294
+ const changes: SimpleChanges = {
295
+ customSearchEvent: {
296
+ previousValue: {searchValue: '', modelName: 'Other'},
297
+ currentValue: {searchValue: 'Text', modelName: 'Other'},
298
+ },
299
+ } as unknown as SimpleChanges;
300
+ const result = component['_isCustomSearchEventChange'](
301
+ changes,
302
+ 'modelName',
303
+ );
304
+ expect(result).toBeFalsy();
305
+ });
306
+ });