-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageView.test.tsx
More file actions
38 lines (31 loc) · 1.24 KB
/
Copy pathImageView.test.tsx
File metadata and controls
38 lines (31 loc) · 1.24 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
import { render } from '@testing-library/react'
import { strict as assert } from 'assert'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { getHyperparamSource } from '../../lib/sources/index.js'
import ImageView from './ImageView.js'
globalThis.fetch = vi.fn()
describe('ImageView Component', () => {
beforeEach(() => {
vi.clearAllMocks()
// unnecessary for now because it has only one test, but safer for future tests
})
it('renders the image correctly', async () => {
const body = new ArrayBuffer(8)
vi.mocked(fetch).mockResolvedValueOnce({
arrayBuffer: () => Promise.resolve(body),
headers: new Map([['content-length', body.byteLength]]),
} as unknown as Response)
const source = getHyperparamSource('test.png', { endpoint: 'http://localhost:3000' })
assert(source?.kind === 'file')
const { findByRole, findByText } = render(
<ImageView source={source} setError={vi.fn()} />
)
// wait for asynchronous image loading
expect(fetch).toHaveBeenCalled()
const image = await findByRole('img')
expect(image.getAttribute('src')).not.toBeNull()
expect(image.getAttribute('alt')).toBe('test.png')
await findByText('8 b')
})
// TODO: test error handling
})