Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Oct 14, 2022
1 parent 3578340 commit 00af454
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/fetchBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export type FetchBaseQueryArgs = {
BaseQueryApi,
'getState' | 'extra' | 'endpoint' | 'type' | 'forced'
>
) => MaybePromise<Headers> | void
) => MaybePromise<Headers | void>
fetchFn?: (
input: RequestInfo,
init?: RequestInit | undefined
Expand Down
41 changes: 41 additions & 0 deletions packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,26 @@ describe('fetchBaseQuery', () => {
expect(request.headers['delete2']).toBeUndefined()
})

test('prepareHeaders can return undefined', async () => {
let request: any

const token = 'accessToken'

const _baseQuery = fetchBaseQuery({
baseUrl,
prepareHeaders: (headers) => {
headers.set('authorization', `Bearer ${token}`)
},
})

const doRequest = async () =>
_baseQuery({ url: '/echo' }, commonBaseQueryApi, {})

;({ data: request } = await doRequest())

expect(request.headers['authorization']).toBe(`Bearer ${token}`)
})

test('prepareHeaders is able to be an async function', async () => {
let request: any

Expand All @@ -679,6 +699,27 @@ describe('fetchBaseQuery', () => {
expect(request.headers['authorization']).toBe(`Bearer ${token}`)
})

test('prepareHeaders is able to be an async function returning undefined', async () => {
let request: any

const token = 'accessToken'
const getAccessTokenAsync = async () => token

const _baseQuery = fetchBaseQuery({
baseUrl,
prepareHeaders: async (headers) => {
headers.set('authorization', `Bearer ${await getAccessTokenAsync()}`)
},
})

const doRequest = async () =>
_baseQuery({ url: '/echo' }, commonBaseQueryApi, {})

;({ data: request } = await doRequest())

expect(request.headers['authorization']).toBe(`Bearer ${token}`)
})

test('prepareHeaders is able to select from a state', async () => {
let request: any

Expand Down

0 comments on commit 00af454

Please sign in to comment.