11import "@testing-library/jest-dom"
2- import { fireEvent , screen , render , waitFor } from '@testing-library/react' ;
2+ import { fireEvent , screen , render , waitFor , cleanup } from '@testing-library/react' ;
33import React from 'react' ;
44import Calendar from '../Calendar' ;
5- import { MockedProvider , wait } from "@apollo/client/testing" ;
5+ import { MockedProvider , MockedResponse } from "@apollo/client/testing" ;
66import { toast } from "react-toastify" ;
7- import { GET_EVENTS , GET_EVENT , ADD_EVENT , EDIT_EVENT , CANCEL_EVENT } from "../../Mutations/event" ;
7+ import { GET_EVENTS , ADD_EVENT , EDIT_EVENT , CANCEL_EVENT } from "../../Mutations/event" ;
88
9- const getEventsMock = {
9+ const getEventsMock : MockedResponse = {
1010 request : {
1111 query : GET_EVENTS ,
1212 variables : {
13- authToken : "mocked_auth_token"
14- }
15- } ,
16- result : {
17- data : {
18- getEvents : [
19- {
20- id : "1" ,
21- user : "1" ,
22- end : "2024-09-12T00:00:00.000Z" ,
23- hostName : "Jack" ,
24- start : "2024-09-12T00:00:00.000Z" ,
25- timeToEnd : "10:00" ,
26- timeToStart : "09:00" ,
27- title : "Mock Event" ,
28- invitees : [
29- {
30- 31- } ,
32- {
33- 34- }
35- ]
36- } ,
37- {
38- id : "2" ,
39- user : "1" ,
40- end : "2024-09-12T00:00:00.000Z" ,
41- hostName : "Musoni" ,
42- start : "2024-09-12T00:00:00.000Z" ,
43- timeToEnd : "10:00" ,
44- timeToStart : "09:00" ,
45- title : "Next Event" ,
46- invitees : [
47- {
48- 49- } ,
50- {
51- 52- }
53- ]
54- }
55- ]
13+ authToken : 'mocked_auth_token'
5614 }
5715 } ,
16+ result :{
17+ data :{
18+ getEvents :[
19+ {
20+ id : "1" ,
21+ user : "1" ,
22+ end : "2024-10-02T00:00:00.000Z" ,
23+ hostName : "Jack" ,
24+ start : "2024-10-02T00:00:00.000Z" ,
25+ timeToEnd : "03:00" ,
26+ timeToStart : "04:00" ,
27+ title : "Mocked Event" ,
28+ invitees :
[ { email :
"[email protected] " } , { email :
"[email protected] " } ] 29+ } ,
30+ {
31+ id : "2" ,
32+ user : "1" ,
33+ end : "2024-10-02T00:00:00.000Z" ,
34+ hostName : "Jones" ,
35+ start : "2024-10-02T00:00:00.000Z" ,
36+ timeToEnd : "03:00" ,
37+ timeToStart : "04:00" ,
38+ title : "Another Mocked Event" ,
39+ invitees :
[ { email :
"[email protected] " } , { email :
"[email protected] " } ] 40+ } ,
41+ ] }
42+ }
5843}
5944
60- const addEventMock = {
45+ const addEventMock : MockedResponse = {
6146 request : {
6247 query : ADD_EVENT ,
6348 } ,
@@ -76,7 +61,7 @@ const addEventMock = {
7661 }
7762}
7863
79- const addEventErrorMock = {
64+ const addEventErrorMock : MockedResponse = {
8065 request : {
8166 query : ADD_EVENT ,
8267 } ,
@@ -103,11 +88,14 @@ const editEventMock = {
10388 }
10489}
10590
106- const cancelEventMock = {
91+ const cancelEventMock : MockedResponse = {
10792 request : {
10893 query : CANCEL_EVENT ,
94+ variables : {
95+ eventId : '1' ,
96+ authToken : 'mocked_auth_token'
97+ }
10998 } ,
110- variableMatcher : ( ) => true ,
11199 result : {
112100 data : {
113101 cancelEvent : {
@@ -129,17 +117,23 @@ jest.mock('react-toastify',()=>({
129117 }
130118} ) )
131119
132- jest . spyOn ( Storage . prototype , 'getItem' ) . mockImplementation ( ( key : string ) => {
133- if ( key === " auth_token" ) return " mocked_auth_token"
134- if ( key === " orgToken" ) return " mocked_org_token"
135- if ( key === " auth" ) return JSON . stringify ( {
120+ beforeEach ( ( ) => {
121+ localStorage . setItem ( ' auth_token' , ' mocked_auth_token' )
122+ localStorage . setItem ( ' orgToken' , ' mocked_org_token' )
123+ localStorage . setItem ( ' auth' , JSON . stringify ( {
136124 auth : true ,
137- firstName : "Jack" ,
138125126+ firstName : "Jack" ,
139127 role : "admin" ,
140128 userId : "1"
141- } )
142- return null
129+ } ) )
130+ jest . useFakeTimers ( )
131+ } )
132+
133+ afterEach ( ( ) => {
134+ localStorage . clear ( )
135+ jest . runAllTimers ( )
136+ cleanup ( )
143137} )
144138
145139describe ( 'Calendar Tests' , ( ) => {
@@ -150,10 +144,10 @@ describe('Calendar Tests', () => {
150144 </ MockedProvider >
151145 ) ;
152146 await waitFor ( ( ) => {
153- expect ( screen . getByText ( "Mock Event" ) ) . toBeInTheDocument ( )
147+ expect ( screen . getByText ( "Mocked Event" ) ) . toBeInTheDocument ( )
154148 expect ( screen . getByText ( "Jack" ) ) . toBeInTheDocument ( )
155- expect ( screen . getByText ( "Next Event" ) ) . toBeInTheDocument ( )
156- expect ( screen . getByText ( "Musoni " ) ) . toBeInTheDocument ( )
149+ expect ( screen . getByText ( "Another Mocked Event" ) ) . toBeInTheDocument ( )
150+ expect ( screen . getByText ( "Jones " ) ) . toBeInTheDocument ( )
157151 } )
158152 } ) ;
159153
@@ -214,7 +208,6 @@ describe('Calendar Tests', () => {
214208 } ) ;
215209
216210 it ( 'should delete event when delete button is clicked' , async ( ) => {
217- jest . useFakeTimers ( )
218211 render (
219212 < MockedProvider mocks = { [ getEventsMock , cancelEventMock ] } addTypename = { false } >
220213 < Calendar />
0 commit comments