Skip to content

Commit 057d54c

Browse files
committed
test: refactor to use renderhook, set records with useInstrumentRecord mock
1 parent cb92937 commit 057d54c

File tree

1 file changed

+37
-57
lines changed

1 file changed

+37
-57
lines changed

apps/web/src/hooks/__tests__/useInstrumentVisualization.test.ts

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import React from 'react';
2-
3-
import { act } from '@testing-library/react';
4-
import { afterEach, describe, expect, it, vi } from 'vitest';
5-
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { renderHook, act } from '@testing-library/react';
63
import { useInstrumentVisualization } from '../useInstrumentVisualization';
74

85
const mockUseInstrument = vi.hoisted(() =>
@@ -14,55 +11,43 @@ const mockUseInstrument = vi.hoisted(() =>
1411
}))
1512
);
1613

17-
const mockRecords = [
18-
{
19-
__date__: new Date(),
20-
__time__: new Date().getTime(),
21-
someValue: 'abc'
22-
}
23-
];
24-
25-
vi.mock('@/hooks/useInstrument', () => ({
26-
useInstrument: mockUseInstrument
27-
}));
28-
2914
const mockStore = {
3015
currentGroup: { id: 'testGroupId' },
3116
currentUser: { username: 'testUser' }
3217
};
3318

3419
const mockBasicIsoString = '2025-04-30';
3520

36-
const mockUseDownload = vi.fn();
21+
const mockDownloadFn = vi.fn();
3722

3823
const mockInfoQuery = {
3924
useInstrumentInfoQuery: vi.fn()
4025
};
4126

4227
const mockInstrumentRecords = {
43-
useInstrumentRecords: vi.fn()
28+
data: [
29+
{
30+
date: new Date(),
31+
computedMeasures: {},
32+
data: { someValue: 'abc' }
33+
}
34+
]
4435
};
4536

37+
vi.mock('@/hooks/useInstrument', () => ({
38+
useInstrument: mockUseInstrument
39+
}));
40+
4641
vi.mock('@/store', () => ({
4742
useAppStore: vi.fn((selector) => selector(mockStore))
4843
}));
4944

5045
vi.mock('@douglasneuroinformatics/libui/hooks', () => ({
51-
useDownload: () => mockUseDownload,
46+
useDownload: vi.fn(() => mockDownloadFn),
5247
useNotificationsStore: () => ({ addNotification: vi.fn() }),
5348
useTranslation: () => ({ t: vi.fn((key) => key) })
5449
}));
5550

56-
vi.mock('react', async (importOriginal) => {
57-
const actual = await importOriginal<typeof React>();
58-
return {
59-
...actual,
60-
useEffect: vi.fn(),
61-
useMemo: vi.fn(),
62-
useState: vi.fn(() => [mockRecords, 'setRecords'])
63-
};
64-
});
65-
6651
vi.mock('@/hooks/useInstrumentInfoQuery', () => ({
6752
useInstrumentInfoQuery: () => mockInfoQuery
6853
}));
@@ -76,80 +61,75 @@ vi.mock('@douglasneuroinformatics/libjs', () => ({
7661
}));
7762

