forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListOf.test.js
More file actions
78 lines (71 loc) · 2.1 KB
/
ListOf.test.js
File metadata and controls
78 lines (71 loc) · 2.1 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
73
74
75
76
77
78
import React from 'react'
import { waitFor, render } from '@testing-library/react'
import { ThemeProvider, theme } from '@chakra-ui/core'
import { ListOf } from '../ListOf'
describe('<ListOf />', () => {
describe('when passed a null value', () => {
it('wraps the return value of the ifEmpty prop in a List', async () => {
const { getByText } = render(
<ThemeProvider theme={theme}>
<ListOf elements={null} ifEmpty={() => <em>nothing</em>}>
{() => <p>something</p>}
</ListOf>
</ThemeProvider>,
)
await waitFor(() => {
expect(getByText('nothing')).toBeInTheDocument()
})
})
})
describe('with an empty array', () => {
it('wraps the return value of the ifEmpty prop in a List', async () => {
const { getByText } = render(
<ThemeProvider theme={theme}>
<ListOf elements={[]} ifEmpty={() => <em>nothing</em>}>
{() => <p>something</p>}
</ListOf>
</ThemeProvider>,
)
await waitFor(() => {
expect(getByText('nothing')).toBeInTheDocument()
})
})
})
describe('with an array of objects', () => {
it('calls the child function once for each object', async () => {
const mock = jest.fn()
render(
<ThemeProvider theme={theme}>
<ListOf
elements={[{ foo: 'foo' }, { bar: 'bar' }]}
ifEmpty={() => <em>nothing</em>}
>
{mock}
</ListOf>
</ThemeProvider>,
)
await waitFor(() => {
expect(mock).toHaveBeenCalledTimes(2)
})
})
it('passes the current element and the index as arguments', async () => {
const mock = jest.fn()
render(
<ThemeProvider theme={theme}>
<ListOf
elements={[{ foo: 'foo' }, { bar: 'bar' }]}
ifEmpty={() => <em>nothing</em>}
>
{mock}
</ListOf>
</ThemeProvider>,
)
await waitFor(() => {
expect(mock.mock.calls).toEqual([
[{ foo: 'foo' }, 0],
[{ bar: 'bar' }, 1],
])
})
})
})
})