Skip to content

Commit 00f9261

Browse files
Write test to expose bug on cache invalidation of queries that use serializeQueryArgs
1 parent bdf8af3 commit 00f9261

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

packages/toolkit/src/query/tests/createApi.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,13 @@ describe('custom serializeQueryArgs per endpoint', () => {
876876
},
877877
}
878878

879+
const TagTypes = {
880+
ListItemsTag: 'ListItemsTag',
881+
} as const;
882+
879883
const api = createApi({
880884
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
885+
tagTypes: Object.values(TagTypes),
881886
serializeQueryArgs: ({ endpointName, queryArgs }) =>
882887
`base-${endpointName}-${queryArgs}`,
883888
endpoints: (build) => ({
@@ -944,6 +949,16 @@ describe('custom serializeQueryArgs per endpoint', () => {
944949
return currentArg !== previousArg
945950
},
946951
}),
952+
listItems3: build.query<string[], number>({
953+
query: (pageNumber) => {
954+
console.log(pageNumber);
955+
return `/listItems3?page=${pageNumber}`
956+
},
957+
serializeQueryArgs: ({ endpointName }) => {
958+
return endpointName
959+
},
960+
providesTags: [TagTypes.ListItemsTag],
961+
}),
947962
}),
948963
})
949964

@@ -1083,4 +1098,35 @@ describe('custom serializeQueryArgs per endpoint', () => {
10831098
baseQueryMeta: expect.any(Object),
10841099
})
10851100
})
1101+
1102+
test('serializeQueryArgs allows refetching as args change with same cache key if invalidated', async () => {
1103+
const allItems = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'i']
1104+
const PAGE_SIZE = 3
1105+
1106+
server.use(
1107+
rest.get('https://example.com/listItems3', (req, res, ctx) => {
1108+
const pageString = req.url.searchParams.get('page')
1109+
const pageNum = parseInt(pageString || '0')
1110+
1111+
const results = paginate(allItems, PAGE_SIZE, pageNum)
1112+
return res(ctx.json(results))
1113+
})
1114+
)
1115+
1116+
// Page number shouldn't matter here, because the cache key ignores that.
1117+
// We just need to select the only cache entry.
1118+
const selectListItems = api.endpoints.listItems3.select(0)
1119+
1120+
await storeRef.store.dispatch(api.endpoints.listItems3.initiate(1))
1121+
1122+
const initialEntry = selectListItems(storeRef.store.getState())
1123+
expect(initialEntry.data).toEqual(['a', 'b', 'c'])
1124+
1125+
await storeRef.store.dispatch(api.util.invalidateTags([TagTypes.ListItemsTag]));
1126+
1127+
// TODO: due to bug, the call to listItems3 still uses `1` instead of `2` as expected
1128+
await storeRef.store.dispatch(api.endpoints.listItems3.initiate(2))
1129+
const updatedEntry = selectListItems(storeRef.store.getState())
1130+
expect(updatedEntry.data).toEqual(['a', 'b', 'c', 'd', 'e', 'f'])
1131+
})
10861132
})

0 commit comments

Comments
 (0)