[MNT-25413] ADW - First login after a clear cache does not show the s… (#11318)

This commit is contained in:
dominikiwanekhyland
2025-10-31 16:03:23 +01:00
committed by GitHub
parent 4feba2f304
commit 4acda3b09b
2 changed files with 140 additions and 104 deletions
@@ -57,7 +57,6 @@ describe('SavedSearchesService', () => {
}); });
service = TestBed.inject(SavedSearchesService); service = TestBed.inject(SavedSearchesService);
authService = TestBed.inject(AuthenticationService); authService = TestBed.inject(AuthenticationService);
spyOn(service.nodesApi, 'getNode').and.callFake(() => Promise.resolve({ entry: { id: testNodeId } } as NodeEntry));
spyOn(service.nodesApi, 'getNodeContent').and.callFake(() => createBlob()); spyOn(service.nodesApi, 'getNodeContent').and.callFake(() => createBlob());
spyOn(service.nodesApi, 'deleteNode').and.callFake(() => Promise.resolve()); spyOn(service.nodesApi, 'deleteNode').and.callFake(() => Promise.resolve());
spyOn(service.preferencesApi, 'getPreference').and.callFake(() => spyOn(service.preferencesApi, 'getPreference').and.callFake(() =>
@@ -72,6 +71,10 @@ describe('SavedSearchesService', () => {
localStorage.removeItem(LOCAL_STORAGE_KEY); localStorage.removeItem(LOCAL_STORAGE_KEY);
}); });
describe('Saved searches retrieval and migration', () => {
beforeEach(() => {
spyOn(service.nodesApi, 'getNode').and.callFake(() => Promise.resolve({ entry: { id: testNodeId } } as NodeEntry));
});
it('should retrieve saved searches from the preferences API', (done) => { it('should retrieve saved searches from the preferences API', (done) => {
spyOn(authService, 'getUsername').and.callFake(() => testUserName); spyOn(authService, 'getUsername').and.callFake(() => testUserName);
spyOn(localStorage, 'getItem').and.callFake(() => 'true'); spyOn(localStorage, 'getItem').and.callFake(() => 'true');
@@ -155,7 +158,6 @@ describe('SavedSearchesService', () => {
expect(searches.length).toBe(2); expect(searches.length).toBe(2);
expect(searches[0].name).toBe('Search 3'); expect(searches[0].name).toBe('Search 3');
expect(searches[0].order).toBe(0); expect(searches[0].order).toBe(0);
expect(searches[1].name).toBe('Search 2'); expect(searches[1].name).toBe('Search 2');
expect(searches[1].order).toBe(1); expect(searches[1].order).toBe(1);
done(); done();
@@ -176,6 +178,37 @@ describe('SavedSearchesService', () => {
}); });
}); });
}); });
});
describe('Saved searches error handling', () => {
it('should fallback to preferences API if getting saved searches node ID fails', (done) => {
spyOn(authService, 'getUsername').and.returnValue(testUserName);
spyOn(localStorage, 'getItem').and.returnValue('');
const error = new Error(JSON.stringify({ error: { statusCode: 500 } }));
spyOn(service.nodesApi, 'getNode').and.returnValue(Promise.reject(error));
service.getSavedSearches().subscribe((searches) => {
expect(service.preferencesApi.getPreference).toHaveBeenCalledWith('-me-', 'saved-searches');
expect(searches.length).toBe(2);
done();
});
});
it('should handle 404 from getNode() by setting migration flag and falling back to preferences API', (done) => {
spyOn(authService, 'getUsername').and.returnValue(testUserName);
spyOn(localStorage, 'getItem').and.returnValue('');
spyOn(localStorage, 'setItem');
const notFoundError = new Error(JSON.stringify({ error: { statusCode: 404 } }));
spyOn(service.nodesApi, 'getNode').and.returnValue(Promise.reject(notFoundError));
service.getSavedSearches().subscribe((searches) => {
expect(localStorage.setItem).toHaveBeenCalledWith(LOCAL_STORAGE_KEY, 'true');
expect(service.preferencesApi.getPreference).toHaveBeenCalledWith('-me-', 'saved-searches');
expect(searches.length).toBe(2);
done();
});
});
});
/** /**
* Prepares default mocks for service * Prepares default mocks for service
@@ -56,7 +56,10 @@ export class SavedSearchesService {
readonly savedSearches$ = new ReplaySubject<SavedSearch[]>(1); readonly savedSearches$ = new ReplaySubject<SavedSearch[]>(1);
constructor(private readonly apiService: AlfrescoApiService, private readonly authService: AuthenticationService) {} constructor(
private readonly apiService: AlfrescoApiService,
private readonly authService: AuthenticationService
) {}
init(): void { init(): void {
this.fetchSavedSearches(); this.fetchSavedSearches();
@@ -70,10 +73,7 @@ export class SavedSearchesService {
getSavedSearches(): Observable<SavedSearch[]> { getSavedSearches(): Observable<SavedSearch[]> {
const savedSearchesMigrated = localStorage.getItem(this.getLocalStorageKey()) ?? ''; const savedSearchesMigrated = localStorage.getItem(this.getLocalStorageKey()) ?? '';
if (savedSearchesMigrated === 'true') { if (savedSearchesMigrated === 'true') {
return from(this.preferencesApi.getPreference('-me-', 'saved-searches')).pipe( return this.getSavedSearchesFromPreferenceApi();
map((preference) => JSON.parse(preference.entry.value)),
catchError(() => of([]))
);
} else { } else {
return this.getSavedSearchesNodeId().pipe( return this.getSavedSearchesNodeId().pipe(
take(1), take(1),
@@ -81,12 +81,10 @@ export class SavedSearchesService {
if (this.savedSearchFileNodeId !== '') { if (this.savedSearchFileNodeId !== '') {
return this.migrateSavedSearches(); return this.migrateSavedSearches();
} else { } else {
return from(this.preferencesApi.getPreference('-me-', 'saved-searches')).pipe( return this.getSavedSearchesFromPreferenceApi();
map((preference) => JSON.parse(preference.entry.value)),
catchError(() => of([]))
);
} }
}) }),
catchError(() => this.getSavedSearchesFromPreferenceApi())
); );
} }
} }
@@ -237,10 +235,8 @@ export class SavedSearchesService {
const errorStatusCode = JSON.parse(error.message).error.statusCode; const errorStatusCode = JSON.parse(error.message).error.statusCode;
if (errorStatusCode === 404) { if (errorStatusCode === 404) {
localStorage.setItem(this.getLocalStorageKey(), 'true'); localStorage.setItem(this.getLocalStorageKey(), 'true');
return '';
} else {
return throwError(() => error);
} }
return throwError(() => error);
}) })
); );
} }
@@ -271,4 +267,11 @@ export class SavedSearchesService {
}) })
); );
} }
private getSavedSearchesFromPreferenceApi(): Observable<SavedSearch[]> {
return from(this.preferencesApi.getPreference('-me-', 'saved-searches')).pipe(
map((preference) => JSON.parse(preference.entry.value)),
catchError(() => of([]))
);
}
} }