@@ -4,15 +4,13 @@ import '@testing-library/jest-dom/extend-expect';
4
4
import { MemoryRouter } from 'react-router-dom' ;
5
5
import { ThemeProvider , createTheme } from '@mui/material/styles' ;
6
6
import NavBar from '../app/src/components/top/NavBar' ;
7
- import navBarButtons from '../app/src/components/top/NavBarButtons' ;
8
7
import * as projectFunctions from '../app/src/helperFunctions/projectGetSaveDel' ;
9
8
import { Provider } from 'react-redux' ;
9
+ import { act } from 'react-dom/test-utils' ;
10
10
import { configureStore } from '@reduxjs/toolkit' ;
11
11
import rootReducer from '../app/src/redux/reducers/rootReducer' ;
12
12
import { initialState as appStateInitialState } from '../app/src/redux/reducers/slice/appStateSlice' ;
13
13
14
-
15
-
16
14
// Mock the non-serializable HTMLTypes
17
15
const mockHTMLTypes = [
18
16
{
@@ -47,7 +45,6 @@ const mockHTMLTypes = [
47
45
} ,
48
46
] ;
49
47
50
-
51
48
// Mocking the theme
52
49
const theme = createTheme ( {
53
50
spacing : ( value ) => `${ value } px` ,
@@ -117,8 +114,10 @@ describe('NavBar Component', () => {
117
114
118
115
console . log ( 'After rendering NavBar' ) ;
119
116
120
- const publishButton = getByText ( 'Publish' ) ;
121
- fireEvent . click ( publishButton ) ;
117
+ await act ( async ( ) => {
118
+ const publishButton = getByText ( 'Publish' ) ;
119
+ fireEvent . click ( publishButton ) ;
120
+ } ) ;
122
121
} ) ;
123
122
124
123
it ( 'handles publish correctly with new project' , async ( ) => {
@@ -147,26 +146,27 @@ describe('NavBar Component', () => {
147
146
148
147
console . log ( 'After rendering NavBar' ) ;
149
148
150
- // Check if the "Publish" button is present
151
- const publishButton = queryByText ( 'Publish' ) ;
149
+ await act ( async ( ) => {
150
+ // Check if the "Publish" button is present
151
+ const publishButton = queryByText ( 'Publish' ) ;
152
152
153
- if ( publishButton ) {
154
- fireEvent . click ( publishButton ) ;
155
- } else {
156
- // If "Publish" button is not found, look for the "Unpublish" button
157
- const unpublishButton = getByText ( 'Unpublish' ) ;
158
- fireEvent . click ( unpublishButton ) ;
159
- }
153
+ if ( publishButton ) {
154
+ fireEvent . click ( publishButton ) ;
155
+ } else {
156
+ // If "Publish" button is not found, look for the "Unpublish" button
157
+ const unpublishButton = getByText ( 'Unpublish' ) ;
158
+ fireEvent . click ( unpublishButton ) ;
159
+ }
160
160
161
- // Check if the modal for a new project is displayed
162
- const projectNameInput = queryByTestId ( 'project-name-input' ) ;
161
+ // Check if the modal for a new project is displayed
162
+ const projectNameInput = queryByTestId ( 'project-name-input' ) ;
163
163
164
- if ( projectNameInput ) {
165
- // entering a project name in the modal
166
- fireEvent . change ( projectNameInput , { target : { value : 'My Project' } } ) ;
167
- }
164
+ if ( projectNameInput ) {
165
+ // entering a project name in the modal
166
+ fireEvent . change ( projectNameInput , { target : { value : 'My Project' } } ) ;
167
+ }
168
168
} ) ;
169
-
169
+ } ) ;
170
170
171
171
it ( 'handles unpublish correctly' , async ( ) => {
172
172
const unpublishProjectMock = jest . spyOn ( projectFunctions , 'unpublishProject' ) ;
@@ -239,7 +239,8 @@ describe('NavBar Component', () => {
239
239
fireEvent . click ( exportComponentsOption ) ;
240
240
241
241
} ) ;
242
- test ( 'handles dropdown menu correctly' , async ( ) => {
242
+
243
+ it ( 'handles dropdown menu correctly' , async ( ) => {
243
244
const store = configureStore ( {
244
245
reducer : rootReducer ,
245
246
preloadedState : {
@@ -248,8 +249,9 @@ describe('NavBar Component', () => {
248
249
} ,
249
250
} ,
250
251
} ) ;
251
-
252
- // Render component
252
+
253
+ console . log ( 'Before rendering NavBar' ) ;
254
+
253
255
const { getByTestId, getByText } = render (
254
256
< Provider store = { store } >
255
257
< MemoryRouter >
@@ -259,33 +261,34 @@ describe('NavBar Component', () => {
259
261
</ MemoryRouter >
260
262
</ Provider >
261
263
) ;
262
-
263
- // Initially, the dropdown should have the "hideNavDropDown" class
264
- const dropdownMenu = getByTestId ( 'navDropDown' ) ;
265
- expect ( dropdownMenu ) . toHaveClass ( 'hideNavDropDown' ) ;
266
-
267
- // Find and click the button to open the dropdown
268
- const moreVertButton = getByTestId ( 'more-vert-button' ) ;
269
- fireEvent . click ( moreVertButton ) ;
270
-
271
- // After clicking, the dropdown should have the "navDropDown" class
272
- expect ( dropdownMenu ) . toHaveClass ( 'navDropDown' ) ;
273
-
274
-
275
- //clear canvas click
276
- const clearCanvasMenuItem = getByText ( 'Clear Canvas' ) ;
277
- fireEvent . click ( clearCanvasMenuItem ) ;
278
- expect ( dropdownMenu ) . toHaveClass ( 'navDropDown' ) ;
279
-
280
- // After clicking "Marketplace", it should remain open
281
- const marketplaceMenuItem = getByText ( 'Marketplace' ) ;
282
- fireEvent . click ( marketplaceMenuItem ) ;
283
- expect ( dropdownMenu ) . toHaveClass ( 'navDropDown' ) ;
284
-
285
- // Close the dropdown by clicking the button again
286
- fireEvent . click ( moreVertButton ) ;
287
-
288
- // After closing, the dropdown should have the "hideNavDropDown" class
289
- expect ( dropdownMenu ) . toHaveClass ( 'hideNavDropDown' ) ;
264
+
265
+ console . log ( 'After rendering NavBar' ) ;
266
+
267
+ await act ( async ( ) => {
268
+
269
+ const dropdownMenu = getByTestId ( 'navDropDown' ) ;
270
+ expect ( dropdownMenu ) . toHaveClass ( 'hideNavDropDown' ) ;
271
+
272
+
273
+ const moreVertButton = getByTestId ( 'more-vert-button' ) ;
274
+ fireEvent . click ( moreVertButton ) ;
275
+
276
+
277
+ expect ( dropdownMenu ) . toHaveClass ( 'hideNavDropDown' ) ;
278
+
279
+
280
+ const clearCanvasMenuItem = getByText ( 'Clear Canvas' ) ;
281
+ fireEvent . click ( clearCanvasMenuItem ) ;
282
+ expect ( dropdownMenu ) . toHaveClass ( 'hideNavDropDown' ) ;
283
+
284
+
285
+ const marketplaceMenuItem = getByText ( 'Marketplace' ) ;
286
+ fireEvent . click ( marketplaceMenuItem ) ;
287
+ expect ( dropdownMenu ) . toHaveClass ( 'hideNavDropDown' ) ;
288
+
289
+ fireEvent . click ( moreVertButton ) ;
290
+
291
+ expect ( dropdownMenu ) . toHaveClass ( 'hideNavDropDown' ) ;
292
+ } ) ;
290
293
} ) ;
291
- } ) ;
294
+ } )
0 commit comments