-
-
Notifications
You must be signed in to change notification settings - Fork 802
/
pages.spec.js
112 lines (94 loc) 路 3.06 KB
/
pages.spec.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import renderer from 'react-test-renderer'
import { enableFetchMocks } from 'jest-fetch-mock'
import App from '../pages/_app'
import Index from '../pages/index'
import About from '../pages/about'
import Contributors, {
getStaticProps as getContributorsStaticProps,
} from '../pages/contributors'
import RelatedTools from '../pages/related-tools'
import GitmojisApi from '../pages/api/gitmojis'
import gitmojisData from '../data/gitmojis.json'
import * as stubs from './stubs'
jest.mock('next/router', () => ({
query: {},
events: { on: jest.fn(), off: jest.fn() },
useRouter: jest.fn().mockImplementation(() => ({
query: {},
})),
}))
describe('Pages', () => {
describe('App', () => {
beforeAll(() => {
Math.random = jest.fn().mockReturnValue(1)
})
it('should render the page', () => {
const wrapper = renderer.create(<App {...stubs.appProps} />)
expect(wrapper).toMatchSnapshot()
})
})
describe('Index', () => {
it('should render the page', () => {
const wrapper = renderer.create(<Index />)
expect(wrapper).toMatchSnapshot()
})
})
describe('About', () => {
it('should render the page', () => {
const wrapper = renderer.create(<About />)
expect(wrapper).toMatchSnapshot()
})
})
describe('Contributors', () => {
beforeAll(() => {
enableFetchMocks()
})
it('should fetch contributos from GitHub', async () => {
fetch.mockResponseOnce(JSON.stringify(stubs.contributorsMock))
const props = await getContributorsStaticProps()
expect(props).toEqual({
props: {
contributors: stubs.contributors,
},
revalidate: 3600 * 3,
})
})
it('should render the page', () => {
const wrapper = renderer.create(
<Contributors contributors={stubs.contributors} />
)
expect(wrapper).toMatchSnapshot()
})
})
describe('Related tools', () => {
it('should render the page', () => {
const wrapper = renderer.create(<RelatedTools />)
expect(wrapper).toMatchSnapshot()
})
})
describe('Api', () => {
describe('gitmojis endpoint', () => {
describe('when request method is GET', () => {
it('should set response status to 200 and gitmojis as body json', () => {
const request = stubs.request('GET')
const response = stubs.response()
GitmojisApi(request, response)
expect(response.status).toHaveBeenCalledWith(200)
expect(response.json).toHaveBeenCalledWith(gitmojisData)
})
})
describe('when request method is not GET', () => {
it('should setHeader, status 405 and end the request', () => {
const request = stubs.request('POST')
const response = stubs.response()
GitmojisApi(request, response)
expect(response.setHeader).toHaveBeenCalledWith('Allow', ['GET'])
expect(response.status).toHaveBeenCalledWith(405)
expect(response.json).toHaveBeenCalledWith({
error: `Error: method POST not allowed`,
})
})
})
})
})
})