-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjest.setup.js
More file actions
72 lines (64 loc) · 2.02 KB
/
Copy pathjest.setup.js
File metadata and controls
72 lines (64 loc) · 2.02 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-disable @next/next/no-img-element */
import '@testing-library/jest-dom';
import 'jest-styled-components';
export const useRouter = jest.fn();
jest.mock('next/navigation', () => ({
useRouter: jest.fn().mockReturnValue({ push: jest.fn(), replace: jest.fn() }),
}));
export const mockUseRouter = props => {
useRouter.mockImplementation(() => ({
basePath: props.basePath || '',
pathname: props.pathname || '/mock-path',
route: props.route || '/mock-route',
asPath: props.asPath || '',
push: props.push || jest.fn(),
replace: props.replace || jest.fn(),
reload: props.reload || jest.fn(),
back: props.back || jest.fn(),
}));
};
// **Global Mocks**
// Due to Jest transformer issues, we mock next-auth's useSession hook directly:
export const mockSession = {
expires: new Date(Date.now() + 2 * 86400).toISOString(),
user: { name: 'admin' },
};
jest.mock('next-auth/react', () => {
const originalModule = jest.requireActual('next-auth/react');
return {
__esModule: true,
...originalModule,
useSession: jest.fn(() => ({
data: mockSession,
status: 'authenticated',
})),
};
});
// Reference: https://github.com/nextauthjs/next-auth/discussions/4185#discussioncomment-2397318
// We also need to mock the whole next-auth package, since it's used in
// our various pages via the `export { getServerSideProps }` function.
jest.mock('next-auth', () => ({
__esModule: true,
default: jest.fn(),
unstable_getServerSession: jest.fn(
() =>
new Promise(resolve => {
resolve({
expiresIn: undefined,
loggedInAt: undefined,
someProp: 'someString',
});
}),
),
}));
// Reference: https://github.com/nextauthjs/next-auth/issues/4866
jest.mock('next/image', () => {
return function MockedImage(props) {
return <img src={props.src} alt={props.alt} {...props} />;
};
});
jest.mock('@/components/calendar', () => ({
__esModule: true,
default: () => <div data-testid="mock-calendario"></div>,
}));
global.fetch = jest.fn();