Skip to content

Commit

Permalink
refactor(mocks): update MSW handlers to use new HttpResponse API
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Dec 31, 2024
1 parent 5517c8e commit 4f6bd1c
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import { http } from 'msw';
import { http, HttpResponse } from 'msw';

const handlers = [
http.post('/login', async (_req, res, ctx) => {
http.post('/login', async () => {
// Persist user's authentication in the session
sessionStorage.setItem('is-authenticated', 'true');
return res(
// Respond with a 200 status code
ctx.status(200),
);

return new HttpResponse(null, { status: 200 });
}),
http.get('/user', async (_req, res, ctx) => {
http.get('/user', async () => {
// Check if the user is authenticated in this session
const isAuthenticated = sessionStorage.getItem('is-authenticated');
if (!isAuthenticated) {
// If not authenticated, respond with a 403 error
return res(
ctx.status(403),
ctx.json({
return HttpResponse.json(
{
errorMessage: 'Not authorized',
}),
},
{ status: 403 },
);
}

// If authenticated, return a mocked user details
return res(
ctx.status(200),
ctx.json({
username: 'admin',
}),

return HttpResponse.json(
{ username: 'admin' },
{
status: 200,
},
);
}),
];
Expand Down

0 comments on commit 4f6bd1c

Please sign in to comment.