Skip to content

Commit

Permalink
fix(sanity): reset search cursor state when any parameter changes
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Nov 27, 2024
1 parent 0178e29 commit deb1a04
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,113 @@ describe('searchReducer', () => {
]
`)
})

it.each<SearchAction>([
{
type: 'SEARCH_CLEAR',
},
{
type: 'TERMS_QUERY_SET',
query: 'test query b',
},
{
type: 'TERMS_SET',
terms: {
query: 'test',
types: [],
},
},
{
type: 'ORDERING_SET',
ordering: {
titleKey: 'search.ordering.test-label',
sort: {
field: '_createdAt',
direction: 'desc',
},
},
},
{
type: 'ORDERING_RESET',
},
{
type: 'TERMS_FILTERS_ADD',
filter: {
filterName: 'test',
operatorType: 'test',
},
},
{
type: 'TERMS_FILTERS_REMOVE',
filterKey: 'test',
},
{
type: 'TERMS_FILTERS_SET_OPERATOR',
filterKey: 'test',
operatorType: 'test',
},
{
type: 'TERMS_FILTERS_SET_VALUE',
filterKey: 'test',
},
{
type: 'TERMS_FILTERS_CLEAR',
},
{
type: 'TERMS_TYPE_ADD',
schemaType: mockSearchableType,
},
{
type: 'TERMS_TYPE_REMOVE',
schemaType: mockSearchableType,
},
{
type: 'TERMS_TYPES_CLEAR',
},
])('should reset cursor state when any parameter changes ($type)', async (action) => {
const {result} = renderHook(() => useReducer(searchReducer, initialState))
const [, dispatch] = result.current

act(() =>
dispatch({
type: 'TERMS_QUERY_SET',
query: 'test query a',
}),
)

act(() =>
dispatch({
type: 'SEARCH_REQUEST_COMPLETE',
nextCursor: 'cursorA',
hits: [],
}),
)

act(() =>
dispatch({
type: 'PAGE_INCREMENT',
}),
)

act(() =>
dispatch({
type: 'SEARCH_REQUEST_COMPLETE',
nextCursor: 'cursorB',
hits: [],
}),
)

const [stateA] = result.current

expect(stateA.cursor).toBe('cursorA')
expect(stateA.nextCursor).toBe('cursorB')
act(() => dispatch(action))

const [stateB] = result.current

expect(stateB.cursor).toBeNull()
expect(stateB.nextCursor).toBeNull()
})
})

it.each<SearchAction>([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ export function searchReducer(state: SearchReducerState, action: SearchAction):
...state,
ordering: getOrderings({searchStrategy: state.strategy}).relevance,
terms: stripRecent(state.terms),
cursor: null,
nextCursor: null,
result: {
...state.result,
hasLocal: false,
Expand All @@ -189,6 +191,8 @@ export function searchReducer(state: SearchReducerState, action: SearchAction):
...state,
ordering: action.ordering,
terms: stripRecent(state.terms),
cursor: null,
nextCursor: null,
result: {
...state.result,
hasLocal: false,
Expand Down Expand Up @@ -262,6 +266,8 @@ export function searchReducer(state: SearchReducerState, action: SearchAction):
}),
filters,
lastAddedFilter: newFilter,
cursor: null,
nextCursor: null,
terms: {
...state.terms,
filter: generateFilterQuery({
Expand All @@ -288,6 +294,8 @@ export function searchReducer(state: SearchReducerState, action: SearchAction):
types: state.terms.types,
}),
filters,
cursor: null,
nextCursor: null,
terms: {
...state.terms,
filter: generateFilterQuery({
Expand Down Expand Up @@ -319,6 +327,8 @@ export function searchReducer(state: SearchReducerState, action: SearchAction):
types: state.terms.types,
}),
filters,
cursor: null,
nextCursor: null,
terms: {
...state.terms,
filter: generateFilterQuery({
Expand Down Expand Up @@ -362,6 +372,8 @@ export function searchReducer(state: SearchReducerState, action: SearchAction):
return {
...state,
filters,
cursor: null,
nextCursor: null,
terms: {
...state.terms,
filter: generateFilterQuery({
Expand Down Expand Up @@ -391,6 +403,8 @@ export function searchReducer(state: SearchReducerState, action: SearchAction):
return {
...state,
filters,
cursor: null,
nextCursor: null,
terms: {
...state.terms,
filter: generateFilterQuery({
Expand Down

0 comments on commit deb1a04

Please sign in to comment.