Bird
0
0

You want to mock a POST request in Remix tests that returns a 201 status with JSON { success: true }. Which msw handler is correct?

hard📝 Application Q8 of 15
Remix - Testing
You want to mock a POST request in Remix tests that returns a 201 status with JSON { success: true }. Which msw handler is correct?
Arest.post('/api/save', (req, res, ctx) => res(ctx.status(201), ctx.json({ success: true })))
Brest.get('/api/save', (req, res, ctx) => res(ctx.status(201), ctx.json({ success: true })))
Crest.post('/api/save', (req, res) => res.json({ success: true }))
Drest.post('/api/save', (req, res, ctx) => { return { success: true } })
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct HTTP method and response format

    POST method is rest.post; response must use res(ctx.status(201), ctx.json(...)).
  2. Step 2: Check other options for errors

    rest.get('/api/save', (req, res, ctx) => res(ctx.status(201), ctx.json({ success: true }))) uses GET instead of POST; rest.post('/api/save', (req, res) => res.json({ success: true })) misses ctx and status; rest.post('/api/save', (req, res, ctx) => { return { success: true } }) returns plain object, not response.
  3. Final Answer:

    rest.post('/api/save', (req, res, ctx) => res(ctx.status(201), ctx.json({ success: true }))) -> Option A
  4. Quick Check:

    POST mock uses rest.post + res(ctx.status, ctx.json) [OK]
Quick Trick: Use rest.post with res(ctx.status, ctx.json) for POST mocks [OK]
Common Mistakes:
MISTAKES
  • Using wrong HTTP method
  • Not returning response properly
  • Missing ctx in handler

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Remix Quizzes