-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #424 from bounswe/feature/FE-jest-test
Add Jest Test for News and Dashboard
- Loading branch information
Showing
7 changed files
with
180 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = "test-file-stub"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}], | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |