Skip to content

Commit

Permalink
fix(useLimitedSearchResults): Don't subscribe to topic when query is …
Browse files Browse the repository at this point in the history
…empty (#2187)

* fix(useLimitedSearchResults): Don't subscribe to topic when query is empty

* Update assets/tests/hooks/useSearchResults.test.ts

Co-authored-by: Kayla Firestack <[email protected]>

---------

Co-authored-by: Kayla Firestack <[email protected]>
  • Loading branch information
KaylaBrady and firestack authored Aug 16, 2023
1 parent 6debd51 commit baf9663
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion assets/src/hooks/useSearchResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export const useLimitedSearchResults = (
query: { property: VehiclePropertyQuery; text: string; limit: number } | null
): Ok<LimitedSearchResults<Vehicle | Ghost>> | Loading | null => {
const topic: string | null =
query && `vehicles_search:limited:${query.property}:${query.text}`
query?.text && query.text != ""
? `vehicles_search:limited:${query.property}:${query.text}`
: null

const [state, pushUpdate] = useCheckedTwoWayChannel<
LimitedSearchResultsData,
Expand Down
14 changes: 14 additions & 0 deletions assets/tests/hooks/useSearchResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,20 @@ describe("useLimitedSearchResults", () => {
)
expect(result.current).toEqual(null)
})

test("when no query is for empty string, returns null", () => {
const mockSocket = makeMockSocket()

const { result } = renderHook(() =>
useLimitedSearchResults(mockSocket, {
text: "",
property: "run",
limit: 5,
})
)
expect(result.current).toEqual(null)
})

test("initializing the hook subscribes to the search results", () => {
const mockSocket = makeMockSocket()
const mockChannel = makeMockChannel("ok")
Expand Down

0 comments on commit baf9663

Please sign in to comment.