-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJsonView.test.tsx
More file actions
121 lines (105 loc) · 3.58 KB
/
JsonView.test.tsx
File metadata and controls
121 lines (105 loc) · 3.58 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
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
import { render, waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { FileSource } from '../../lib/sources/types.js'
import JsonView from './JsonView.js'
vi.mock('../../../src/lib/utils.js', async () => {
const actual = await vi.importActual('../../../src/lib/utils.js')
return { ...actual, asyncBufferFrom: vi.fn() }
})
globalThis.fetch = vi.fn()
describe('JsonView Component', () => {
beforeEach(() => {
vi.clearAllMocks()
})
const encoder = new TextEncoder()
it('renders json content as nested tree items (if not collapsed)', async () => {
const text = '{"key":["value"]}'
const body = encoder.encode(text).buffer
const source: FileSource = {
resolveUrl: 'testKey0',
kind: 'file',
fileName: 'testKey0',
sourceId: 'testKey0',
sourceParts: [],
}
vi.mocked(fetch).mockResolvedValueOnce({
status: 200,
headers: new Headers({ 'Content-Length': body.byteLength.toString() }),
text: () => Promise.resolve(text),
} as Response)
const { findAllByRole, findByText } = render(
<JsonView source={source} setError={vi.fn()} />
)
expect(fetch).toHaveBeenCalledWith('testKey0', undefined)
// Wait for asynchronous JSON loading and parsing
await findAllByRole('treeitem')
await findByText('key:')
await findByText('"value"')
})
it('displays an error when the json content is too long', async () => {
const source: FileSource = {
sourceId: 'testKey1',
sourceParts: [],
kind: 'file',
fileName: 'testKey1',
resolveUrl: 'testKey1',
}
vi.mocked(fetch).mockResolvedValueOnce({
status: 200,
headers: new Headers({ 'Content-Length': '8000001' }),
text: () => Promise.resolve(''),
} as Response)
const setError = vi.fn()
render(<JsonView source={source} setError={setError} />)
expect(fetch).toHaveBeenCalledWith('testKey1', undefined)
await waitFor(() => {
expect(setError).toHaveBeenCalledWith(expect.objectContaining({
message: 'File is too large to display',
}))
})
})
it('displays an error when the json content is invalid', async () => {
const body = encoder.encode('INVALIDJSON').buffer
const source: FileSource = {
resolveUrl: 'testKey2',
kind: 'file',
fileName: 'testKey2',
sourceId: 'testKey2',
sourceParts: [],
}
vi.mocked(fetch).mockResolvedValueOnce({
status: 200,
headers: new Headers({ 'Content-Length': body.byteLength.toString() }),
text: () => Promise.resolve('INVALIDJSON'),
} as Response)
const setError = vi.fn()
render(<JsonView source={source} setError={setError} />)
expect(fetch).toHaveBeenCalledWith('testKey2', undefined)
await waitFor(() => {
expect(setError).toHaveBeenCalledWith(expect.objectContaining({
message: expect.stringContaining('Unexpected token') as string,
}))
})
})
it('displays an error when unauthorized', async () => {
const source: FileSource = {
resolveUrl: 'testKey3',
kind: 'file',
fileName: 'testKey3',
sourceId: 'testKey3',
sourceParts: [],
}
vi.mocked(fetch).mockResolvedValueOnce({
status: 401,
text: () => Promise.resolve('Unauthorized'),
} as Response)
const setError = vi.fn()
render(<JsonView source={source} setError={setError} />)
expect(fetch).toHaveBeenCalledWith('testKey3', undefined)
await waitFor(() => {
expect(setError).toHaveBeenCalledWith(expect.objectContaining({
message: 'Unauthorized',
}))
})
})
})