/
index.spec.tsx
64 lines (50 loc) · 1.63 KB
/
index.spec.tsx
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
import React, { ComponentProps } from 'react'
import { render, waitFor, act } from '@testing-library/react'
import { useTopService } from '@/services/templates/hooks/useTopService'
import { Top } from '@/components/templates/Top'
import { testComponentProps } from '@/jest/utils/testComponentProps'
import { TopContainer } from '.'
jest.mock('@/services/templates/hooks/useTopService', () => ({
useTopService: jest.fn(),
}))
jest.mock('@/components/templates/Top', () => ({
Top: jest.fn().mockImplementation(() => 'Top Component'),
}))
const useTopServiceMock = useTopService as jest.Mock
const TopMock = Top as jest.Mock
describe('Top Container', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('should render Top Component on success', async () => {
useTopServiceMock.mockReturnValue({
data: {},
loading: false,
error: undefined,
})
render(<TopContainer />)
testComponentProps(TopMock, async (props: ComponentProps<typeof Top>) => {
expect(props.isClicked).toBe(false)
expect(TopMock).toBeCalledTimes(1)
act(() => {
props.onClick()
})
await waitFor(() => [props.isClicked])
expect(TopMock).toBeCalledTimes(2)
expect(props.isClicked).toBe(true)
})
})
it('should throw errors that occur in useTopService', () => {
useTopServiceMock.mockReturnValue({
data: undefined,
loading: false,
errors: [new Error('useTopService Error')],
})
try {
render(<TopContainer />)
} catch (error) {
expect(error).toEqual([new Error('useTopService Error')])
expect(TopMock).toBeCalledTimes(0)
}
})
})