11import "@testing-library/jest-dom" ;
2- import { act , render , screen , waitFor , fireEvent } from "@testing-library/react" ;
2+ import {
3+ act ,
4+ fireEvent ,
5+ render ,
6+ screen ,
7+ waitFor ,
8+ } from "@testing-library/react" ;
39import userEvent from "@testing-library/user-event" ;
10+ import { deleteAudioEntry , getAudioEntries } from "../http/snoreAPI" ;
411import RecordAudioPage from "./recordAudioPage" ;
5- import { getAudioEntries , deleteAudioEntry } from "../http/snoreAPI" ;
6-
712
813const mockRouter = jest . fn ( ) ;
914jest . mock ( "next/navigation" , ( ) => ( {
@@ -15,194 +20,181 @@ jest.mock("next/navigation", () => ({
1520} ) ) ;
1621
1722const userData = {
18- uid : '1' ,
19- }
20-
23+ uid : "1" ,
24+ } ;
2125
2226jest . mock ( "../http/snoreAPI" , ( ) => {
23- return {
24- getAudioEntries : jest . fn ( ) ,
25- deleteAudioEntry : jest . fn ( ) ,
26- }
27- } )
28-
29- describe ( "Logged in user" , ( ) => {
30- jest . mock ( "../contexts/AuthContext" , ( ) => {
31- return {
32- useAuth : ( ) => {
33- return {
34- user : userData
35- }
36- }
37- } ;
38- } ) ;
39-
40- beforeEach ( async ( ) => {
41- await act ( async ( ) => {
42- render ( < RecordAudioPage /> ) ;
43- } ) ;
44- } )
45-
46- it ( "Table is displayed correctly" , ( ) => {
47- const dateHeader = screen . getByText ( "Date" ) ;
48- const resultHeader = screen . getByText ( "Result" ) ;
49- expect ( dateHeader ) . toBeInTheDocument ( ) ;
50- expect ( resultHeader ) . toBeInTheDocument ( ) ;
51- } ) ;
52-
53- it ( 'renders RecordAudioPage component' , ( ) => {
54- const recordIcon = screen . getByAltText ( 'Record Audio Icon' ) ;
55- expect ( recordIcon ) . toBeInTheDocument ( ) ;
56- } ) ;
57-
58- it ( "Fetches audio entries correctly" , async ( ) => {
59- getAudioEntries . mockResolvedValue ( {
60- success : "SUCCESS" ,
61- data : [
62- {
63- uid : 1 ,
64- date : "2024-01-01T00:00:00.000Z" ,
65- filename : "Dummy file name" ,
66- result : "Snoring Detected" ,
67- }
68- ]
69- } ) ;
70- await act ( async ( ) => {
71- jest . advanceTimersByTime ( 500 ) ;
72- } ) ;
73- await waitFor ( ( ) => {
74- expect ( getAudioEntries ) . toHaveBeenCalled ( ) ;
75- } ) ;
76- } ) ;
77-
78- it ( "Audio entries list is displayed correctly" , async ( ) => {
79- getAudioEntries . mockResolvedValue ( {
80- success : "SUCCESS" ,
81- data : [
82- {
83- uid : 1 ,
84- date : "2024-01-01T00:00:00.000Z" ,
85- filename : "Dummy file name" ,
86- result : "Snoring Detected" ,
87- }
88- ]
89- } ) ;
90- const date = await screen . findByText ( "Jan 1, 2024" ) ;
91- const result = await screen . findByText ( "Snoring Detected" ) ;
92-
93- await waitFor ( async ( ) => {
94- expect ( date ) . toBeInTheDocument ( ) ;
95- expect ( result ) . toBeInTheDocument ( ) ;
96- } )
97-
98- } ) ;
99-
100- it ( "Deletes audio entry" , async ( ) => {
101- setTimeout ( async ( ) => {
102- deleteAudioEntry . mockResolvedValue ( {
103- result : { message : 'Audio entry deleted successfully' }
104- } ) ;
105-
106- const audioID = '1' ;
107-
108- const trashIcon = screen . getByAltText ( 'Trash icon' ) ;
109- await userEvent . click ( trashIcon ) ;
110- const result = await deleteAudioEntry ( audioID ) ;
111- expect ( result . result . message ) . toEqual ( 'Audio entry deleted successfully' ) ;
112- expect ( mockRouter ) . toHaveBeenCalledWith ( "/snoringAI" ) ;
113- } , 1000 ) ;
114- } )
115-
116- it ( "should handle recording click" , async ( ) => {
117- setTimeout ( async ( ) => {
118- const dummyData = new Uint8Array ( [
119- 0x52 , 0x49 , 0x46 , 0x46 ,
120- 0x24 , 0x08 , 0x00 , 0x00 ,
121- 0x57 , 0x41 , 0x56 , 0x45 ,
122- 0x66 , 0x6D , 0x74 , 0x20 ,
123- 0x10 , 0x00 , 0x00 , 0x00 ,
124- 0x01 , 0x00 ,
125- 0x01 , 0x00 ,
126- 0x80 , 0xBB , 0x00 , 0x00 ,
127- 0x00 , 0x77 , 0x01 , 0x00 ,
128- 0x02 , 0x00 ,
129- 0x10 , 0x00 ,
130- 0x64 , 0x61 , 0x74 , 0x61 ,
131- 0x00 , 0x08 , 0x00 , 0x00 ,
132- ] ) ;
133- const mockMediaDevices = {
134- getUserMedia : jest . fn ( ( ) => Promise . resolve ( { } ) ) ,
135- } ;
136- global . navigator . mediaDevices = mockMediaDevices ;
137- await act ( async ( ) => {
138- await userEvent . click ( screen . getByText ( 'Record' ) ) ;
139- } ) ;
140- expect ( mockMediaDevices . getUserMedia ) . toHaveBeenCalled ( ) ;
141- await act ( async ( ) => {
142- fireEvent ( mockMediaDevices . mediaRecorderRef . current , {
143- type : 'dataavailable' ,
144- data : new Blob ( [ dummyData ] , { type : 'audio/wav' } ) ,
145- } ) ;
146- } ) ;
147- userEvent . click ( screen . getByText ( 'Record' ) ) ;
148- const recordingDateElement = await screen . findByText ( / R e c o r d i n g D a t e / i) ;
149- expect ( recordingDateElement ) . toBeInTheDocument ( ) ;
150- } , 1000 ) ;
151- } )
152-
153- it ( 'should handle stop click' , async ( ) => {
154- setTimeout ( async ( ) => {
155-
156- const recordIcon = screen . getByTestId ( 'record-container' ) ;
157- await userEvent . click ( recordIcon ) ;
158-
159- const originalMediaRecorderRef = global . MediaRecorder ;
160- global . MediaRecorder = {
161- onstop : jest . fn ( ) ,
162- stop : jest . fn ( ) ,
163- } ;
164-
165- const originalSetInterval = global . setInterval ;
166- global . setInterval = jest . fn ( ( ) => 123 ) ;
167-
168- const stopButton = await screen . findByText ( / S t o p / i) ;
169- await userEvent . click ( stopButton ) ;
170-
171- expect ( navigator . mediaDevices . getUserMedia ) . toHaveBeenCalled ( ) ;
172- expect ( global . MediaRecorder ) . toHaveBeenCalled ( ) ;
173- } , 1000 ) ;
174-
175- } ) ;
176-
177-
178- it ( "Back button redirects to health page" , async ( ) => {
179- setTimeout ( async ( ) => {
180- const backButton = screen . getAllByRole ( "button" ) [ 0 ] ;
181- userEvent . click ( backButton ) ;
182- expect ( mockRouter ) . toHaveBeenCalledWith ( "/health" ) ;
183- } , 1000 ) ;
184- } ) ;
185-
186- } ) ;
187-
27+ return {
28+ getAudioEntries : jest . fn ( ) ,
29+ deleteAudioEntry : jest . fn ( ) ,
30+ } ;
31+ } ) ;
32+
33+ describe ( "Logged in user" , ( ) => {
34+ jest . mock ( "../contexts/AuthContext" , ( ) => {
35+ return {
36+ useAuth : ( ) => {
37+ return {
38+ user : userData ,
39+ } ;
40+ } ,
41+ } ;
42+ } ) ;
43+
44+ beforeEach ( async ( ) => {
45+ await act ( async ( ) => {
46+ render ( < RecordAudioPage /> ) ;
47+ } ) ;
48+ } ) ;
49+
50+ it ( "Table is displayed correctly" , ( ) => {
51+ const dateHeader = screen . getByText ( "Date" ) ;
52+ const resultHeader = screen . getByText ( "Result" ) ;
53+ expect ( dateHeader ) . toBeInTheDocument ( ) ;
54+ expect ( resultHeader ) . toBeInTheDocument ( ) ;
55+ } ) ;
56+
57+ it ( "renders RecordAudioPage component" , ( ) => {
58+ const recordIcon = screen . getByAltText ( "Record Audio Icon" ) ;
59+ expect ( recordIcon ) . toBeInTheDocument ( ) ;
60+ } ) ;
61+
62+ it ( "Fetches audio entries correctly" , async ( ) => {
63+ getAudioEntries . mockResolvedValue ( {
64+ success : "SUCCESS" ,
65+ data : [
66+ {
67+ uid : 1 ,
68+ date : "2024-01-01T00:00:00.000Z" ,
69+ filename : "Dummy file name" ,
70+ result : "[0,1]" ,
71+ } ,
72+ ] ,
73+ } ) ;
74+ await act ( async ( ) => {
75+ jest . advanceTimersByTime ( 500 ) ;
76+ } ) ;
77+ await waitFor ( ( ) => {
78+ expect ( getAudioEntries ) . toHaveBeenCalled ( ) ;
79+ } ) ;
80+ } ) ;
81+
82+ it ( "Audio entries list is displayed correctly" , async ( ) => {
83+ getAudioEntries . mockResolvedValue ( {
84+ success : "SUCCESS" ,
85+ data : [
86+ {
87+ uid : 1 ,
88+ date : "2024-01-01T00:00:00.000Z" ,
89+ filename : "Dummy file name" ,
90+ result : "[1]" ,
91+ } ,
92+ ] ,
93+ } ) ;
94+ const date = await screen . findByText ( "Jan 1, 2024" ) ;
95+ const result = await screen . findByText ( "Snoring Detected" ) ;
96+
97+ await waitFor ( async ( ) => {
98+ expect ( date ) . toBeInTheDocument ( ) ;
99+ expect ( result ) . toBeInTheDocument ( ) ;
100+ } ) ;
101+ } ) ;
102+
103+ it ( "Deletes audio entry" , async ( ) => {
104+ setTimeout ( async ( ) => {
105+ deleteAudioEntry . mockResolvedValue ( {
106+ result : { message : "Audio entry deleted successfully" } ,
107+ } ) ;
108+
109+ const audioID = "1" ;
110+
111+ const trashIcon = screen . getByAltText ( "Trash icon" ) ;
112+ await userEvent . click ( trashIcon ) ;
113+ const result = await deleteAudioEntry ( audioID ) ;
114+ expect ( result . result . message ) . toEqual (
115+ "Audio entry deleted successfully"
116+ ) ;
117+ expect ( mockRouter ) . toHaveBeenCalledWith ( "/snoringAI" ) ;
118+ } , 1000 ) ;
119+ } ) ;
120+
121+ it ( "should handle recording click" , async ( ) => {
122+ setTimeout ( async ( ) => {
123+ const dummyData = new Uint8Array ( [
124+ 0x52 , 0x49 , 0x46 , 0x46 , 0x24 , 0x08 , 0x00 , 0x00 , 0x57 , 0x41 ,
125+ 0x56 , 0x45 , 0x66 , 0x6d , 0x74 , 0x20 , 0x10 , 0x00 , 0x00 , 0x00 ,
126+ 0x01 , 0x00 , 0x01 , 0x00 , 0x80 , 0xbb , 0x00 , 0x00 , 0x00 , 0x77 ,
127+ 0x01 , 0x00 , 0x02 , 0x00 , 0x10 , 0x00 , 0x64 , 0x61 , 0x74 , 0x61 ,
128+ 0x00 , 0x08 , 0x00 , 0x00 ,
129+ ] ) ;
130+ const mockMediaDevices = {
131+ getUserMedia : jest . fn ( ( ) => Promise . resolve ( { } ) ) ,
132+ } ;
133+ global . navigator . mediaDevices = mockMediaDevices ;
134+ await act ( async ( ) => {
135+ await userEvent . click ( screen . getByText ( "Record" ) ) ;
136+ } ) ;
137+ expect ( mockMediaDevices . getUserMedia ) . toHaveBeenCalled ( ) ;
138+ await act ( async ( ) => {
139+ fireEvent ( mockMediaDevices . mediaRecorderRef . current , {
140+ type : "dataavailable" ,
141+ data : new Blob ( [ dummyData ] , { type : "audio/wav" } ) ,
142+ } ) ;
143+ } ) ;
144+ userEvent . click ( screen . getByText ( "Record" ) ) ;
145+ const recordingDateElement = await screen . findByText (
146+ / R e c o r d i n g D a t e / i
147+ ) ;
148+ expect ( recordingDateElement ) . toBeInTheDocument ( ) ;
149+ } , 1000 ) ;
150+ } ) ;
151+
152+ it ( "should handle stop click" , async ( ) => {
153+ setTimeout ( async ( ) => {
154+ const recordIcon = screen . getByTestId ( "record-container" ) ;
155+ await userEvent . click ( recordIcon ) ;
156+
157+ const originalMediaRecorderRef = global . MediaRecorder ;
158+ global . MediaRecorder = {
159+ onstop : jest . fn ( ) ,
160+ stop : jest . fn ( ) ,
161+ } ;
162+
163+ const originalSetInterval = global . setInterval ;
164+ global . setInterval = jest . fn ( ( ) => 123 ) ;
165+
166+ const stopButton = await screen . findByText ( / S t o p / i) ;
167+ await userEvent . click ( stopButton ) ;
168+
169+ expect ( navigator . mediaDevices . getUserMedia ) . toHaveBeenCalled ( ) ;
170+ expect ( global . MediaRecorder ) . toHaveBeenCalled ( ) ;
171+ } , 1000 ) ;
172+ } ) ;
173+
174+ it ( "Back button redirects to health page" , async ( ) => {
175+ setTimeout ( async ( ) => {
176+ const backButton = screen . getAllByRole ( "button" ) [ 0 ] ;
177+ userEvent . click ( backButton ) ;
178+ expect ( mockRouter ) . toHaveBeenCalledWith ( "/health" ) ;
179+ } , 1000 ) ;
180+ } ) ;
181+ } ) ;
188182
189183describe ( "User not logged in" , ( ) => {
190-
191- beforeEach ( ( ) => {
192- jest . mock ( "../contexts/AuthContext" , ( ) => {
193- return {
194- useAuth : ( ) => {
195- return {
196- user : null
197- }
198- }
199- } ;
200- } ) ;
201- } ) ;
202-
203- it ( "Router push method redirects to login page" , ( ) => {
204- render ( < RecordAudioPage /> ) ;
205- expect ( mockRouter ) . toBeCalledWith ( '/login' ) ;
206- } ) ;
207-
208- } ) ;
184+ beforeEach ( ( ) => {
185+ jest . mock ( "../contexts/AuthContext" , ( ) => {
186+ return {
187+ useAuth : ( ) => {
188+ return {
189+ user : null ,
190+ } ;
191+ } ,
192+ } ;
193+ } ) ;
194+ } ) ;
195+
196+ it ( "Router push method redirects to login page" , ( ) => {
197+ render ( < RecordAudioPage /> ) ;
198+ expect ( mockRouter ) . toBeCalledWith ( "/login" ) ;
199+ } ) ;
200+ } ) ;
0 commit comments