1
- import React , { act } from 'react' ;
1
+ import React from 'react' ;
2
+ import { act } from 'react-dom/test-utils' ;
2
3
import { render , screen } from '@testing-library/react' ;
3
- import 'jest-location-mock' ;
4
+ import userEvent from '@testing-library/user-event' ;
5
+ import { describe , test , expect , vi , beforeEach } from 'vitest' ;
4
6
import { I18nextProvider } from 'react-i18next' ;
7
+ import { BrowserRouter } from 'react-router-dom' ;
8
+ import { MockedProvider } from '@apollo/client/testing' ;
5
9
6
10
import i18nForTest from 'utils/i18nForTest' ;
7
11
import type { InterfaceOrgListCardProps } from './OrgListCard' ;
8
12
import OrgListCard from './OrgListCard' ;
9
- import userEvent from '@testing-library/user-event' ;
10
- import { BrowserRouter } from 'react-router-dom' ;
11
13
import { IS_SAMPLE_ORGANIZATION_QUERY } from 'GraphQl/Queries/Queries' ;
12
14
import { StaticMockLink } from 'utils/StaticMockLink' ;
13
- import { MockedProvider } from '@apollo/react-testing' ;
14
15
import useLocalStorage from 'utils/useLocalstorage' ;
15
16
16
17
const { setItem, removeItem } = useLocalStorage ( ) ;
17
18
19
+ // Mock window.location
20
+ const mockAssign = vi . fn ( ) ;
21
+ Object . defineProperty ( window , 'location' , {
22
+ value : { assign : mockAssign } ,
23
+ writable : true ,
24
+ } ) ;
25
+
18
26
const MOCKS = [
19
27
{
20
28
request : {
@@ -75,6 +83,10 @@ const props: InterfaceOrgListCardProps = {
75
83
} ;
76
84
77
85
describe ( 'Testing the Super Dash List' , ( ) => {
86
+ beforeEach ( ( ) => {
87
+ vi . clearAllMocks ( ) ;
88
+ } ) ;
89
+
78
90
test ( 'should render props and text elements test for the page component' , async ( ) => {
79
91
removeItem ( 'id' ) ;
80
92
setItem ( 'id' , '123' ) ; // Means the user is an admin
@@ -88,22 +100,24 @@ describe('Testing the Super Dash List', () => {
88
100
</ BrowserRouter >
89
101
</ MockedProvider > ,
90
102
) ;
103
+
91
104
await wait ( ) ;
92
- expect ( screen . getByAltText ( / D o g s C a r e i m a g e / i) ) . toBeInTheDocument ( ) ;
93
- expect ( screen . getByText ( / A d m i n s : / i) ) . toBeInTheDocument ( ) ;
94
- expect ( screen . getByText ( / M e m b e r s : / i) ) . toBeInTheDocument ( ) ;
95
- expect ( screen . getByText ( 'Dogs Care' ) ) . toBeInTheDocument ( ) ;
96
- expect ( screen . getByText ( / S a m p l e C i t y / i) ) . toBeInTheDocument ( ) ;
97
- expect ( screen . getByText ( / 1 2 3 S a m p l e S t r e e t / i) ) . toBeInTheDocument ( ) ;
98
- expect ( screen . getByTestId ( / m a n a g e B t n / i) ) . toBeInTheDocument ( ) ;
99
- expect ( screen . getByTestId ( / f l a s k I c o n / i) ) . toBeInTheDocument ( ) ;
100
- userEvent . click ( screen . getByTestId ( / m a n a g e B t n / i) ) ;
105
+
106
+ expect ( screen . getByAltText ( / D o g s C a r e i m a g e / i) ) . toBeDefined ( ) ;
107
+ expect ( screen . getByText ( / A d m i n s : / i) ) . toBeDefined ( ) ;
108
+ expect ( screen . getByText ( / M e m b e r s : / i) ) . toBeDefined ( ) ;
109
+ expect ( screen . getByText ( 'Dogs Care' ) ) . toBeDefined ( ) ;
110
+ expect ( screen . getByText ( / S a m p l e C i t y / i) ) . toBeDefined ( ) ;
111
+ expect ( screen . getByText ( / 1 2 3 S a m p l e S t r e e t / i) ) . toBeDefined ( ) ;
112
+ expect ( screen . getByTestId ( / m a n a g e B t n / i) ) . toBeDefined ( ) ;
113
+ expect ( screen . getByTestId ( / f l a s k I c o n / i) ) . toBeDefined ( ) ;
114
+
115
+ await userEvent . click ( screen . getByTestId ( / m a n a g e B t n / i) ) ;
101
116
removeItem ( 'id' ) ;
102
117
} ) ;
103
118
104
119
test ( 'Testing if the props data is not provided' , ( ) => {
105
120
window . location . assign ( '/orgdash' ) ;
106
-
107
121
render (
108
122
< MockedProvider addTypename = { false } link = { link } >
109
123
< BrowserRouter >
@@ -114,14 +128,15 @@ describe('Testing the Super Dash List', () => {
114
128
</ MockedProvider > ,
115
129
) ;
116
130
117
- expect ( window . location ) . toBeAt ( '/orgdash' ) ;
131
+ expect ( mockAssign ) . toHaveBeenCalledWith ( '/orgdash' ) ;
118
132
} ) ;
119
133
120
134
test ( 'Testing if component is rendered properly when image is null' , ( ) => {
121
135
const imageNullProps = {
122
136
...props ,
123
137
...{ data : { ...props . data , ...{ image : null } } } ,
124
138
} ;
139
+
125
140
render (
126
141
< MockedProvider addTypename = { false } link = { link } >
127
142
< BrowserRouter >
@@ -131,10 +146,11 @@ describe('Testing the Super Dash List', () => {
131
146
</ BrowserRouter >
132
147
</ MockedProvider > ,
133
148
) ;
134
- expect ( screen . getByTestId ( / e m p t y C o n t a i n e r F o r I m a g e / i) ) . toBeInTheDocument ( ) ;
149
+
150
+ expect ( screen . getByTestId ( / e m p t y C o n t a i n e r F o r I m a g e / i) ) . toBeDefined ( ) ;
135
151
} ) ;
136
152
137
- test ( 'Testing if user is redirected to orgDash screen' , ( ) => {
153
+ test ( 'Testing if user is redirected to orgDash screen' , async ( ) => {
138
154
render (
139
155
< MockedProvider addTypename = { false } link = { link } >
140
156
< BrowserRouter >
@@ -144,6 +160,7 @@ describe('Testing the Super Dash List', () => {
144
160
</ BrowserRouter >
145
161
</ MockedProvider > ,
146
162
) ;
147
- userEvent . click ( screen . getByTestId ( 'manageBtn' ) ) ;
163
+
164
+ await userEvent . click ( screen . getByTestId ( 'manageBtn' ) ) ;
148
165
} ) ;
149
166
} ) ;
0 commit comments