Skip to content

Commit 10b733e

Browse files
committed
[MNT-25635] Search filter autocomplete search for tags when input changes
1 parent 7b29d56 commit 10b733e

File tree

2 files changed

+35
-39
lines changed

2 files changed

+35
-39
lines changed

lib/content-services/src/lib/search/components/search-filter-autocomplete-chips/search-filter-autocomplete-chips.component.spec.ts

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import { ComponentFixture, TestBed } from '@angular/core/testing';
1919
import { By } from '@angular/platform-browser';
2020
import { SearchFilterAutocompleteChipsComponent } from './search-filter-autocomplete-chips.component';
21-
import { EMPTY, of, ReplaySubject } from 'rxjs';
21+
import { of, ReplaySubject } from 'rxjs';
2222
import { AutocompleteField, AutocompleteOption } from '../../models/autocomplete-option.interface';
2323
import { TagService } from '../../../tag/services/tag.service';
2424
import { SitesService } from '../../../common/services/sites.service';
@@ -33,13 +33,7 @@ describe('SearchFilterAutocompleteChipsComponent', () => {
3333

3434
beforeEach(() => {
3535
TestBed.configureTestingModule({
36-
imports: [SearchFilterAutocompleteChipsComponent],
37-
providers: [
38-
{
39-
provide: TagService,
40-
useValue: { getAllTheTags: () => EMPTY }
41-
}
42-
]
36+
imports: [SearchFilterAutocompleteChipsComponent]
4337
});
4438

4539
fixture = TestBed.createComponent(SearchFilterAutocompleteChipsComponent);
@@ -86,23 +80,6 @@ describe('SearchFilterAutocompleteChipsComponent', () => {
8680
});
8781
});
8882

89-
it('should load tags if field = TAG', (done) => {
90-
const tagPagingMock = {
91-
list: {
92-
pagination: {},
93-
entries: [{ entry: { tag: 'tag1', id: 'id1' } }, { entry: { tag: 'tag2', id: 'id2' } }]
94-
}
95-
};
96-
97-
component.settings.field = AutocompleteField.TAG;
98-
spyOn(tagService, 'getAllTheTags').and.returnValue(of(tagPagingMock));
99-
component.ngOnInit();
100-
component.autocompleteOptions$.subscribe((result) => {
101-
expect(result).toEqual([{ value: 'tag1' }, { value: 'tag2' }]);
102-
done();
103-
});
104-
});
105-
10683
it('should update display value when options changes', () => {
10784
const newOption = 'option1';
10885
spyOn(component, 'onOptionsChange').and.callThrough();
@@ -215,13 +192,9 @@ describe('SearchFilterAutocompleteChipsComponent', () => {
215192
it('should use id if present, otherwise value, in LOCATION query fragment', () => {
216193
component.settings.field = AutocompleteField.LOCATION;
217194
component.settings.autocompleteOptions = [];
218-
component.selectedOptions = [
219-
{ id: 'site1', value: 'Marketing' },
220-
{ value: 'custom' }
221-
];
195+
component.selectedOptions = [{ id: 'site1', value: 'Marketing' }, { value: 'custom' }];
222196
component.submitValues();
223-
expect(component.context.queryFragments[component.id])
224-
.toBe('SITE:"site1" OR SITE:"custom"');
197+
expect(component.context.queryFragments[component.id]).toBe('SITE:"site1" OR SITE:"custom"');
225198
});
226199

227200
it('should still call sitesService.getSites when input is empty for LOCATION field', () => {
@@ -251,14 +224,30 @@ describe('SearchFilterAutocompleteChipsComponent', () => {
251224
expect(searchSpy).toHaveBeenCalledWith('', 0, 15);
252225
});
253226

227+
it('should search for tags if field = TAG and input is not empty', () => {
228+
component.settings.field = AutocompleteField.TAG;
229+
const searchSpy = spyOn(tagService, 'searchTags').and.returnValue(
230+
of({
231+
list: {
232+
pagination: {},
233+
entries: [{ entry: { tag: 'tag1', id: 'id1' } }, { entry: { tag: 'tag2', id: 'id2' } }]
234+
}
235+
})
236+
);
237+
238+
component.onInputChange('tag');
239+
240+
expect(searchSpy).toHaveBeenCalledWith('tag', { orderBy: 'tag', direction: 'asc' }, false, 0, 15);
241+
});
242+
254243
describe('optionComparator', () => {
255244
it('should return false if either option is undefined', () => {
256245
expect(component.optionComparator(undefined, { value: 'A' })).toBe(false);
257246
expect(component.optionComparator({ value: 'A' }, undefined)).toBe(false);
258247
});
259248

260249
it('should compare by id if both have id', () => {
261-
expect(component.optionComparator({ id: 'abc', value: 'B' } , { id: 'ABC', value: 'B' })).toBe(true);
250+
expect(component.optionComparator({ id: 'abc', value: 'B' }, { id: 'ABC', value: 'B' })).toBe(true);
262251
expect(component.optionComparator({ id: 'abc', value: 'B' }, { id: 'def', value: 'B' })).toBe(false);
263252
});
264253

lib/content-services/src/lib/search/components/search-filter-autocomplete-chips/search-filter-autocomplete-chips.component.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ export class SearchFilterAutocompleteChipsComponent implements SearchWidget, OnI
124124
onInputChange(value: string) {
125125
if (this.settings.field === AutocompleteField.CATEGORIES) {
126126
this.searchForExistingCategories(value);
127+
} else if (this.settings.field === AutocompleteField.TAG) {
128+
this.searchForExistingTags(value);
127129
} else if (this.settings.field === AutocompleteField.LOCATION) {
128130
this.populateSitesOptions();
129131
}
@@ -166,13 +168,7 @@ export class SearchFilterAutocompleteChipsComponent implements SearchWidget, OnI
166168
private setOptions() {
167169
switch (this.settings.field) {
168170
case AutocompleteField.TAG:
169-
this.tagService.getAllTheTags().subscribe((tagPaging) => {
170-
this.autocompleteOptionsSubject$.next(
171-
tagPaging.list.entries.map((tag) => ({
172-
value: tag.entry.tag
173-
}))
174-
);
175-
});
171+
this.autocompleteOptionsSubject$.next([]);
176172
break;
177173
case AutocompleteField.CATEGORIES:
178174
this.autocompleteOptionsSubject$.next([]);
@@ -197,6 +193,17 @@ export class SearchFilterAutocompleteChipsComponent implements SearchWidget, OnI
197193
});
198194
}
199195

196+
private searchForExistingTags(searchTerm: string) {
197+
this.tagService.searchTags(searchTerm, { orderBy: 'tag', direction: 'asc' }, false, 0, 15).subscribe((existingTagsResult) => {
198+
this.autocompleteOptionsSubject$.next(
199+
existingTagsResult.list.entries.map((tag) => ({
200+
id: tag.entry.id,
201+
value: tag.entry.tag
202+
}))
203+
);
204+
});
205+
}
206+
200207
private populateSitesOptions(): void {
201208
this.sitesService
202209
.getSites()

0 commit comments

Comments
 (0)