Skip to content

Commit

Permalink
Adds async default transformer integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 21, 2020
1 parent bc0d38f commit d526e8e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
23 changes: 22 additions & 1 deletion test/msw-api/context/async-response-transformer.mocks.ts
@@ -1,4 +1,11 @@
import { ResponseTransformer, setupWorker, rest, context, compose } from 'msw'
import {
ResponseTransformer,
setupWorker,
rest,
context,
compose,
createResponseComposition,
} from 'msw'
import base64Image from 'url-loader!../../fixtures/image.jpg'

async function jpeg(base64: string): Promise<ResponseTransformer> {
Expand All @@ -11,10 +18,24 @@ async function jpeg(base64: string): Promise<ResponseTransformer> {
)
}

const customResponse = createResponseComposition(null, [
async (res) => {
res.statusText = 'Custom Status Text'
return res
},
async (res) => {
res.headers.set('x-custom', 'yes')
return res
},
])

const worker = setupWorker(
rest.get('/image', async (req, res, ctx) => {
return res(ctx.status(201), await jpeg(base64Image))
}),
rest.post('/search', (req, res, ctx) => {
return customResponse(ctx.status(301))
}),
)

worker.start()
22 changes: 22 additions & 0 deletions test/msw-api/context/async-response-transformer.test.ts
Expand Up @@ -27,3 +27,25 @@ test('supports asynchronous response transformer', async () => {

return runtime.cleanup()
})

test('supports asynchronous default response transformer', async () => {
const runtime = await runBrowserWith(
path.resolve(__dirname, 'async-response-transformer.mocks.ts'),
)

const res = await runtime.request({
url: `${runtime.origin}/search`,
fetchOptions: {
method: 'POST',
},
})
const status = res.status()
const statusText = res.statusText()
const headers = res.headers()

expect(status).toBe(301)
expect(statusText).toBe('Custom Status Text')
expect(headers).toHaveProperty('x-custom', 'yes')

return runtime.cleanup()
})

0 comments on commit d526e8e

Please sign in to comment.