Skip to content

Commit 165287e

Browse files
committed
test: added tests for subjectTableExcel download method and confirming its contents
1 parent a0b2032 commit 165287e

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const mockStore = {
2020

2121
const mockDownloadFn = vi.fn();
2222

23+
const mockExcelDownloadFn = vi.hoisted(() => vi.fn());
24+
2325
const mockInfoQuery = {
2426
data: []
2527
};
@@ -53,6 +55,10 @@ vi.mock('@/hooks/useInstrumentInfoQuery', () => ({
5355
useInstrumentInfoQuery: () => mockInfoQuery
5456
}));
5557

58+
vi.mock('@/utils/excel', () => ({
59+
downloadSubjectTableExcel: mockExcelDownloadFn
60+
}));
61+
5662
vi.mock('@/hooks/useInstrumentRecords', () => ({
5763
useInstrumentRecords: () => mockInstrumentRecords
5864
}));
@@ -124,6 +130,51 @@ describe('useInstrumentVisualization', () => {
124130
);
125131
});
126132
});
133+
describe('Excel', () => {
134+
it('Should download', () => {
135+
const { result } = renderHook(() => useInstrumentVisualization({ params: { subjectId: 'testId' } }));
136+
const { dl, records } = result.current;
137+
act(() => dl('Excel'));
138+
expect(records).toBeDefined();
139+
expect(mockExcelDownloadFn).toHaveBeenCalledTimes(1);
140+
const [filename, getContentFn] = mockExcelDownloadFn.mock.calls[0] ?? [];
141+
expect(filename).toMatch('.xlsx');
142+
const excelContents = getContentFn;
143+
144+
expect(excelContents).toEqual([
145+
{
146+
GroupID: 'testGroupId',
147+
subjectId: 'testId',
148+
// eslint-disable-next-line perfectionist/sort-objects
149+
Date: '2025-04-30',
150+
someValue: 'abc'
151+
}
152+
]);
153+
});
154+
});
155+
describe('Excel Long', () => {
156+
it('Should download', () => {
157+
const { result } = renderHook(() => useInstrumentVisualization({ params: { subjectId: 'testId' } }));
158+
const { dl, records } = result.current;
159+
act(() => dl('Excel Long'));
160+
expect(records).toBeDefined();
161+
expect(mockExcelDownloadFn).toHaveBeenCalledTimes(1);
162+
163+
const [filename, getContentFn] = mockExcelDownloadFn.mock.calls[0] ?? [];
164+
expect(filename).toMatch('.xlsx');
165+
const excelContents = getContentFn;
166+
167+
expect(excelContents).toEqual([
168+
{
169+
Date: '2025-04-30',
170+
GroupID: 'testGroupId',
171+
SubjectID: 'testId',
172+
Value: 'abc',
173+
Variable: 'someValue'
174+
}
175+
]);
176+
});
177+
});
127178
describe('JSON', () => {
128179
it('Should download', async () => {
129180
const { result } = renderHook(() => useInstrumentVisualization({ params: { subjectId: 'testId' } }));
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { downloadExcel } from '../excel';
3+
import { downloadExcel, downloadSubjectTableExcel } from '../excel';
44

55
describe('downloadExcel', () => {
66
it('should be defined', () => {
77
expect(downloadExcel).toBeDefined();
88
});
99
});
10+
11+
describe('downloadSujectTableExcel', () => {
12+
it('should be defined', () => {
13+
expect(downloadSubjectTableExcel).toBeDefined();
14+
});
15+
});

0 commit comments

Comments
 (0)