Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

Explorer.test.tsx

68 lines1.9 KB

1import React from 'react'
2import { fireEvent, render, screen } from '@testing-library/react'
3​
4import { chunkArray, DefaultRenderer } from '../Explorer'
5import { displayValue } from '../utils'
6​
7describe('Explorer', () => {
8 describe('chunkArray', () => {
9 it('when the size is less than one return an empty array', () => {
10 expect(chunkArray([1, 2, 3], 0)).toStrictEqual([])
11 })
12​
13 it('when the array is empty return an empty array', () => {
14 expect(chunkArray([], 2)).toStrictEqual([])
15 })
16​
17 it('when the array is evenly chunked return full chunks ', () => {
18 expect(chunkArray([1, 2, 3, 4], 2)).toStrictEqual([
19 [1, 2],
20 [3, 4],
21 ])
22 })
23​
24 it('when the array is not evenly chunkable by size the last item is the remaining elements ', () => {
25 const chunks = chunkArray([1, 2, 3, 4, 5], 2)
26 const lastChunk = chunks[chunks.length - 1]
27 expect(lastChunk).toStrictEqual([5])
28 })
29 })
30​
31 describe('DefaultRenderer', () => {
32 it('when the entry label is clicked, toggle expanded', async () => {
33 const toggleExpanded = jest.fn()
34​
35 render(
36 <DefaultRenderer
37 label="the top level label"
38 toggleExpanded={toggleExpanded}
39 pageSize={10}
40 expanded={false}
41 subEntryPages={[[{ label: 'A lovely label' }]]}
42 HandleEntry={() => <></>}
43 value={undefined}
44 subEntries={[]}
45 type="string"
46 />
47 )
48​
49 const expandButton = screen.getByRole('button', {
50 name: /β–Ά the top level label 0 item/i,
51 })
52​
53 fireEvent.click(expandButton)
54​
55 expect(toggleExpanded).toHaveBeenCalledTimes(1)
56 })
57 })
58​
59 describe('displayValue', () => {
60 it('when the value is a boolean', () => {
61 expect(displayValue(true)).toBe('true')
62 })
63​
64 it('when the value is a BigInt', () => {
65 expect(displayValue(BigInt(1))).toBe('"1n"')
66 })
67 })
68})