Skip to content

Commit

Permalink
Merge pull request #424 from bounswe/feature/FE-jest-test
Browse files Browse the repository at this point in the history
Add Jest Test for News and Dashboard
  • Loading branch information
mahmutbugramert authored Nov 25, 2024
2 parents 44eb864 + 6570040 commit a4ed8ab
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 42 deletions.
1 change: 1 addition & 0 deletions frontend/__mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "test-file-stub";
6 changes: 6 additions & 0 deletions frontend/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
'@babel/preset-env',
['@babel/preset-react', {runtime: 'automatic'}],
],
};
10 changes: 10 additions & 0 deletions frontend/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
transform: {
'^.+\\.[jt]sx?$': 'babel-jest', // Transform JS, JSX, TS, and TSX files
},
moduleNameMapper: {
'\\.(css|less|scss|sass)$': 'identity-obj-proxy', // Mock CSS imports
"\\.(png|jpg|jpeg|gif|webp|svg)$": "<rootDir>/__mocks__/fileMock.js",
},
testEnvironment: 'jsdom', // Simulates a browser-like environment
};
108 changes: 68 additions & 40 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"dependencies": {
"@fortawesome/free-solid-svg-icons": "^6.6.0",
"@fortawesome/react-fontawesome": "^0.2.2",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.7.7",
Expand All @@ -25,7 +24,7 @@
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test": "jest",
"eject": "react-scripts eject"
},
"eslintConfig": {
Expand All @@ -48,6 +47,8 @@
},
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@testing-library/jest-dom": "^6.6.3",
"react-test-renderer": "^18.3.1",
"@babel/preset-env": "^7.26.0",
"@babel/preset-react": "^7.25.9",
"babel-jest": "^29.7.0"
Expand Down
60 changes: 60 additions & 0 deletions frontend/src/test/Dashboard.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import Dashboard from "../components/Dashboard";
import '@testing-library/jest-dom'

jest.mock("react-toastify", () => ({
toast: { success: jest.fn() },
}));

describe("Dashboard Component", () => {
test("renders Sign In and Register buttons when not logged in", () => {
render(
<MemoryRouter>
<Dashboard />
</MemoryRouter>
);

expect(screen.getByText(/Sign In/i)).toBeInTheDocument();
expect(screen.getByText(/Register/i)).toBeInTheDocument();
});

test("renders user information and Sign Out button when logged in", () => {
localStorage.setItem("accessToken", "mockToken");
localStorage.setItem("userName", "TestUser");

render(
<MemoryRouter>
<Dashboard />
</MemoryRouter>
);

expect(screen.getByText("TestUser")).toBeInTheDocument();
expect(screen.getByText(/Sign Out/i)).toBeInTheDocument();
});

test("renders dark mode toggle button", () => {
render(
<MemoryRouter>
<Dashboard />
</MemoryRouter>
);

expect(screen.getByRole("button", { name: /sun|moon/i })).toBeInTheDocument();
});

test("renders navigation links", () => {
render(
<MemoryRouter>
<Dashboard />
</MemoryRouter>
);

expect(screen.getByText(/Home/i)).toBeInTheDocument();
expect(screen.getByText(/Community/i)).toBeInTheDocument();
expect(screen.getByText(/Markets/i)).toBeInTheDocument();
expect(screen.getByText(/News/i)).toBeInTheDocument();
expect(screen.getByText(/Portfolio/i)).toBeInTheDocument();
});
});
32 changes: 32 additions & 0 deletions frontend/src/test/NewsPage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import NewsPage from '../components/news/NewsPage';

describe('NewsPage Component', () => {
beforeEach(() => {
render(<NewsPage />);
});

test('renders page header', () => {
expect(screen.getByText('Economic News')).toBeInTheDocument();
expect(screen.getByText('Your daily updates on economic trends')).toBeInTheDocument();
});

test('renders mock news and RSS sections', () => {
expect(screen.getByText('Mock Economic News Section')).toBeInTheDocument();
expect(screen.getByText('Mock RSS Feed Economic News Section')).toBeInTheDocument();
});

test('renders news and RSS feed cards', () => {
const newsCards = screen.getAllByRole('button', { name: /news-card/i });
expect(newsCards.length).toBeGreaterThan(0);
});

test('shows all cards when "All" category is selected', () => {
fireEvent.click(screen.getByText('Stock Market'));
fireEvent.click(screen.getByText('All'));
const visibleCards = screen.getAllByRole('button', { name: /news-card/i });
expect(visibleCards.length).toBeGreaterThan(0);
});
});

0 comments on commit a4ed8ab

Please sign in to comment.