Skip to content

Commit

Permalink
fix(search): SSR gets stuck in resolver when query is empty (#2954)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Aug 6, 2024
1 parent 38de74a commit 16b6000
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 37 deletions.
82 changes: 53 additions & 29 deletions libs/search/routing/src/resolvers/search/search.resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(<ActivatedRouteSnapshot><unknown>{
...route.snapshot,
queryParams: {
query,
},
}).subscribe();
expect(store.dispatch).toHaveBeenCalledWith(
new DaffSearchLoad(query, options),
);
});
describe('when the query is defined', () => {
let snapshot: ActivatedRouteSnapshot;

beforeEach(() => {
snapshot = <ActivatedRouteSnapshot><unknown>{
...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();
});
});

Expand Down
18 changes: 10 additions & 8 deletions libs/search/routing/src/resolvers/search/search.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 16b6000

Please sign in to comment.