7863
describe('useInstrumentVisualization', () => {
79-
afterEach(() => {
64+
beforeEach(() => {
8065
vi.clearAllMocks();
8166
});
8267

8368
describe('CSV', () => {
8469
it('Should download', () => {
85-
const { dl, records } = useInstrumentVisualization({
86-
params: { subjectId: 'testId' }
87-
});
88-
act(() => dl('CSV'));
70+
const { result } = renderHook(() => useInstrumentVisualization({ params: { subjectId: 'testId' } }));
71+
const { records } = result.current;
72+
act(() => result.current.dl('CSV'));
8973
expect(records).toBeDefined();
90-
expect(mockUseDownload).toHaveBeenCalledTimes(1);
74+
expect(mockDownloadFn).toHaveBeenCalledTimes(1);
9175

92-
const [filename, getContentFn] = mockUseDownload.mock.calls[0];
76+
const [filename, getContentFn] = mockDownloadFn.mock.calls[0];
9377
expect(filename).toContain('.csv');
9478
const csvContents = getContentFn();
9579
expect(csvContents).toMatch('subjectId,Date,someValue\r\ntestId,2025-04-30,abc');
9680
});
9781
});
9882
describe('TSV', () => {
9983
it('Should download', () => {
100-
const { dl, records } = useInstrumentVisualization({
101-
params: { subjectId: 'testId' }
102-
});
84+
const { result } = renderHook(() => useInstrumentVisualization({ params: { subjectId: 'testId' } }));
85+
const { dl, records } = result.current;
10386
act(() => dl('TSV'));
10487
expect(records).toBeDefined();
105-
expect(mockUseDownload).toHaveBeenCalledTimes(1);
88+
expect(mockDownloadFn).toHaveBeenCalledTimes(1);
10689

107-
const [filename, getContentFn] = mockUseDownload.mock.calls[0];
90+
const [filename, getContentFn] = mockDownloadFn.mock.calls[0];
10891
expect(filename).toContain('.tsv');
10992
const tsvContents = getContentFn();
11093
expect(tsvContents).toMatch('subjectId\tDate\tsomeValue\r\ntestId\t2025-04-30\tabc');
11194
});
11295
});
11396
describe('CSV Long', () => {
11497
it('Should download', () => {
115-
const { dl, records } = useInstrumentVisualization({
116-
params: { subjectId: 'testId' }
117-
});
98+
const { result } = renderHook(() => useInstrumentVisualization({ params: { subjectId: 'testId' } }));
99+
const { dl, records } = result.current;
118100
act(() => dl('CSV Long'));
119101
expect(records).toBeDefined();
120-
expect(mockUseDownload).toHaveBeenCalledTimes(1);
102+
expect(mockDownloadFn).toHaveBeenCalledTimes(1);
121103

122-
const [filename, getContentFn] = mockUseDownload.mock.calls[0];
104+
const [filename, getContentFn] = mockDownloadFn.mock.calls[0];
123105
expect(filename).toContain('.csv');
124106
const csvLongContents = getContentFn();
125107
expect(csvLongContents).toMatch('Date,SubjectID,Value,Variable\r\n2025-04-30,testId,abc,someValue');
126108
});
127109
});
128110
describe('TSV Long', () => {
129111
it('Should download', () => {
130-
const { dl, records } = useInstrumentVisualization({
131-
params: { subjectId: 'testId' }
132-
});
112+
const { result } = renderHook(() => useInstrumentVisualization({ params: { subjectId: 'testId' } }));
113+
const { dl, records } = result.current;
133114
act(() => dl('TSV Long'));
134115
expect(records).toBeDefined();
135-
expect(mockUseDownload).toHaveBeenCalledTimes(1);
116+
expect(mockDownloadFn).toHaveBeenCalledTimes(1);
136117

137-
const [filename, getContentFn] = mockUseDownload.mock.calls[0];
118+
const [filename, getContentFn] = mockDownloadFn.mock.calls[0];
138119
expect(filename).toMatch('.tsv');
139120
const tsvLongContents = getContentFn();
140121
expect(tsvLongContents).toMatch('Date\tSubjectID\tValue\tVariable\r\n2025-04-30\ttestId\tabc\tsomeValue');
141122
});
142123
});
143124
describe('JSON', () => {
144125
it('Should download', async () => {
145-
const { dl, records } = useInstrumentVisualization({
146-
params: { subjectId: 'testId' }
147-
});
126+
const { result } = renderHook(() => useInstrumentVisualization({ params: { subjectId: 'testId' } }));
127+
const { dl, records } = result.current;
148128
act(() => dl('JSON'));
149129
expect(records).toBeDefined();
150-
expect(mockUseDownload).toHaveBeenCalledTimes(1);
130+
expect(mockDownloadFn).toHaveBeenCalledTimes(1);
151131

152-
const [filename, getContentFn] = mockUseDownload.mock.calls[0];
132+
const [filename, getContentFn] = mockDownloadFn.mock.calls[0];
153133
expect(filename).toMatch('.json');
154134
const jsonContents = await getContentFn();
155135
expect(jsonContents).toContain('"someValue": "abc"');

0 commit comments

Comments
 (0)