Im currently trying to write jest test for my RTKQuery, but I get stuck on the authentication level for the test.
Basically the api Im using is designed to have the token on query param instead of having it on the request header: "https://api/v1/something/meta/?token=userToken"
So when I try to test the api call it shows me the request has been rejected. Does anyone know how to write the test with this case?
here is my RTKQuery endpoint:
// index.ts
export const rootApi = createApi({
reducerPath: "root",
baseQuery: fetchBaseQuery({baseUrl: API_ROOT}),
endpoints: () => ({});
})
// dataEndpoint.ts
const token = getToken(); // Gets the user's token from localStorage after user login
export cosnt apiWithData = rootApi.injectEndpoints({
endpoints: (build) => ({
fetchDataMetaList: build.mutation<DataType, any>({
query: ({offset = 0, size = 20, body}) => ({
// token is passed in for query param
url: `${API_URL}?offset=${offset}&size=${size}&token=${token}`,
method: "POST",
body: body || {}
})
})
})
})
below is my test:
// data.test.tsx
const body = { offset: 0, size: 20, body: {} };
const updateTimeout = 10000;
beforeEach((): void => {
fetchMock.resetMocks();
})
const wrapper: React.FC = ({ children }) => {
const storeRef = setupApiStore(rootApi);
return <Provider store={storeRef.store}>{children}</Provider>
}
describe("useFetchDataMetaListMutation", () => {
it("Success", async () => {
fetchMock.mockResponse(JSON.string(response));
cosnt { result, waitForNextupdate } = renderHook(
() => useFetchDataMetaListMutation(),
{ wrapper }
)
const [fetchDataMetaList, initialResponse] = result.current;
expect(initialResponse.data).toBeUndefined();
expect(initialResponse.isLoading).toBe(false);
act(() => {
void fetchDataMetaList(body);
})
const loadingResponse = result.current[1];
expect(loadingResponse.data).toBeUndefined();
expect(loadingResponse.isLoading).toBe(true);
// Up til this point everything is passing fine
await waitForNextUpdate({ timeout: updateTimeout });
const loadedResponse = result.current[1];
// expect loadedResponse.data to be defined, but returned undefined
// console out put for loaded Response status is 'rejected' with 401 access level
// error code
})
})