Skip to content

Commit

Permalink
Merge pull request #57 from BUMETCS673/fix_frontend_test
Browse files Browse the repository at this point in the history
fixed the frontend test and added serverRequest test
  • Loading branch information
eddie27lee authored Oct 6, 2024
2 parents be0669b + 67ffa70 commit 74d4573
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
7 changes: 3 additions & 4 deletions code/client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import AccountCircleIcon from "@mui/icons-material/AccountCircle";
import EditNoteIcon from "@mui/icons-material/EditNote";
import TrackChangesIcon from "@mui/icons-material/TrackChanges";
import CloseIcon from "@mui/icons-material/Close";
import BarChartIcon from "@mui/icons-material/BarChart";

import "@fontsource/mulish";

Expand All @@ -55,7 +54,7 @@ const theme = createTheme({



function App() {
function App({ RouterComponent = Router }) {
const [isAuthenticated, setIsAuthenticated] = useState(false);

// On component mount, check if user is already logged in
Expand All @@ -74,7 +73,7 @@ function App() {
};
return (
<ThemeProvider theme={theme}>
<Router>
<RouterComponent>
<Box sx={{ display: "flex" }}>
<CssBaseline />

Expand Down Expand Up @@ -225,7 +224,7 @@ function App() {
</Box>
</Box>
</Box>
</Router>
</RouterComponent>
</ThemeProvider>
);
}
Expand Down
46 changes: 33 additions & 13 deletions code/client/src/__tests__/frontend.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
import apiClient from '../services/apiClient.js'
// frontend.test.js
import { render, screen } from '@testing-library/react';
import App from '../App';

// tests
// test('test connection to server', async () => {
// const response = await apiClient.get('/check-connection');
// expect(response.status).toBe(200);
// })

test('renders home text', () => {
render(<App />);
const homeElement = screen.getByText(/welcome to the home page/i); // case-insensitive due to the regular expression /i
expect(homeElement).toBeInTheDocument();
import { MemoryRouter } from 'react-router-dom';
import App from '../App.js';
// import { authenticated } from '../utils/authenticate.js';

// Mock the charts
jest.mock('react-chartjs-2', () => ({
Line: () => <div>Mocked Line Chart</div>,
}));

// Mock the authenticated function to prevent redirection
jest.mock('../utils/authenticate.js', () => ({
authenticated: jest.fn(),
}));

beforeEach(() => {
// Simulate authenticated user
localStorage.setItem('authToken', 'mock-token');
});

afterEach(() => {
// Cleanup localStorage
localStorage.removeItem('authToken');
});

test('renders home page with mocked charts', () => {
render(
<App RouterComponent={MemoryRouter} />
);

const mockCharts = screen.getAllByText(/Mocked Line Chart/i);
expect(mockCharts[0]).toBeInTheDocument();
expect(mockCharts).toHaveLength(5); // Adjust based on the number of charts expected
});
7 changes: 7 additions & 0 deletions code/client/src/__tests__/serverRequest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import apiClient from '../services/apiClient.js'


test('test connection to server', async () => {
const response = await apiClient.get('/');
expect(response.status).toBe(200);
})

0 comments on commit 74d4573

Please sign in to comment.