@@ -4,7 +4,6 @@ import { act } from '@testing-library/react';
44import { afterEach , describe , expect , it , vi } from 'vitest' ;
55
66import { useInstrumentVisualization } from '../useInstrumentVisualization' ;
7- import { toBasicISOString } from '@douglasneuroinformatics/libjs' ;
87
98const mockUseInstrument = vi . hoisted ( ( ) =>
109 vi . fn ( ( ) => ( {
@@ -107,4 +106,69 @@ describe('useInstrumentVisualization tests', () => {
107106 expect ( csvContents ) . toMatch ( 'subjectId,Date,someValue\r\ntestId,2025-04-30,abc' ) ;
108107 } ) ;
109108 } ) ;
109+ describe ( 'TSV' , ( ) => {
110+ it ( 'Should download' , ( ) => {
111+ vi . spyOn ( React , 'useEffect' ) . mockImplementation ( ( fn ) => fn ( ) ) ;
112+ const { dl, records } = useInstrumentVisualization ( {
113+ params : { subjectId : 'testId' }
114+ } ) ;
115+ act ( ( ) => dl ( 'TSV' ) ) ;
116+ expect ( records ) . toBeDefined ( ) ;
117+ expect ( mockUseDownload ) . toHaveBeenCalledTimes ( 1 ) ;
118+
119+ const [ filename , getContentFn ] = mockUseDownload . mock . calls [ 0 ] ;
120+ expect ( filename ) . toContain ( '.tsv' ) ;
121+ const tsvContents = getContentFn ( ) ;
122+ expect ( tsvContents ) . toMatch ( 'subjectId\tDate\tsomeValue\r\ntestId\t2025-04-30\tabc' ) ;
123+ } ) ;
124+ } ) ;
125+ describe ( 'CSV Long' , ( ) => {
126+ it ( 'Should download' , ( ) => {
127+ vi . spyOn ( React , 'useEffect' ) . mockImplementation ( ( fn ) => fn ( ) ) ;
128+ const { dl, records } = useInstrumentVisualization ( {
129+ params : { subjectId : 'testId' }
130+ } ) ;
131+ act ( ( ) => dl ( 'CSV Long' ) ) ;
132+ expect ( records ) . toBeDefined ( ) ;
133+ expect ( mockUseDownload ) . toHaveBeenCalledTimes ( 1 ) ;
134+
135+ const [ filename , getContentFn ] = mockUseDownload . mock . calls [ 0 ] ;
136+ expect ( filename ) . toContain ( '.csv' ) ;
137+ const csvLongContents = getContentFn ( ) ;
138+ expect ( csvLongContents ) . toMatch ( 'Date,SubjectID,Value,Variable\r\n2025-04-30,testId,abc,someValue' ) ;
139+ } ) ;
140+ } ) ;
141+ describe ( 'TSV Long' , ( ) => {
142+ it ( 'Should download' , ( ) => {
143+ vi . spyOn ( React , 'useEffect' ) . mockImplementation ( ( fn ) => fn ( ) ) ;
144+ const { dl, records } = useInstrumentVisualization ( {
145+ params : { subjectId : 'testId' }
146+ } ) ;
147+ act ( ( ) => dl ( 'TSV Long' ) ) ;
148+ expect ( records ) . toBeDefined ( ) ;
149+ expect ( mockUseDownload ) . toHaveBeenCalledTimes ( 1 ) ;
150+
151+ const [ filename , getContentFn ] = mockUseDownload . mock . calls [ 0 ] ;
152+ expect ( filename ) . toMatch ( '.tsv' ) ;
153+ const tsvLongContents = getContentFn ( ) ;
154+ expect ( tsvLongContents ) . toMatch ( 'Date\tSubjectID\tValue\tVariable\r\n2025-04-30\ttestId\tabc\tsomeValue' ) ;
155+ } ) ;
156+ } ) ;
157+ describe ( 'JSON' , ( ) => {
158+ it ( 'Should download' , async ( ) => {
159+ vi . spyOn ( React , 'useEffect' ) . mockImplementation ( ( fn ) => fn ( ) ) ;
160+ const { dl, records } = useInstrumentVisualization ( {
161+ params : { subjectId : 'testId' }
162+ } ) ;
163+ act ( ( ) => dl ( 'JSON' ) ) ;
164+ expect ( records ) . toBeDefined ( ) ;
165+ expect ( mockUseDownload ) . toHaveBeenCalledTimes ( 1 ) ;
166+
167+ const [ filename , getContentFn ] = mockUseDownload . mock . calls [ 0 ] ;
168+ expect ( filename ) . toMatch ( '.json' ) ;
169+ const jsonContents = await getContentFn ( ) ;
170+ expect ( jsonContents ) . toContain ( '"someValue": "abc"' ) ;
171+ expect ( jsonContents ) . toContain ( '"subjectID": "testId"' ) ;
172+ } ) ;
173+ } ) ;
110174} ) ;
0 commit comments