Skip to content

Commit

Permalink
test(sanity): ensure ordering is applied correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed May 1, 2024
1 parent 3342213 commit ee967b4
Showing 1 changed file with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {useReducer} from 'react'
import {type RecentSearch} from '../../datastores/recentSearches'
import {type SearchOrdering} from '../../types'
import {
type InitialSearchState,
initialSearchState,
type SearchAction,
searchReducer,
Expand Down Expand Up @@ -39,12 +40,15 @@ const recentSearchTerms = {
query: 'foo',
types: [],
} as RecentSearch

const initialStateContext: InitialSearchState = {
currentUser: mockUser,
definitions: {fields: {}, filters: {}, operators: {}},
pagination: {cursor: null, nextCursor: null},
}

const initialState: SearchReducerState = {
...initialSearchState({
currentUser: mockUser,
definitions: {fields: {}, filters: {}, operators: {}},
pagination: {cursor: null, nextCursor: null},
}),
...initialSearchState(initialStateContext),
terms: recentSearchTerms,
}

Expand Down Expand Up @@ -119,6 +123,52 @@ describe('searchReducer', () => {
expect((state.terms as RecentSearch).__recent).toBeUndefined()
})

it('should not include an order when using Text Search API strategy and ordering by relevance', () => {
const {result} = renderHook(() =>
useReducer(
searchReducer,
initialSearchState({
...initialStateContext,
enableLegacySearch: false,
}),
),
)

const [state] = result.current

expect(state.ordering).toMatchInlineSnapshot(`
Object {
"customMeasurementLabel": "relevance",
"titleKey": "search.ordering.best-match-label",
}
`)
})

it('should order by `updatedAt` when using GROQ Query API strategy and ordering by relevance', () => {
const {result} = renderHook(() =>
useReducer(
searchReducer,
initialSearchState({
...initialStateContext,
enableLegacySearch: true,
}),
),
)

const [state] = result.current

expect(state.ordering).toMatchInlineSnapshot(`
Object {
"customMeasurementLabel": "relevance",
"sort": Object {
"direction": "desc",
"field": "_updatedAt",
},
"titleKey": "search.ordering.best-match-label",
}
`)
})

it('should merge results after fetching an additional page', () => {
const {result} = renderHook(() => useReducer(searchReducer, initialState))
const [, dispatch] = result.current
Expand Down

0 comments on commit ee967b4

Please sign in to comment.