-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImportFrameworkDialog.test.tsx
More file actions
104 lines (82 loc) · 3.98 KB
/
Copy pathImportFrameworkDialog.test.tsx
File metadata and controls
104 lines (82 loc) · 3.98 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
import { describe, it, expect, vi } from 'vitest'
import '@testing-library/jest-dom'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import ImportFrameworkDialog, { type ImportResult } from './ImportFrameworkDialog'
const importResult: ImportResult = { status: 'imported', id: 'doc-1', version: 1 }
describe('ImportFrameworkDialog', () => {
it('imports from a URL by default', async () => {
const onImport = vi.fn().mockResolvedValue(importResult)
const onImportJson = vi.fn()
render(
<ImportFrameworkDialog open onCancel={vi.fn()} onImport={onImport} onImportJson={onImportJson} />,
)
fireEvent.change(screen.getByLabelText('Framework URL'), {
target: { value: 'https://case.example.org/ims/case/v1p1/CFPackages/doc-1' },
})
fireEvent.click(screen.getByRole('button', { name: /import framework/i }))
await waitFor(() => expect(onImport).toHaveBeenCalledWith(
'https://case.example.org/ims/case/v1p1/CFPackages/doc-1',
undefined,
))
expect(onImportJson).not.toHaveBeenCalled()
})
it('switches to Paste JSON mode and imports a parsed CFPackage', async () => {
const onImport = vi.fn()
const onImportJson = vi.fn().mockResolvedValue(importResult)
render(
<ImportFrameworkDialog open onCancel={vi.fn()} onImport={onImport} onImportJson={onImportJson} />,
)
fireEvent.click(screen.getByRole('button', { name: 'Paste JSON' }))
const cfPackage = { CFDocument: { identifier: 'doc-1' } }
fireEvent.change(screen.getByLabelText('Framework JSON'), {
target: { value: JSON.stringify(cfPackage) },
})
fireEvent.click(screen.getByRole('button', { name: /import framework/i }))
await waitFor(() => expect(onImportJson).toHaveBeenCalledWith(cfPackage))
expect(onImport).not.toHaveBeenCalled()
})
it('shows an inline error for malformed JSON without calling onImportJson', async () => {
const onImport = vi.fn()
const onImportJson = vi.fn()
render(
<ImportFrameworkDialog open onCancel={vi.fn()} onImport={onImport} onImportJson={onImportJson} />,
)
fireEvent.click(screen.getByRole('button', { name: 'Paste JSON' }))
fireEvent.change(screen.getByLabelText('Framework JSON'), {
target: { value: '{ not valid json' },
})
fireEvent.click(screen.getByRole('button', { name: /import framework/i }))
expect(await screen.findByText(/doesn.t look like valid json/i)).toBeInTheDocument()
expect(onImportJson).not.toHaveBeenCalled()
expect(onImport).not.toHaveBeenCalled()
})
it('surfaces validation warnings returned from a successful import', async () => {
const onImport = vi.fn()
const onImportJson = vi.fn().mockResolvedValue({
...importResult,
validationWarnings: ['CFDocument.title is required'],
})
render(
<ImportFrameworkDialog open onCancel={vi.fn()} onImport={onImport} onImportJson={onImportJson} />,
)
fireEvent.click(screen.getByRole('button', { name: 'Paste JSON' }))
fireEvent.change(screen.getByLabelText('Framework JSON'), {
target: { value: '{ "CFDocument": { "identifier": "doc-1" } }' },
})
fireEvent.click(screen.getByRole('button', { name: /import framework/i }))
expect(await screen.findByText('CFDocument.title is required')).toBeInTheDocument()
})
it('surfaces a thrown error from onImportJson', async () => {
const onImport = vi.fn()
const onImportJson = vi.fn().mockRejectedValue(new Error('import_failed: Schema validation failed'))
render(
<ImportFrameworkDialog open onCancel={vi.fn()} onImport={onImport} onImportJson={onImportJson} />,
)
fireEvent.click(screen.getByRole('button', { name: 'Paste JSON' }))
fireEvent.change(screen.getByLabelText('Framework JSON'), {
target: { value: '{ "CFDocument": { "identifier": "doc-1" } }' },
})
fireEvent.click(screen.getByRole('button', { name: /import framework/i }))
expect(await screen.findByText('import_failed: Schema validation failed')).toBeInTheDocument()
})
})