How do I handle difference responses to the same endpoint? #885
Unanswered
CodyBontecou
asked this question in
Q&A
Replies: 2 comments 12 replies
-
I found the permanent override. Is this the proper solution? Write my handlers within the test file itself? |
Beta Was this translation helpful? Give feedback.
1 reply
-
Hey, @CodyBontecou. Please use the runtime handlers in the combination with resetting handlers between tests. That way you don't have to use const server = setupServer(
rest.get('/resource', (req, res, ctx) => res(ctx.text('hello')))
)
beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
test('happy path', () => {
// make request.
})
test('error stat', () => {
server.use(
rest.get('/resource', (req, res, ctx) => res(ctx.status(500)))
)
// make request
})
test('another state', () => {
server.use(
// Model any response overrides you need.
rest.get('/resource', (req, res, ctx) => res(ctx.status(403)))
)
// make request
}) This setup is also illustrated in the React Testing Library example. |
Beta Was this translation helpful? Give feedback.
11 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'd like to test views when given no data, errors, and expected data.
I can create the handler to manage a single response, but what's the best way to manage multiple responses for the same endpoint depending on the state I'm trying to test?
Beta Was this translation helpful? Give feedback.
All reactions