|
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 | + |
4 | 6 | import { useInstrumentVisualization } from '../useInstrumentVisualization'; |
5 | 7 |
|
| 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 | + |
6 | 65 | describe('useInstrumentVisualization tests', () => { |
| 66 | + afterEach(() => { |
| 67 | + vi.clearAllMocks(); |
| 68 | + }); |
| 69 | + |
7 | 70 | describe('CSV', () => { |
8 | 71 | it('Should download', () => { |
| 72 | + vi.spyOn(React, 'useEffect').mockImplementation((fn) => fn()); |
9 | 73 | const { dl, records } = useInstrumentVisualization({ |
10 | 74 | params: { subjectId: 'testId' } |
11 | 75 | }); |
| 76 | + act(() => dl('CSV')); |
| 77 | + expect(mockInstrument.useInstrument).toHaveBeenCalledOnce(); |
| 78 | + expect(mockStore.useAppStore).toHaveBeenCalled(); |
12 | 79 | expect(records).toBeDefined(); |
13 | | - console.log(records); |
14 | | - dl('CSV'); |
15 | 80 | }); |
16 | 81 | }); |
17 | 82 | }); |
0 commit comments