Skip to content

Commit 8c753ff

Browse files
committed
test: add mocks for all hooks and react components
1 parent b6c29b1 commit 8c753ff

1 file changed

Lines changed: 70 additions & 5 deletions

File tree

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,82 @@
1-
// import { MockFactory } from '@douglasneuroinformatics/libnest/testing';
2-
// import type { MockedInstance } from '@douglasneuroinformatics/libnest/testing';
3-
import { describe, it, vi, expect, beforeEach } from 'vitest';
1+
import React from 'react';
2+
3+
import { act } from '@testing-library/react';
4+
import { afterEach, describe, expect, it, vi } from 'vitest';
5+
46
import { useInstrumentVisualization } from '../useInstrumentVisualization';
57

8+
const mockInstrument = {
9+
useInstrument: vi.fn()
10+
};
11+
12+
vi.mock('@/hooks/useInstrument', () => ({
13+
useInstrument: () => mockInstrument
14+
}));
15+
16+
const mockStore = {
17+
useAppStore: vi.fn()
18+
};
19+
const mockDownload = {
20+
useDownload: vi.fn()
21+
};
22+
const mockNotification = {
23+
useNotificationsStore: vi.fn()
24+
};
25+
const mockTranslation = {
26+
useTranslation: vi.fn()
27+
};
28+
29+
const mockInfoQuery = {
30+
useInstrumentInfoQuery: vi.fn()
31+
};
32+
33+
const mockInstrumentRecords = {
34+
useInstrumentRecords: vi.fn()
35+
};
36+
37+
vi.mock('@/store', () => ({
38+
useAppStore: () => mockStore
39+
}));
40+
41+
vi.mock('@douglasneuroinformatics/libui/hooks', () => ({
42+
useDownload: () => mockDownload,
43+
useNotificationsStore: () => mockNotification,
44+
useTranslation: () => mockTranslation
45+
}));
46+
47+
vi.mock('react', async (importOriginal) => {
48+
const actual = await importOriginal<typeof React>();
49+
return {
50+
...actual,
51+
useEffect: vi.fn(),
52+
useMemo: vi.fn(),
53+
useState: vi.fn(() => ['mockedRecords', vi.fn()])
54+
};
55+
});
56+
57+
vi.mock('@/hooks/useInstrumentInfoQuery', () => ({
58+
useInstrumentInfoQuery: () => mockInfoQuery
59+
}));
60+
61+
vi.mock('@/hooks/useInstrumentRecords', () => ({
62+
useInstrumentRecords: () => mockInstrumentRecords
63+
}));
64+
665
describe('useInstrumentVisualization tests', () => {
66+
afterEach(() => {
67+
vi.clearAllMocks();
68+
});
69+
770
describe('CSV', () => {
871
it('Should download', () => {
72+
vi.spyOn(React, 'useEffect').mockImplementation((fn) => fn());
973
const { dl, records } = useInstrumentVisualization({
1074
params: { subjectId: 'testId' }
1175
});
76+
act(() => dl('CSV'));
77+
expect(mockInstrument.useInstrument).toHaveBeenCalledOnce();
78+
expect(mockStore.useAppStore).toHaveBeenCalled();
1279
expect(records).toBeDefined();
13-
console.log(records);
14-
dl('CSV');
1580
});
1681
});
1782
});

0 commit comments

Comments
 (0)