Skip to content

Commit 4acda3b

Browse files
[MNT-25413] ADW - First login after a clear cache does not show the s… (#11318)
1 parent 4feba2f commit 4acda3b

File tree

2 files changed

+126
-90
lines changed

2 files changed

+126
-90
lines changed

lib/content-services/src/lib/common/services/saved-searches.service.spec.ts

Lines changed: 110 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ describe('SavedSearchesService', () => {
5757
});
5858
service = TestBed.inject(SavedSearchesService);
5959
authService = TestBed.inject(AuthenticationService);
60-
spyOn(service.nodesApi, 'getNode').and.callFake(() => Promise.resolve({ entry: { id: testNodeId } } as NodeEntry));
6160
spyOn(service.nodesApi, 'getNodeContent').and.callFake(() => createBlob());
6261
spyOn(service.nodesApi, 'deleteNode').and.callFake(() => Promise.resolve());
6362
spyOn(service.preferencesApi, 'getPreference').and.callFake(() =>
@@ -72,106 +71,140 @@ describe('SavedSearchesService', () => {
7271
localStorage.removeItem(LOCAL_STORAGE_KEY);
7372
});
7473

75-
it('should retrieve saved searches from the preferences API', (done) => {
76-
spyOn(authService, 'getUsername').and.callFake(() => testUserName);
77-
spyOn(localStorage, 'getItem').and.callFake(() => 'true');
78-
service.init();
79-
80-
service.getSavedSearches().subscribe((searches) => {
81-
expect(localStorage.getItem).toHaveBeenCalledWith(LOCAL_STORAGE_KEY);
82-
expect(service.preferencesApi.getPreference).toHaveBeenCalledWith('-me-', 'saved-searches');
83-
expect(searches.length).toBe(2);
84-
expect(searches[0].name).toBe('Search 1');
85-
expect(searches[1].name).toBe('Search 2');
86-
done();
74+
describe('Saved searches retrieval and migration', () => {
75+
beforeEach(() => {
76+
spyOn(service.nodesApi, 'getNode').and.callFake(() => Promise.resolve({ entry: { id: testNodeId } } as NodeEntry));
77+
});
78+
it('should retrieve saved searches from the preferences API', (done) => {
79+
spyOn(authService, 'getUsername').and.callFake(() => testUserName);
80+
spyOn(localStorage, 'getItem').and.callFake(() => 'true');
81+
service.init();
82+
83+
service.getSavedSearches().subscribe((searches) => {
84+
expect(localStorage.getItem).toHaveBeenCalledWith(LOCAL_STORAGE_KEY);
85+
expect(service.preferencesApi.getPreference).toHaveBeenCalledWith('-me-', 'saved-searches');
86+
expect(searches.length).toBe(2);
87+
expect(searches[0].name).toBe('Search 1');
88+
expect(searches[1].name).toBe('Search 2');
89+
done();
90+
});
8791
});
88-
});
8992

90-
it('should automatically migrate saved searches if config.json file exists', (done) => {
91-
spyOn(localStorage, 'setItem');
92-
spyOn(authService, 'getUsername').and.callFake(() => testUserName);
93+
it('should automatically migrate saved searches if config.json file exists', (done) => {
94+
spyOn(localStorage, 'setItem');
95+
spyOn(authService, 'getUsername').and.callFake(() => testUserName);
9396

94-
service.getSavedSearches().subscribe((searches) => {
95-
expect(service.nodesApi.getNode).toHaveBeenCalledWith('-my-', { relativePath: 'config.json' });
96-
expect(service.nodesApi.getNodeContent).toHaveBeenCalledWith(testNodeId);
97-
expect(localStorage.setItem).toHaveBeenCalledWith(LOCAL_STORAGE_KEY, 'true');
98-
expect(service.preferencesApi.updatePreference).toHaveBeenCalledWith('-me-', 'saved-searches', SAVED_SEARCHES_CONTENT);
99-
expect(service.nodesApi.deleteNode).toHaveBeenCalledWith(testNodeId, { permanent: true });
100-
expect(searches.length).toBe(2);
101-
done();
97+
service.getSavedSearches().subscribe((searches) => {
98+
expect(service.nodesApi.getNode).toHaveBeenCalledWith('-my-', { relativePath: 'config.json' });
99+
expect(service.nodesApi.getNodeContent).toHaveBeenCalledWith(testNodeId);
100+
expect(localStorage.setItem).toHaveBeenCalledWith(LOCAL_STORAGE_KEY, 'true');
101+
expect(service.preferencesApi.updatePreference).toHaveBeenCalledWith('-me-', 'saved-searches', SAVED_SEARCHES_CONTENT);
102+
expect(service.nodesApi.deleteNode).toHaveBeenCalledWith(testNodeId, { permanent: true });
103+
expect(searches.length).toBe(2);
104+
done();
105+
});
102106
});
103-
});
104107

105-
it('should save a new search', (done) => {
106-
spyOn(authService, 'getUsername').and.callFake(() => testUserName);
107-
spyOn(localStorage, 'getItem').and.callFake(() => 'true');
108-
const newSearch = { name: 'Search 3', description: 'Description 3', encodedUrl: 'url3' };
109-
service.init();
108+
it('should save a new search', (done) => {
109+
spyOn(authService, 'getUsername').and.callFake(() => testUserName);
110+
spyOn(localStorage, 'getItem').and.callFake(() => 'true');
111+
const newSearch = { name: 'Search 3', description: 'Description 3', encodedUrl: 'url3' };
112+
service.init();
110113

111-
service.saveSearch(newSearch).subscribe(() => {
112-
expect(service.preferencesApi.updatePreference).toHaveBeenCalledWith('-me-', 'saved-searches', jasmine.any(String));
113-
expect(service.savedSearches$).toBeDefined();
114-
done();
114+
service.saveSearch(newSearch).subscribe(() => {
115+
expect(service.preferencesApi.updatePreference).toHaveBeenCalledWith('-me-', 'saved-searches', jasmine.any(String));
116+
expect(service.savedSearches$).toBeDefined();
117+
done();
118+
});
115119
});
116-
});
117120

118-
it('should emit initial saved searches on subscription', (done) => {
119-
spyOn(authService, 'getUsername').and.callFake(() => testUserName);
120-
spyOn(localStorage, 'getItem').and.returnValue('true');
121-
service.init();
121+
it('should emit initial saved searches on subscription', (done) => {
122+
spyOn(authService, 'getUsername').and.callFake(() => testUserName);
123+
spyOn(localStorage, 'getItem').and.returnValue('true');
124+
service.init();
122125

123-
service.savedSearches$.pipe().subscribe((searches) => {
124-
expect(searches.length).toBe(2);
125-
expect(searches[0].name).toBe('Search 1');
126-
expect(service.preferencesApi.getPreference).toHaveBeenCalledWith('-me-', 'saved-searches');
127-
done();
126+
service.savedSearches$.pipe().subscribe((searches) => {
127+
expect(searches.length).toBe(2);
128+
expect(searches[0].name).toBe('Search 1');
129+
expect(service.preferencesApi.getPreference).toHaveBeenCalledWith('-me-', 'saved-searches');
130+
done();
131+
});
132+
133+
service.getSavedSearches().subscribe();
128134
});
129135

130-
service.getSavedSearches().subscribe();
131-
});
136+
it('should emit updated saved searches after saving a new search', (done) => {
137+
spyOn(authService, 'getUsername').and.callFake(() => testUserName);
138+
spyOn(localStorage, 'getItem').and.callFake(() => 'true');
139+
const newSearch = { name: 'Search 3', description: 'Description 3', encodedUrl: 'url3' };
140+
service.init();
141+
142+
service.saveSearch(newSearch).subscribe(() => {
143+
service.savedSearches$.subscribe((searches) => {
144+
expect(searches.length).toBe(3);
145+
expect(searches[2].name).toBe('Search 2');
146+
expect(service.preferencesApi.updatePreference).toHaveBeenCalledWith('-me-', 'saved-searches', jasmine.any(String));
147+
done();
148+
});
149+
});
150+
});
132151

133-
it('should emit updated saved searches after saving a new search', (done) => {
134-
spyOn(authService, 'getUsername').and.callFake(() => testUserName);
135-
spyOn(localStorage, 'getItem').and.callFake(() => 'true');
136-
const newSearch = { name: 'Search 3', description: 'Description 3', encodedUrl: 'url3' };
137-
service.init();
152+
it('should edit a search', (done) => {
153+
const updatedSearch = { name: 'Search 3', description: 'Description 3', encodedUrl: 'url3', order: 0 };
154+
prepareDefaultMock();
155+
156+
service.editSavedSearch(updatedSearch).subscribe(() => {
157+
service.savedSearches$.subscribe((searches) => {
158+
expect(searches.length).toBe(2);
159+
expect(searches[0].name).toBe('Search 3');
160+
expect(searches[0].order).toBe(0);
161+
expect(searches[1].name).toBe('Search 2');
162+
expect(searches[1].order).toBe(1);
163+
done();
164+
});
165+
});
166+
});
138167

139-
service.saveSearch(newSearch).subscribe(() => {
140-
service.savedSearches$.subscribe((searches) => {
141-
expect(searches.length).toBe(3);
142-
expect(searches[2].name).toBe('Search 2');
143-
expect(service.preferencesApi.updatePreference).toHaveBeenCalledWith('-me-', 'saved-searches', jasmine.any(String));
144-
done();
168+
it('should delete a search', (done) => {
169+
const searchToDelete = { name: 'Search 1', description: 'Description 1', encodedUrl: 'url1', order: 0 };
170+
prepareDefaultMock();
171+
172+
service.deleteSavedSearch(searchToDelete).subscribe(() => {
173+
service.savedSearches$.subscribe((searches) => {
174+
expect(searches.length).toBe(1);
175+
expect(searches[0].name).toBe('Search 2');
176+
expect(searches[0].order).toBe(0);
177+
done();
178+
});
145179
});
146180
});
147181
});
148182

149-
it('should edit a search', (done) => {
150-
const updatedSearch = { name: 'Search 3', description: 'Description 3', encodedUrl: 'url3', order: 0 };
151-
prepareDefaultMock();
183+
describe('Saved searches error handling', () => {
184+
it('should fallback to preferences API if getting saved searches node ID fails', (done) => {
185+
spyOn(authService, 'getUsername').and.returnValue(testUserName);
186+
spyOn(localStorage, 'getItem').and.returnValue('');
187+
const error = new Error(JSON.stringify({ error: { statusCode: 500 } }));
188+
spyOn(service.nodesApi, 'getNode').and.returnValue(Promise.reject(error));
152189

153-
service.editSavedSearch(updatedSearch).subscribe(() => {
154-
service.savedSearches$.subscribe((searches) => {
190+
service.getSavedSearches().subscribe((searches) => {
191+
expect(service.preferencesApi.getPreference).toHaveBeenCalledWith('-me-', 'saved-searches');
155192
expect(searches.length).toBe(2);
156-
expect(searches[0].name).toBe('Search 3');
157-
expect(searches[0].order).toBe(0);
158-
159-
expect(searches[1].name).toBe('Search 2');
160-
expect(searches[1].order).toBe(1);
161193
done();
162194
});
163195
});
164-
});
165196

166-
it('should delete a search', (done) => {
167-
const searchToDelete = { name: 'Search 1', description: 'Description 1', encodedUrl: 'url1', order: 0 };
168-
prepareDefaultMock();
197+
it('should handle 404 from getNode() by setting migration flag and falling back to preferences API', (done) => {
198+
spyOn(authService, 'getUsername').and.returnValue(testUserName);
199+
spyOn(localStorage, 'getItem').and.returnValue('');
200+
spyOn(localStorage, 'setItem');
201+
const notFoundError = new Error(JSON.stringify({ error: { statusCode: 404 } }));
202+
spyOn(service.nodesApi, 'getNode').and.returnValue(Promise.reject(notFoundError));
169203

170-
service.deleteSavedSearch(searchToDelete).subscribe(() => {
171-
service.savedSearches$.subscribe((searches) => {
172-
expect(searches.length).toBe(1);
173-
expect(searches[0].name).toBe('Search 2');
174-
expect(searches[0].order).toBe(0);
204+
service.getSavedSearches().subscribe((searches) => {
205+
expect(localStorage.setItem).toHaveBeenCalledWith(LOCAL_STORAGE_KEY, 'true');
206+
expect(service.preferencesApi.getPreference).toHaveBeenCalledWith('-me-', 'saved-searches');
207+
expect(searches.length).toBe(2);
175208
done();
176209
});
177210
});

lib/content-services/src/lib/common/services/saved-searches.service.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ export class SavedSearchesService {
5656

5757
readonly savedSearches$ = new ReplaySubject<SavedSearch[]>(1);
5858

59-
constructor(private readonly apiService: AlfrescoApiService, private readonly authService: AuthenticationService) {}
59+
constructor(
60+
private readonly apiService: AlfrescoApiService,
61+
private readonly authService: AuthenticationService
62+
) {}
6063

6164
init(): void {
6265
this.fetchSavedSearches();
@@ -70,23 +73,18 @@ export class SavedSearchesService {
7073
getSavedSearches(): Observable<SavedSearch[]> {
7174
const savedSearchesMigrated = localStorage.getItem(this.getLocalStorageKey()) ?? '';
7275
if (savedSearchesMigrated === 'true') {
73-
return from(this.preferencesApi.getPreference('-me-', 'saved-searches')).pipe(
74-
map((preference) => JSON.parse(preference.entry.value)),
75-
catchError(() => of([]))
76-
);
76+
return this.getSavedSearchesFromPreferenceApi();
7777
} else {
7878
return this.getSavedSearchesNodeId().pipe(
7979
take(1),
8080
concatMap(() => {
8181
if (this.savedSearchFileNodeId !== '') {
8282
return this.migrateSavedSearches();
8383
} else {
84-
return from(this.preferencesApi.getPreference('-me-', 'saved-searches')).pipe(
85-
map((preference) => JSON.parse(preference.entry.value)),
86-
catchError(() => of([]))
87-
);
84+
return this.getSavedSearchesFromPreferenceApi();
8885
}
89-
})
86+
}),
87+
catchError(() => this.getSavedSearchesFromPreferenceApi())
9088
);
9189
}
9290
}
@@ -237,10 +235,8 @@ export class SavedSearchesService {
237235
const errorStatusCode = JSON.parse(error.message).error.statusCode;
238236
if (errorStatusCode === 404) {
239237
localStorage.setItem(this.getLocalStorageKey(), 'true');
240-
return '';
241-
} else {
242-
return throwError(() => error);
243238
}
239+
return throwError(() => error);
244240
})
245241
);
246242
}
@@ -271,4 +267,11 @@ export class SavedSearchesService {
271267
})
272268
);
273269
}
270+
271+
private getSavedSearchesFromPreferenceApi(): Observable<SavedSearch[]> {
272+
return from(this.preferencesApi.getPreference('-me-', 'saved-searches')).pipe(
273+
map((preference) => JSON.parse(preference.entry.value)),
274+
catchError(() => of([]))
275+
);
276+
}
274277
}

0 commit comments

Comments
 (0)