-
Notifications
You must be signed in to change notification settings - Fork 1
/
jest.setup.ts
60 lines (52 loc) · 1.5 KB
/
jest.setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* Core */
import React from 'react';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import env from 'dotenv';
import '@testing-library/jest-dom';
import 'whatwg-fetch';
import 'jest-styled-components';
env.config({ path: './.env.test' });
const API_URL = process.env.NEXT_PUBLIC_API_URL;
const server = setupServer(
rest.post(`${API_URL}/api/checkout`, (req, res, ctx) => {
return res(ctx.json({ message: 'Success...' }));
}),
rest.get(`${API_URL}/api/category`, (req, res, ctx) => {
return res(ctx.json({ message: 'Success...' }));
}),
);
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterAll(() => server.close());
afterEach(() => server.resetHandlers());
const push = jest.fn((pathname: string) => {
global.window = Object.create(window);
Object.defineProperty(window, 'location', { value: { pathname } });
Promise.resolve(true);
});
jest.mock('next/router', () => ({
__esModule: true,
useRouter: () => ({
query: {},
pathname: '/',
asPath: '/',
events: {
emit: jest.fn(),
on: jest.fn(),
off: jest.fn(),
},
push,
prefetch: jest.fn(() => Promise.resolve(true)),
replace: jest.fn(() => Promise.resolve(true)),
}),
}));
/* Types */
interface Mocks {
push: typeof push;
}
declare global {
// eslint-disable-next-line vars-on-top, no-var
var mocks: Mocks;
}
global.mocks = { push };
global.React = React;