Skip to content

Commit 10af6a5

Browse files
committed
clear mocks, fix test, replace fireEvent with userEvent
1 parent 9768060 commit 10af6a5

File tree

8 files changed

+66
-34
lines changed

8 files changed

+66
-34
lines changed

src/components/File/File.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { render } from '@testing-library/react'
22
import { strict as assert } from 'assert'
33
import { act } from 'react'
4-
import { describe, expect, it, vi } from 'vitest'
4+
import { beforeEach, describe, expect, it, vi } from 'vitest'
55
import { Config, ConfigProvider } from '../../hooks/useConfig.js'
66
import { getHttpSource, getHyperparamSource } from '../../lib/sources/index.js'
77
import File from './File.js'
@@ -21,6 +21,10 @@ const headers = { get: vi.fn() }
2121
globalThis.fetch = vi.fn(() => Promise.resolve({ text, headers } as unknown as Response))
2222

2323
describe('File Component', () => {
24+
beforeEach(() => {
25+
vi.clearAllMocks()
26+
})
27+
2428
it('renders a local file path', async () => {
2529
text.mockResolvedValueOnce('test content')
2630
const source = getHyperparamSource('folder/subfolder/test.txt', { endpoint })

src/components/Folder/Folder.test.tsx

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { fireEvent, render, waitFor } from '@testing-library/react'
1+
import { render, waitFor } from '@testing-library/react'
22
import { strict as assert } from 'assert'
33
import { act } from 'react'
4-
import { describe, expect, it, test, vi } from 'vitest'
4+
import { beforeEach, describe, expect, it, test, vi } from 'vitest'
55
import { Config, ConfigProvider } from '../../hooks/useConfig.js'
66
import { DirSource, FileMetadata, HyperparamFileMetadata, getHyperparamSource } from '../../lib/sources/index.js'
77
import Folder from './Folder.js'
@@ -22,6 +22,10 @@ const config: Config = {
2222
globalThis.fetch = vi.fn()
2323

2424
describe('Folder Component', () => {
25+
beforeEach(() => {
26+
vi.clearAllMocks()
27+
})
28+
2529
test.for([
2630
'',
2731
'subfolder/',
@@ -100,19 +104,16 @@ describe('Folder Component', () => {
100104

101105
// Type a search query
102106
const searchInput = getByPlaceholderText('Search...') as HTMLInputElement
103-
act(() => {
104-
fireEvent.keyUp(searchInput, { target: { value: 'file1' } })
105-
})
107+
const user = userEvent.setup()
108+
await user.type(searchInput, 'file1')
106109

107110
// Only matching files are displayed
108111
await findByText('file1.txt')
109112
expect(queryByText('folder1/')).toBeNull()
110113
expect(queryByText('report.pdf')).toBeNull()
111114

112115
// Clear search with escape key
113-
act(() => {
114-
fireEvent.keyUp(searchInput, { key: 'Escape' })
115-
})
116+
await user.type(searchInput, '{Escape}')
116117

117118
await findByText('report.pdf')
118119
getByText('folder1/')
@@ -149,13 +150,16 @@ describe('Folder Component', () => {
149150
expect(location.href).toBe('/files?key=file1.txt')
150151
})
151152

152-
it('jumps to search box when user types /', async () => {
153+
it('jumps to search box when user types / while the body is focused', async () => {
153154
const dirSource: DirSource = {
154155
sourceId: 'test-source',
155156
sourceParts: [{ text: 'test-source', sourceId: 'test-source' }],
156157
kind: 'directory',
157158
prefix: '',
158-
listFiles: () => Promise.resolve([]),
159+
listFiles: async () => {
160+
await fetch('something') // to ensure we wait for loading
161+
return []
162+
},
159163
}
160164
const { getByPlaceholderText } = render(<Folder source={dirSource} />)
161165

@@ -165,30 +169,33 @@ describe('Folder Component', () => {
165169
})
166170

167171
const searchInput = getByPlaceholderText('Search...') as HTMLInputElement
172+
const user = userEvent.setup()
168173

169-
// Typing / should focus the search box
170-
act(() => {
171-
fireEvent.keyDown(document.body, { key: '/' })
172-
})
174+
// By default, the search box is already focused in this test
173175
expect(document.activeElement).toBe(searchInput)
174176

175177
// Typing inside the search box should work including /
176-
act(() => {
177-
fireEvent.keyUp(searchInput, { target: { value: 'file1/' } })
178-
})
178+
await user.type(searchInput, 'file1/')
179179
expect(searchInput.value).toBe('file1/')
180180

181181
// Unfocus and re-focus should select all text in search box
182182
act(() => {
183183
searchInput.blur()
184184
})
185185
expect(document.activeElement).not.toBe(searchInput)
186+
expect(document.activeElement).toBe(document.body)
186187

187-
act(() => {
188-
fireEvent.keyDown(document.body, { key: '/' })
189-
})
188+
await user.keyboard('/')
190189
expect(document.activeElement).toBe(searchInput)
191190
expect(searchInput.selectionStart).toBe(0)
192191
expect(searchInput.selectionEnd).toBe(searchInput.value.length)
192+
193+
// Focus another element and try again: it does not focus the search box
194+
await user.tab()
195+
expect(document.activeElement).not.toBe(searchInput)
196+
expect(document.activeElement).not.toBe(document.body)
197+
198+
await user.keyboard('/')
199+
expect(document.activeElement).not.toBe(searchInput)
193200
})
194201
})

src/components/ImageView/ImageView.test.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { render } from '@testing-library/react'
22
import { strict as assert } from 'assert'
3-
import { describe, expect, it, vi } from 'vitest'
3+
import { beforeEach, describe, expect, it, vi } from 'vitest'
44
import { getHyperparamSource } from '../../lib/sources/index.js'
55
import ImageView from './ImageView.js'
66

77
globalThis.fetch = vi.fn()
88

99
describe('ImageView Component', () => {
10+
beforeEach(() => {
11+
vi.clearAllMocks()
12+
// unnecessary for now because it has only one test, but safer for future tests
13+
})
14+
1015
it('renders the image correctly', async () => {
1116
const body = new ArrayBuffer(8)
1217
vi.mocked(fetch).mockResolvedValueOnce({

src/components/JsonView/JsonView.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { render, waitFor } from '@testing-library/react'
2-
import { describe, expect, it, vi } from 'vitest'
2+
import { beforeEach, describe, expect, it, vi } from 'vitest'
33
import { FileSource } from '../../lib/sources/types.js'
44
import JsonView from './JsonView.js'
55

@@ -11,6 +11,10 @@ vi.mock('../../../src/lib/utils.js', async () => {
1111
globalThis.fetch = vi.fn()
1212

1313
describe('JsonView Component', () => {
14+
beforeEach(() => {
15+
vi.clearAllMocks()
16+
})
17+
1418
const encoder = new TextEncoder()
1519

1620
it('renders json content as nested tree items (if not collapsed)', async () => {

src/components/MarkdownView/MarkdownView.test.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import { render } from '@testing-library/react'
22
import { strict as assert } from 'assert'
3-
import { describe, expect, it, vi } from 'vitest'
3+
import { beforeEach, describe, expect, it, vi } from 'vitest'
44
import { getHyperparamSource } from '../../lib/sources/index.js'
55
import MarkdownView from './MarkdownView.js'
66

77
globalThis.fetch = vi.fn()
88

99
describe('MarkdownView Component', () => {
10+
beforeEach(() => {
11+
vi.clearAllMocks()
12+
// unnecessary for now because it has only one test, but safer for future tests
13+
})
14+
1015
it('renders markdown correctly', async () => {
1116
const text = '# Markdown\n\nThis is a test of the markdown viewer.'
1217
vi.mocked(fetch).mockResolvedValueOnce({

src/components/Welcome/Welcome.test.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { fireEvent, render } from '@testing-library/react'
2-
import { describe, expect, it, vi } from 'vitest'
2+
import { beforeEach, describe, expect, it, vi } from 'vitest'
33
import { ConfigProvider } from '../../hooks/useConfig.js'
44
import Welcome from './Welcome.js'
55

6+
const onClose = vi.fn()
7+
68
describe('Welcome Component', () => {
9+
beforeEach(() => {
10+
vi.clearAllMocks()
11+
})
12+
713
it('renders welcome content', () => {
8-
const onClose = vi.fn()
914
const { getByRole, getByText } = render(<Welcome onClose={onClose} />)
1015

1116
getByText('npx hyperparam')
@@ -15,15 +20,13 @@ describe('Welcome Component', () => {
1520
})
1621

1722
it('calls onClose when button is clicked', () => {
18-
const onClose = vi.fn()
1923
const { getByRole } = render(<Welcome onClose={onClose} />)
2024

2125
fireEvent.click(getByRole('button'))
2226
expect(onClose).toHaveBeenCalledTimes(1)
2327
})
2428

2529
it('calls onClose when clicking outside the popup', () => {
26-
const onClose = vi.fn()
2730
const { getByRole } = render(<Welcome onClose={onClose} />)
2831

2932
// Find the backdrop element
@@ -34,7 +37,6 @@ describe('Welcome Component', () => {
3437
})
3538

3639
it('does not call onClose when clicking inside the popup', () => {
37-
const onClose = vi.fn()
3840
const { getByText } = render(<Welcome onClose={onClose} />)
3941

4042
// Find and click on an element inside the popup content
@@ -45,7 +47,6 @@ describe('Welcome Component', () => {
4547
})
4648

4749
it('calls onClose when pressing Escape key', () => {
48-
const onClose = vi.fn()
4950
render(<Welcome onClose={onClose} />)
5051

5152
// Simulate pressing the Escape key
@@ -54,7 +55,6 @@ describe('Welcome Component', () => {
5455
})
5556

5657
it('does not call onClose when pressing other keys', () => {
57-
const onClose = vi.fn()
5858
render(<Welcome onClose={onClose} />)
5959

6060
// Simulate pressing a different key
@@ -63,7 +63,6 @@ describe('Welcome Component', () => {
6363
})
6464

6565
it('renders custom content and button text', () => {
66-
const onClose = vi.fn()
6766
const customContent = <p>Custom welcome message</p>
6867
const customButtonText = 'Custom Got it'
6968
const { getByText } = render(

test/lib/sources/httpSource.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { describe, expect, it, test, vi } from 'vitest'
1+
import { beforeEach, describe, expect, it, test, vi } from 'vitest'
22
import { getHttpSource } from '../../../src/lib/sources/httpSource.js'
33

44
globalThis.fetch = vi.fn()
55

66
describe('getHttpSource', () => {
7+
beforeEach(() => {
8+
vi.clearAllMocks()
9+
})
10+
711
test.for([
812
'http://example.com/test.txt',
913
'https://example.com/test.txt',

test/lib/sources/hyperparamSource.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { assert, describe, expect, it, test, vi } from 'vitest'
1+
import { assert, beforeEach, describe, expect, it, test, vi } from 'vitest'
22
import { HyperparamFileMetadata, getHyperparamSource } from '../../../src/lib/sources/hyperparamSource.js'
33

44
globalThis.fetch = vi.fn()
55

66
describe('getHyperparamSource', () => {
7+
beforeEach(() => {
8+
vi.clearAllMocks()
9+
})
10+
711
const endpoint = 'http://localhost:3000'
812

913
it('recognizes local files', () => {

0 commit comments

Comments
 (0)