diff --git a/libs/search/routing/src/resolvers/search/search.resolver.spec.ts b/libs/search/routing/src/resolvers/search/search.resolver.spec.ts index 1b7525a3b2..4652699aab 100644 --- a/libs/search/routing/src/resolvers/search/search.resolver.spec.ts +++ b/libs/search/routing/src/resolvers/search/search.resolver.spec.ts @@ -78,44 +78,68 @@ describe('@daffodil/search/routing | DaffSearchPageResolver', () => { spyOn(store, 'dispatch'); })); - it('should dispatch a DaffSearchLoad action with the correct search query', () => { - searchResolver.resolve({ - ...route.snapshot, - queryParams: { - query, - }, - }).subscribe(); - expect(store.dispatch).toHaveBeenCalledWith( - new DaffSearchLoad(query, options), - ); - }); + describe('when the query is defined', () => { + let snapshot: ActivatedRouteSnapshot; + + beforeEach(() => { + snapshot = { + ...route.snapshot, + queryParams: { + query, + }, + }; + }); - it('should resolve when DaffSearchLoadSuccess is dispatched', done => { - searchResolver.resolve(route.snapshot).subscribe(value => { - expect(value).toEqual(true); - done(); + it('should dispatch a DaffSearchLoad action with the correct search query', () => { + searchResolver.resolve(snapshot).subscribe(); + expect(store.dispatch).toHaveBeenCalledWith( + new DaffSearchLoad(query, options), + ); }); - actions$.next(new DaffSearchLoadSuccess({ - collection: {}, - metadata: {}, - })); - }); + it('should resolve when DaffSearchLoadSuccess is dispatched', done => { + searchResolver.resolve(snapshot).subscribe(value => { + expect(value).toEqual(true); + done(); + }); - it('should resolve when DaffCartLoadFailure is dispatched', done => { - searchResolver.resolve(route.snapshot).subscribe(value => { - expect(value).toEqual(true); - done(); + actions$.next(new DaffSearchLoadSuccess({ + collection: {}, + metadata: {}, + })); }); - actions$.next(new DaffSearchLoadFailure(null)); + it('should resolve when DaffCartLoadFailure is dispatched', done => { + searchResolver.resolve(snapshot).subscribe(value => { + expect(value).toEqual(true); + done(); + }); + + actions$.next(new DaffSearchLoadFailure(null)); + }); + + it('should not resolve without a search load success or failure', () => { + searchResolver.resolve(snapshot).subscribe(() => { + fail(); + }); + expect(true).toBeTruthy(); + }); }); - it('should not resolve without a search load success or failure', () => { - searchResolver.resolve(route.snapshot).subscribe(() => { - fail(); + describe('when the query is not defined', () => { + it('should return true immediately', (done) => { + searchResolver.resolve(route.snapshot).subscribe((res) => { + expect(res).toBeTrue(); + done(); + }); + }); + + it('should not initiate a search', (done) => { + searchResolver.resolve(route.snapshot).subscribe((res) => { + expect(store.dispatch).not.toHaveBeenCalled(); + done(); + }); }); - expect(true).toBeTruthy(); }); }); diff --git a/libs/search/routing/src/resolvers/search/search.resolver.ts b/libs/search/routing/src/resolvers/search/search.resolver.ts index 062420aa18..8a1f18c076 100644 --- a/libs/search/routing/src/resolvers/search/search.resolver.ts +++ b/libs/search/routing/src/resolvers/search/search.resolver.ts @@ -50,15 +50,17 @@ export class DaffSearchPageResolver { const query = getQuery(route); if (query) { this.store.dispatch(new DaffSearchLoad(query, this.builder(route))); + + return isPlatformBrowser(this.platformId) ? of(true) : this.dispatcher.pipe( + ofType( + DaffSearchActionTypes.SearchLoadSuccessAction, + DaffSearchActionTypes.SearchLoadFailureAction, + ), + map(() => true), + take(1), + ); } - return isPlatformBrowser(this.platformId) ? of(true) : this.dispatcher.pipe( - ofType( - DaffSearchActionTypes.SearchLoadSuccessAction, - DaffSearchActionTypes.SearchLoadFailureAction, - ), - map(() => true), - take(1), - ); + return of(true); } }