Skip to content

Commit

Permalink
Updated tests for msw@2
Browse files Browse the repository at this point in the history
  • Loading branch information
solarlime committed Oct 27, 2023
1 parent df27293 commit aaed123
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 183 deletions.
2 changes: 0 additions & 2 deletions __mocks__/svg.ts

This file was deleted.

38 changes: 27 additions & 11 deletions __tests__/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
import { rest } from 'msw';
import { http } from 'msw';
import { setupServer } from 'msw/node';
import { render, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import 'cross-fetch/polyfill';
import App from '../src/App';

const url = 'https://api.github.com/repos/user/repo/deployments';
const deployments = [{ id: 123 }, { id: 456 }];
const server = setupServer(
// @ts-ignore
rest.get(url, (req, res, ctx) => res(ctx.json(deployments))),
rest.post(`${url}/:id/statuses`, (req, res, ctx) => {
if (req.headers.get('authorization')?.includes('notmytoken')) {
return res(ctx.status(404), ctx.body('Not found'));
http.get(url, () => new Response(JSON.stringify(deployments), {
headers: {
'Content-Type': 'application/json',
},
})),
http.post(`${url}/:id/statuses`, async ({ request }) => {
const headers = await request.headers;
if (headers.get('authorization')?.includes('notmytoken')) {
return new Response(JSON.stringify('Not found'), {
status: 404,
});
}
return res(ctx.json({ success: true }));
return new Response(JSON.stringify({ success: true }), {
headers: {
'Content-Type': 'application/json',
},
});
}),
// @ts-ignore
rest.delete(`${url}/:id`, (req, res, ctx) => res(ctx.json({ success: true }))),
http.delete(`${url}/:id`, () => new Response(JSON.stringify({ success: true }), {
headers: {
'Content-Type': 'application/json',
},
})),
);

beforeAll(() => server.listen());
Expand Down Expand Up @@ -54,8 +67,11 @@ describe.each`
const button = getByRole('button', { name: 'Clean repository deployments' });
if (time === 2) {
server.use(
// @ts-ignore
rest.get(url, (req, res, ctx) => res.once(ctx.json([]))),
http.get(url, () => new Response(JSON.stringify([]), {
headers: {
'Content-Type': 'application/json',
},
}), { once: true }),
);
}
await user.click(button);
Expand Down
Loading

0 comments on commit aaed123

Please sign in to comment.