Skip to content

Commit 1be3db2

Browse files
authored
Merge pull request #36 from oslabs-beta/Ahnafkhvn/testing
Ahnafkhvn/testing
2 parents d20ba13 + feac4fd commit 1be3db2

File tree

2 files changed

+160
-1
lines changed

2 files changed

+160
-1
lines changed

__tests__/NavBar.test.tsx

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import React from 'react';
2+
import { render, fireEvent, waitFor } from '@testing-library/react';
3+
import { MemoryRouter } from 'react-router-dom';
4+
import { ThemeProvider, createTheme } from '@mui/material/styles';
5+
import NavBar from '../app/src/components/top/NavBar';
6+
import * as projectFunctions from '../app/src/helperFunctions/projectGetSaveDel';
7+
import { Provider } from 'react-redux';
8+
import { configureStore } from '@reduxjs/toolkit';
9+
import rootReducer from '../app/src/redux/reducers/rootReducer';
10+
import { initialState as appStateInitialState } from '../app/src/redux/reducers/slice/appStateSlice';
11+
12+
// Mocking the theme
13+
const theme = createTheme({
14+
spacing: (value) => `${value}px`,
15+
});
16+
17+
// Mocking the logo
18+
jest.mock('../../public/icons/win/logo.png', () => 'dummy-image-url');
19+
20+
// Grabbing publish and unpublish functions
21+
jest.mock('../app/src/helperFunctions/projectGetSaveDel', () => ({
22+
publishProject: jest.fn(),
23+
unpublishProject: jest.fn(),
24+
}));
25+
26+
const originalError = console.error;
27+
beforeAll(() => {
28+
console.error = jest.fn();
29+
});
30+
31+
afterAll(() => {
32+
console.error = originalError;
33+
});
34+
35+
// Mocking the render
36+
const renderNavBar = (store) => {
37+
return render(
38+
<Provider store={store}>
39+
<MemoryRouter>
40+
<ThemeProvider theme={theme}>
41+
<NavBar />
42+
</ThemeProvider>
43+
</MemoryRouter>
44+
</Provider>
45+
);
46+
};
47+
48+
describe('NavBar Component', () => {
49+
it('handles publish correctly with saved project', async () => {
50+
const publishProjectMock = jest.spyOn(projectFunctions, 'publishProject');
51+
publishProjectMock.mockResolvedValueOnce({
52+
_id: 'mockedId',
53+
name: 'Mocked Project',
54+
published: true,
55+
});
56+
57+
const store = configureStore({
58+
reducer: rootReducer,
59+
preloadedState: {
60+
appState: {
61+
...appStateInitialState,
62+
isLoggedIn: true,
63+
name: 'Mock Project Name',
64+
},
65+
},
66+
});
67+
68+
console.log('Before rendering NavBar');
69+
70+
const { getByText } = renderNavBar(store);
71+
72+
console.log('After rendering NavBar');
73+
74+
const publishButton = getByText('Publish');
75+
fireEvent.click(publishButton);
76+
});
77+
78+
it('handles publish correctly with new project', async () => {
79+
const publishProjectMock = jest.spyOn(projectFunctions, 'publishProject');
80+
publishProjectMock.mockResolvedValueOnce({
81+
_id: 'mockedId',
82+
name: 'My Project',
83+
published: true,
84+
});
85+
86+
const store = configureStore({
87+
reducer: rootReducer,
88+
preloadedState: {
89+
appState: {
90+
...appStateInitialState,
91+
isLoggedIn: true,
92+
name: '',
93+
},
94+
},
95+
});
96+
97+
console.log('Before rendering NavBar');
98+
99+
const { getByText, queryByText, getByTestId, queryByTestId } = renderNavBar(store);
100+
101+
console.log('After rendering NavBar');
102+
103+
// Check if the "Publish" button is present
104+
const publishButton = queryByText('Publish');
105+
106+
if (publishButton) {
107+
fireEvent.click(publishButton);
108+
} else {
109+
// If "Publish" button is not found, look for the "Unpublish" button
110+
const unpublishButton = getByText('Unpublish');
111+
fireEvent.click(unpublishButton);
112+
}
113+
114+
// Check if the modal for a new project is displayed
115+
const projectNameInput = queryByTestId('project-name-input');
116+
117+
if (projectNameInput) {
118+
// entering a project name in the modal
119+
fireEvent.change(projectNameInput, { target: { value: 'My Project' } });
120+
}
121+
});
122+
123+
124+
it('handles unpublish correctly', async () => {
125+
const unpublishProjectMock = jest.spyOn(projectFunctions, 'unpublishProject');
126+
unpublishProjectMock.mockResolvedValueOnce({
127+
_id: 'mockedId',
128+
name: 'Mocked Project',
129+
published: false,
130+
});
131+
132+
const store = configureStore({
133+
reducer: rootReducer,
134+
preloadedState: {
135+
appState: {
136+
...appStateInitialState,
137+
isLoggedIn: true,
138+
name: 'Mock Project Name',
139+
},
140+
},
141+
});
142+
143+
console.log('Before rendering NavBar');
144+
145+
const { queryByText } = renderNavBar(store);
146+
147+
console.log('After rendering NavBar');
148+
149+
// Find the "Publish" or "Unpublish" button based on the project's publish state
150+
const publishButton = queryByText('Publish');
151+
const unpublishButton = queryByText('Unpublish');
152+
153+
if (publishButton) {
154+
fireEvent.click(publishButton);
155+
} else if (unpublishButton) {
156+
fireEvent.click(unpublishButton);
157+
}
158+
});
159+
});

app/src/containers/AppContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ const AppContainer = () => {
6565
</StyledEngineProvider>
6666
);
6767
};
68-
export default AppContainer;
68+
export default AppContainer;

0 commit comments

Comments
 (0)