11import { toBasicISOString } from '@douglasneuroinformatics/libjs' ;
2- import { act , renderHook } from '@testing-library/react' ;
2+ import { act , renderHook , waitFor } from '@testing-library/react' ;
33import { beforeEach , describe , expect , it , vi } from 'vitest' ;
44
55import { useInstrumentVisualization } from '../useInstrumentVisualization' ;
@@ -32,7 +32,19 @@ const mockInstrumentRecords = {
3232 {
3333 computedMeasures : { } ,
3434 data : { someValue : 'abc' } ,
35- date : FIXED_TEST_DATE
35+ date : FIXED_TEST_DATE ,
36+ sessionId : '123'
37+ }
38+ ]
39+ } ;
40+
41+ const mockSessionWithUsername = {
42+ data : [
43+ {
44+ id : '123' ,
45+ user : {
46+ username : 'testusername'
47+ }
3648 }
3749 ]
3850} ;
@@ -63,78 +75,97 @@ vi.mock('@/hooks/useInstrumentRecords', () => ({
6375 useInstrumentRecords : ( ) => mockInstrumentRecords
6476} ) ) ;
6577
78+ vi . mock ( '@/hooks/useFindSessionQuery' , ( ) => ( {
79+ useFindSessionQuery : ( ) => mockSessionWithUsername
80+ } ) ) ;
81+
6682describe ( 'useInstrumentVisualization' , ( ) => {
6783 beforeEach ( ( ) => {
6884 vi . clearAllMocks ( ) ;
6985 } ) ;
7086
7187 describe ( 'CSV' , ( ) => {
72- it ( 'Should download' , ( ) => {
88+ it ( 'Should download' , async ( ) => {
7389 const { result } = renderHook ( ( ) => useInstrumentVisualization ( { params : { subjectId : 'testId' } } ) ) ;
7490 const { records } = result . current ;
91+ await waitFor ( ( ) => {
92+ expect ( result . current . records . length ) . toBeGreaterThan ( 0 ) ;
93+ } ) ;
7594 act ( ( ) => result . current . dl ( 'CSV' ) ) ;
7695 expect ( records ) . toBeDefined ( ) ;
7796 expect ( mockDownloadFn ) . toHaveBeenCalledTimes ( 1 ) ;
7897 const [ filename , getContentFn ] = mockDownloadFn . mock . calls [ 0 ] ?? [ ] ;
7998 expect ( filename ) . toContain ( '.csv' ) ;
8099 const csvContents = getContentFn ( ) ;
81100 expect ( csvContents ) . toMatch (
82- `GroupID,subjectId,Date,someValue\r\ntestGroupId,testId,${ toBasicISOString ( FIXED_TEST_DATE ) } ,abc`
101+ `GroupID,subjectId,Date,Username, someValue\r\ntestGroupId,testId,${ toBasicISOString ( FIXED_TEST_DATE ) } ,testusername ,abc`
83102 ) ;
84103 } ) ;
85104 } ) ;
86105 describe ( 'TSV' , ( ) => {
87- it ( 'Should download' , ( ) => {
106+ it ( 'Should download' , async ( ) => {
88107 const { result } = renderHook ( ( ) => useInstrumentVisualization ( { params : { subjectId : 'testId' } } ) ) ;
89- const { dl, records } = result . current ;
90- act ( ( ) => dl ( 'TSV' ) ) ;
108+ const { records } = result . current ;
109+ await waitFor ( ( ) => {
110+ expect ( result . current . records . length ) . toBeGreaterThan ( 0 ) ;
111+ } ) ;
112+ act ( ( ) => result . current . dl ( 'TSV' ) ) ;
91113 expect ( records ) . toBeDefined ( ) ;
92114 expect ( mockDownloadFn ) . toHaveBeenCalledTimes ( 1 ) ;
93115 const [ filename , getContentFn ] = mockDownloadFn . mock . calls [ 0 ] ?? [ ] ;
94116 expect ( filename ) . toContain ( '.tsv' ) ;
95117 const tsvContents = getContentFn ( ) ;
96118 expect ( tsvContents ) . toMatch (
97- `GroupID\tsubjectId\tDate\tsomeValue\r\ntestGroupId\ttestId\t${ toBasicISOString ( FIXED_TEST_DATE ) } \tabc`
119+ `GroupID\tsubjectId\tDate\tUsername\ tsomeValue\r\ntestGroupId\ttestId\t${ toBasicISOString ( FIXED_TEST_DATE ) } \ttestusername \tabc`
98120 ) ;
99121 } ) ;
100122 } ) ;
101123 describe ( 'CSV Long' , ( ) => {
102- it ( 'Should download' , ( ) => {
124+ it ( 'Should download' , async ( ) => {
103125 const { result } = renderHook ( ( ) => useInstrumentVisualization ( { params : { subjectId : 'testId' } } ) ) ;
104- const { dl, records } = result . current ;
105- act ( ( ) => dl ( 'CSV Long' ) ) ;
126+ const { records } = result . current ;
127+ await waitFor ( ( ) => {
128+ expect ( result . current . records . length ) . toBeGreaterThan ( 0 ) ;
129+ } ) ;
130+ act ( ( ) => result . current . dl ( 'CSV Long' ) ) ;
106131 expect ( records ) . toBeDefined ( ) ;
107132 expect ( mockDownloadFn ) . toHaveBeenCalledTimes ( 1 ) ;
108133
109134 const [ filename , getContentFn ] = mockDownloadFn . mock . calls [ 0 ] ?? [ ] ;
110135 expect ( filename ) . toContain ( '.csv' ) ;
111136 const csvLongContents = getContentFn ( ) ;
112137 expect ( csvLongContents ) . toMatch (
113- `GroupID,Date,SubjectID,Value,Variable\r\ntestGroupId,${ toBasicISOString ( FIXED_TEST_DATE ) } ,testId,abc,someValue`
138+ `GroupID,Date,SubjectID,Username, Value,Variable\r\ntestGroupId,${ toBasicISOString ( FIXED_TEST_DATE ) } ,testId,testusername ,abc,someValue`
114139 ) ;
115140 } ) ;
116141 } ) ;
117142 describe ( 'TSV Long' , ( ) => {
118- it ( 'Should download' , ( ) => {
143+ it ( 'Should download' , async ( ) => {
119144 const { result } = renderHook ( ( ) => useInstrumentVisualization ( { params : { subjectId : 'testId' } } ) ) ;
120- const { dl, records } = result . current ;
121- act ( ( ) => dl ( 'TSV Long' ) ) ;
145+ const { records } = result . current ;
146+ await waitFor ( ( ) => {
147+ expect ( result . current . records . length ) . toBeGreaterThan ( 0 ) ;
148+ } ) ;
149+ act ( ( ) => result . current . dl ( 'TSV Long' ) ) ;
122150 expect ( records ) . toBeDefined ( ) ;
123151 expect ( mockDownloadFn ) . toHaveBeenCalledTimes ( 1 ) ;
124152
125153 const [ filename , getContentFn ] = mockDownloadFn . mock . calls [ 0 ] ?? [ ] ;
126154 expect ( filename ) . toMatch ( '.tsv' ) ;
127155 const tsvLongContents = getContentFn ( ) ;
128156 expect ( tsvLongContents ) . toMatch (
129- `GroupID\tDate\tSubjectID\tValue\tVariable\r\ntestGroupId\t${ toBasicISOString ( FIXED_TEST_DATE ) } \ttestId\tabc\tsomeValue`
157+ `GroupID\tDate\tSubjectID\tUsername\ tValue\tVariable\r\ntestGroupId\t${ toBasicISOString ( FIXED_TEST_DATE ) } \ttestId\ttestusername \tabc\tsomeValue`
130158 ) ;
131159 } ) ;
132160 } ) ;
133161 describe ( 'Excel' , ( ) => {
134- it ( 'Should download' , ( ) => {
162+ it ( 'Should download' , async ( ) => {
135163 const { result } = renderHook ( ( ) => useInstrumentVisualization ( { params : { subjectId : 'testId' } } ) ) ;
136- const { dl, records } = result . current ;
137- act ( ( ) => dl ( 'Excel' ) ) ;
164+ const { records } = result . current ;
165+ await waitFor ( ( ) => {
166+ expect ( result . current . records . length ) . toBeGreaterThan ( 0 ) ;
167+ } ) ;
168+ act ( ( ) => result . current . dl ( 'Excel' ) ) ;
138169 expect ( records ) . toBeDefined ( ) ;
139170 expect ( mockExcelDownloadFn ) . toHaveBeenCalledTimes ( 1 ) ;
140171 const [ filename , getContentFn ] = mockExcelDownloadFn . mock . calls [ 0 ] ?? [ ] ;
@@ -143,20 +174,24 @@ describe('useInstrumentVisualization', () => {
143174
144175 expect ( excelContents ) . toEqual ( [
145176 {
177+ Date : '2025-04-30' ,
146178 GroupID : 'testGroupId' ,
147179 subjectId : 'testId' ,
148180 // eslint-disable-next-line perfectionist/sort-objects
149- Date : '2025-04-30 ' ,
150- someValue : 'abc '
181+ someValue : 'abc ' ,
182+ Username : 'testusername '
151183 }
152184 ] ) ;
153185 } ) ;
154186 } ) ;
155187 describe ( 'Excel Long' , ( ) => {
156- it ( 'Should download' , ( ) => {
188+ it ( 'Should download' , async ( ) => {
157189 const { result } = renderHook ( ( ) => useInstrumentVisualization ( { params : { subjectId : 'testId' } } ) ) ;
158- const { dl, records } = result . current ;
159- act ( ( ) => dl ( 'Excel Long' ) ) ;
190+ const { records } = result . current ;
191+ await waitFor ( ( ) => {
192+ expect ( result . current . records . length ) . toBeGreaterThan ( 0 ) ;
193+ } ) ;
194+ act ( ( ) => result . current . dl ( 'Excel Long' ) ) ;
160195 expect ( records ) . toBeDefined ( ) ;
161196 expect ( mockExcelDownloadFn ) . toHaveBeenCalledTimes ( 1 ) ;
162197
@@ -169,6 +204,7 @@ describe('useInstrumentVisualization', () => {
169204 Date : '2025-04-30' ,
170205 GroupID : 'testGroupId' ,
171206 SubjectID : 'testId' ,
207+ Username : 'testusername' ,
172208 Value : 'abc' ,
173209 Variable : 'someValue'
174210 }
@@ -178,8 +214,11 @@ describe('useInstrumentVisualization', () => {
178214 describe ( 'JSON' , ( ) => {
179215 it ( 'Should download' , async ( ) => {
180216 const { result } = renderHook ( ( ) => useInstrumentVisualization ( { params : { subjectId : 'testId' } } ) ) ;
181- const { dl, records } = result . current ;
182- act ( ( ) => dl ( 'JSON' ) ) ;
217+ const { records } = result . current ;
218+ await waitFor ( ( ) => {
219+ expect ( result . current . records . length ) . toBeGreaterThan ( 0 ) ;
220+ } ) ;
221+ act ( ( ) => result . current . dl ( 'JSON' ) ) ;
183222 expect ( records ) . toBeDefined ( ) ;
184223 expect ( mockDownloadFn ) . toHaveBeenCalledTimes ( 1 ) ;
185224
0 commit comments