11import { vi , describe , test , expect , beforeEach } from 'vitest' ;
2- import { uploadReport , ReportError , fetchLatestReports , fetchAllReports , markReportAsRead } from '../reportService' ;
2+ import {
3+ uploadReport ,
4+ ReportError ,
5+ fetchLatestReports ,
6+ fetchAllReports ,
7+ markReportAsRead ,
8+ } from '../reportService' ;
39import { ReportCategory , ReportStatus } from '../../models/medicalReport' ;
410import axios from 'axios' ;
511
@@ -12,13 +18,13 @@ vi.mock('axios', () => ({
1218 post : vi . fn ( ) ,
1319 get : vi . fn ( ) ,
1420 patch : vi . fn ( ) ,
15- isAxiosError : vi . fn ( ( ) => true )
16- }
21+ isAxiosError : vi . fn ( ( ) => true ) ,
22+ } ,
1723} ) ) ;
1824
1925// Mock dynamic imports to handle the service functions
2026vi . mock ( '../reportService' , async ( importOriginal ) => {
21- const actual = await importOriginal ( ) as typeof ReportServiceModule ;
27+ const actual = ( await importOriginal ( ) ) as typeof ReportServiceModule ;
2228
2329 // Create a new object with the same properties as the original
2430 return {
@@ -38,9 +44,11 @@ vi.mock('../reportService', async (importOriginal) => {
3844 return response . data ;
3945 } catch ( error ) {
4046 // Properly wrap the error in a ReportError
41- throw new actual . ReportError ( error instanceof Error
42- ? `Failed to upload report: ${ error . message } `
43- : 'Failed to upload report' ) ;
47+ throw new actual . ReportError (
48+ error instanceof Error
49+ ? `Failed to upload report: ${ error . message } `
50+ : 'Failed to upload report' ,
51+ ) ;
4452 }
4553 } ,
4654
@@ -50,9 +58,11 @@ vi.mock('../reportService', async (importOriginal) => {
5058 const response = await axios . get ( `/api/reports/latest?limit=${ limit } ` ) ;
5159 return response . data ;
5260 } catch ( error ) {
53- throw new actual . ReportError ( error instanceof Error
54- ? `Failed to fetch latest reports: ${ error . message } `
55- : 'Failed to fetch latest reports' ) ;
61+ throw new actual . ReportError (
62+ error instanceof Error
63+ ? `Failed to fetch latest reports: ${ error . message } `
64+ : 'Failed to fetch latest reports' ,
65+ ) ;
5666 }
5767 } ,
5868
@@ -62,9 +72,11 @@ vi.mock('../reportService', async (importOriginal) => {
6272 const response = await axios . get ( `/api/reports` ) ;
6373 return response . data ;
6474 } catch ( error ) {
65- throw new actual . ReportError ( error instanceof Error
66- ? `Failed to fetch all reports: ${ error . message } `
67- : 'Failed to fetch all reports' ) ;
75+ throw new actual . ReportError (
76+ error instanceof Error
77+ ? `Failed to fetch all reports: ${ error . message } `
78+ : 'Failed to fetch all reports' ,
79+ ) ;
6880 }
6981 } ,
7082
@@ -79,10 +91,10 @@ vi.mock('@aws-amplify/auth', () => ({
7991 fetchAuthSession : vi . fn ( ) . mockResolvedValue ( {
8092 tokens : {
8193 idToken : {
82- toString : ( ) => 'mock-id-token'
83- }
84- }
85- } )
94+ toString : ( ) => 'mock-id-token' ,
95+ } ,
96+ } ,
97+ } ) ,
8698} ) ) ;
8799
88100// Mock response data
@@ -100,7 +112,7 @@ const mockReports = [
100112 status : ReportStatus . UNREAD ,
101113 category : ReportCategory . BRAIN ,
102114 date : '2024-03-24' ,
103- }
115+ } ,
104116] ;
105117
106118describe ( 'reportService' , ( ) => {
@@ -122,7 +134,7 @@ describe('reportService', () => {
122134 status : ReportStatus . UNREAD ,
123135 category : ReportCategory . GENERAL ,
124136 date : '2024-05-10' ,
125- }
137+ } ,
126138 } ) ;
127139 } ) ;
128140
@@ -147,7 +159,7 @@ describe('reportService', () => {
147159 status : ReportStatus . UNREAD ,
148160 category : ReportCategory . HEART ,
149161 date : '2024-05-10' ,
150- }
162+ } ,
151163 } ) ;
152164
153165 const heartFile = new File ( [ 'test' ] , 'heart-report.pdf' , { type : 'application/pdf' } ) ;
@@ -162,7 +174,7 @@ describe('reportService', () => {
162174 status : ReportStatus . UNREAD ,
163175 category : ReportCategory . BRAIN ,
164176 date : '2024-05-10' ,
165- }
177+ } ,
166178 } ) ;
167179
168180 const neuroFile = new File ( [ 'test' ] , 'brain-scan.pdf' , { type : 'application/pdf' } ) ;
@@ -179,20 +191,18 @@ describe('reportService', () => {
179191 test ( 'should throw ReportError on upload failure' , async ( ) => {
180192 // Mock axios.post to fail
181193 ( axios . post as ReturnType < typeof vi . fn > ) . mockRejectedValueOnce (
182- new Error ( 'API request failed' )
194+ new Error ( 'API request failed' ) ,
183195 ) ;
184196
185- await expect ( uploadReport ( mockFile , progressCallback ) )
186- . rejects
187- . toThrow ( ReportError ) ;
197+ await expect ( uploadReport ( mockFile , progressCallback ) ) . rejects . toThrow ( ReportError ) ;
188198 } ) ;
189199 } ) ;
190200
191201 describe ( 'fetchLatestReports' , ( ) => {
192202 beforeEach ( ( ) => {
193203 // Setup axios mock response
194204 ( axios . get as ReturnType < typeof vi . fn > ) . mockResolvedValue ( {
195- data : mockReports . slice ( 0 , 2 )
205+ data : mockReports . slice ( 0 , 2 ) ,
196206 } ) ;
197207 } ) ;
198208
@@ -201,16 +211,18 @@ describe('reportService', () => {
201211
202212 expect ( axios . get ) . toHaveBeenCalled ( ) ;
203213 expect ( reports ) . toHaveLength ( 2 ) ;
204- expect ( reports [ 0 ] ) . toEqual ( expect . objectContaining ( {
205- id : expect . any ( String ) ,
206- title : expect . any ( String )
207- } ) ) ;
214+ expect ( reports [ 0 ] ) . toEqual (
215+ expect . objectContaining ( {
216+ id : expect . any ( String ) ,
217+ title : expect . any ( String ) ,
218+ } ) ,
219+ ) ;
208220 } ) ;
209221
210222 test ( 'should fetch latest reports with custom limit' , async ( ) => {
211223 const limit = 1 ;
212224 ( axios . get as ReturnType < typeof vi . fn > ) . mockResolvedValue ( {
213- data : mockReports . slice ( 0 , 1 )
225+ data : mockReports . slice ( 0 , 1 ) ,
214226 } ) ;
215227
216228 const reports = await fetchLatestReports ( limit ) ;
@@ -222,16 +234,14 @@ describe('reportService', () => {
222234 test ( 'should throw ReportError on fetch failure' , async ( ) => {
223235 ( axios . get as ReturnType < typeof vi . fn > ) . mockRejectedValue ( new Error ( 'Network error' ) ) ;
224236
225- await expect ( fetchLatestReports ( ) )
226- . rejects
227- . toThrow ( ReportError ) ;
237+ await expect ( fetchLatestReports ( ) ) . rejects . toThrow ( ReportError ) ;
228238 } ) ;
229239 } ) ;
230240
231241 describe ( 'fetchAllReports' , ( ) => {
232242 beforeEach ( ( ) => {
233243 ( axios . get as ReturnType < typeof vi . fn > ) . mockResolvedValue ( {
234- data : mockReports
244+ data : mockReports ,
235245 } ) ;
236246 } ) ;
237247
@@ -245,21 +255,19 @@ describe('reportService', () => {
245255 test ( 'should throw ReportError on fetch failure' , async ( ) => {
246256 ( axios . get as ReturnType < typeof vi . fn > ) . mockRejectedValue ( new Error ( 'Network error' ) ) ;
247257
248- await expect ( fetchAllReports ( ) )
249- . rejects
250- . toThrow ( ReportError ) ;
258+ await expect ( fetchAllReports ( ) ) . rejects . toThrow ( ReportError ) ;
251259 } ) ;
252260 } ) ;
253261
254262 describe ( 'markReportAsRead' , ( ) => {
255263 beforeEach ( ( ) => {
256264 const updatedReport = {
257265 ...mockReports [ 0 ] ,
258- status : ReportStatus . READ
266+ status : ReportStatus . READ ,
259267 } ;
260268
261269 ( axios . patch as ReturnType < typeof vi . fn > ) . mockResolvedValue ( {
262- data : updatedReport
270+ data : updatedReport ,
263271 } ) ;
264272 } ) ;
265273
@@ -273,9 +281,7 @@ describe('reportService', () => {
273281 test ( 'should throw error when report not found' , async ( ) => {
274282 ( axios . patch as ReturnType < typeof vi . fn > ) . mockRejectedValue ( new Error ( 'Report not found' ) ) ;
275283
276- await expect ( markReportAsRead ( 'non-existent-id' ) )
277- . rejects
278- . toThrow ( ReportError ) ;
284+ await expect ( markReportAsRead ( 'non-existent-id' ) ) . rejects . toThrow ( ReportError ) ;
279285 } ) ;
280286 } ) ;
281287} ) ;
0 commit comments