Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-128625 / 24.10 / Recent searches in global search shows unusual results #10007

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('GlobalSearchComponent', () => {
spectator.component.resetInput();
spectator.detectChanges();

expect(spectator.component.searchControl.value).toBeNull();
expect(spectator.component.searchControl.value).toBe('');
expect(document.activeElement).toBe(spectator.query('input'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class GlobalSearchComponent implements OnInit {
}

resetInput(): void {
this.searchControl.reset();
this.searchControl.setValue('');
}

private listenForSearchChanges(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import { GlobalSearchSection } from 'app/modules/global-search/enums/global-sear
import { GlobalSearchSectionsProvider } from 'app/modules/global-search/services/global-search-sections.service';
import { UiSearchProvider } from 'app/modules/global-search/services/ui-search.service';

jest.mock('app/../assets/ui-searchable-elements.json', () => ([
{
hierarchy: ['search1'],
synonyms: [],
requiredRoles: [],
anchorRouterLink: [],
routerLink: null,
anchor: 'some-anchor',
triggerAnchor: null,
section: GlobalSearchSection.Ui,
},
]));

describe('GlobalSearchSectionsProvider', () => {
let spectator: SpectatorService<GlobalSearchSectionsProvider>;
const mockLocalStorage = {
Expand All @@ -25,7 +38,9 @@ describe('GlobalSearchSectionsProvider', () => {
],
});

beforeEach(() => spectator = createService());
beforeEach(() => {
spectator = createService();
});

it('should fetch UI section results based on search term', () => {
const searchTerm = 'test';
Expand Down Expand Up @@ -60,13 +75,28 @@ describe('GlobalSearchSectionsProvider', () => {
}]);
});

it('should retrieve recent searches from localStorage', () => {
const recentSearches = [{ hierarchy: ['search1'], targetHref: 'url1', section: '' }];
// ui-searchable-elements.json is mocked above
it('should retrieve recent searches from localStorage and remove outdated items from local storage if any', () => {
const recentSearches = [
{
hierarchy: ['search1'], // existing hierarchy
targetHref: 'url1',
},
{
hierarchy: ['search to be removed'], // non-existing hierarchy
targetHref: 'url2',
},
];

mockLocalStorage.getItem.mockReturnValue(JSON.stringify(recentSearches));

const results = spectator.service.getRecentSearchesSectionResults();

expect(mockLocalStorage.getItem).toHaveBeenCalledWith('recentSearches');
expect(results).toEqual(recentSearches);
expect(mockLocalStorage.setItem).toHaveBeenCalledWith('recentSearches', JSON.stringify([recentSearches[0]]));

expect(results).toEqual([
recentSearches[0],
]);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Inject, Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import UiElementsJson from 'app/../assets/ui-searchable-elements.json';
import { Observable } from 'rxjs';
import { WINDOW } from 'app/helpers/window.helper';
import { GlobalSearchSection } from 'app/modules/global-search/enums/global-search-section.enum';
Expand Down Expand Up @@ -41,6 +42,17 @@ export class GlobalSearchSectionsProvider {
}

getRecentSearchesSectionResults(): UiSearchableElement[] {
return JSON.parse(this.window.localStorage.getItem('recentSearches') || '[]');
const recentSearches = JSON.parse(this.window.localStorage.getItem('recentSearches') || '[]') as UiSearchableElement[];
const exitingHierarchies = new Set(UiElementsJson.map((item) => JSON.stringify(item.hierarchy)));

const validRecentSearches = recentSearches.filter((item) => {
return exitingHierarchies.has(JSON.stringify(item.hierarchy)) || item.hierarchy[0].startsWith('Search Documentation for');
});

if (recentSearches.length !== validRecentSearches.length) {
this.window.localStorage.setItem('recentSearches', JSON.stringify(validRecentSearches));
}

return validRecentSearches;
}
}