forked from TanStack/query
/
Explorer.test.tsx
144 lines (117 loc) · 3.98 KB
/
Explorer.test.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { fireEvent, render, screen, act } from '@testing-library/react'
import * as React from 'react'
import { chunkArray, CopyButton, DefaultRenderer } from '../Explorer'
import { displayValue } from '../utils'
describe('Explorer', () => {
describe('chunkArray', () => {
it('when the size is less than one return an empty array', () => {
expect(chunkArray([1, 2, 3], 0)).toStrictEqual([])
})
it('when the array is empty return an empty array', () => {
expect(chunkArray([], 2)).toStrictEqual([])
})
it('when the array is evenly chunked return full chunks ', () => {
expect(chunkArray([1, 2, 3, 4], 2)).toStrictEqual([
[1, 2],
[3, 4],
])
})
it('when the array is not evenly chunkable by size the last item is the remaining elements ', () => {
const chunks = chunkArray([1, 2, 3, 4, 5], 2)
const lastChunk = chunks[chunks.length - 1]
expect(lastChunk).toStrictEqual([5])
})
})
describe('DefaultRenderer', () => {
it('when the entry label is clicked, toggle expanded', async () => {
const toggleExpanded = jest.fn()
render(
<DefaultRenderer
label="the top level label"
toggleExpanded={toggleExpanded}
pageSize={10}
expanded={false}
copyable={false}
subEntryPages={[[{ label: 'A lovely label' }]]}
handleEntry={() => <></>}
value={undefined}
subEntries={[]}
type="string"
/>,
)
const expandButton = screen.getByRole('button', {
name: /▶ the top level label 0 item/i,
})
fireEvent.click(expandButton)
expect(toggleExpanded).toHaveBeenCalledTimes(1)
})
it('when the entry label is clicked, toggle expanded', async () => {
// Mock clipboard
let clipBoardContent = null
const value = 'someValue'
Object.defineProperty(navigator, 'clipboard', {
value: {
writeText: async () => {
return new Promise(() => (clipBoardContent = value))
},
},
configurable: true,
})
act(() => {
render(<CopyButton value={value} />)
})
// After rendering the clipboard content should be null
expect(clipBoardContent).toBe(null)
const copyButton = screen.getByRole('button')
await screen.findByLabelText('Copy object to clipboard')
// After clicking the content should be added to the clipboard
act(() => {
fireEvent.click(copyButton)
})
expect(clipBoardContent).toBe(value)
})
it('when the entry label is clicked, toggle expanded', async () => {
// Mock clipboard with error state
Object.defineProperty(navigator, 'clipboard', {
value: {
writeText: async () => {
return new Promise(() => {
throw Error
})
},
},
configurable: true,
})
act(() => {
render(<CopyButton value={'someValue'} />)
})
const copyButton = screen.getByRole('button')
await screen.findByLabelText('Copy object to clipboard')
// After clicking the content should NOT be added to the clipboard
await act(async () => {
fireEvent.click(copyButton)
await new Promise(process.nextTick)
})
// Check that it has failed
await screen.findByLabelText('Failed copying to clipboard')
})
})
describe('displayValue', () => {
it('when the value is a boolean', () => {
expect(displayValue(true)).toBe('true')
})
it('when the value is a BigInt', () => {
expect(displayValue(BigInt(1))).toBe('"1"')
})
it('when the value is an Error', () => {
expect(displayValue(new Error('err'))).toBe(
'{"name":"Error","message":"err"}',
)
})
it('when the value is an object', () => {
expect(displayValue({ error: new Error('err'), bigint: 1n })).toBe(
'{"error":{"name":"Error","message":"err"},"bigint":"1"}',
)
})
})
})