-
-
Couldn't load subscription status.
- Fork 245
Test: add unit tests for BarChart component #1801 #1904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
f03b10d
Add unit tests for BarChart component
MohdWaqar98 5fb29a1
Merge branch 'main' into test/bar-chart
MohdWaqar98 2e2e56e
Add unit tests for BarChart component
MohdWaqar98 25985ce
Merge branch 'main' into test/bar-chart
kasya c3375ac
Merge branch 'main' into test/bar-chart
MohdWaqar98 eafd291
Add unit tests for BarChart component
MohdWaqar98 074f35f
Merge branch 'main' into test/bar-chart
MohdWaqar98 4524897
Merge branch 'main' into test/bar-chart
kasya 6a48ae7
Fix linting issues
kasya 52f99ca
Merge branch 'main' into test/bar-chart
kasya 6351257
Merge branch 'main' into test/bar-chart
MohdWaqar98 4652037
Merge branch 'main' into test/bar-chart
MohdWaqar98 52cf954
Merge branch 'main' into test/bar-chart
MohdWaqar98 0050b77
Fixed unit tests for BarChart component
MohdWaqar98 a935130
Merge branch 'main' into test/bar-chart
MohdWaqar98 8346cf4
Fixed unit tests for BarChart component By CodeRabbit
MohdWaqar98 404ae8f
Fixed unit tests for BarChart component By CodeRabbit
MohdWaqar98 4ac3e2c
Merge branch 'main' into test/bar-chart
kasya 9c591d6
Fix tests and make check issues
kasya dd5cfe8
Merge branch 'main' into test/bar-chart
kasya a5dfaa7
Fixed unit tests for BarChart component
MohdWaqar98 e7cc35e
Merge branch 'main' into test/bar-chart
MohdWaqar98 968d4cc
Merge branch 'main' into test/bar-chart
MohdWaqar98 576c6cb
Merge branch 'main' into test/bar-chart
MohdWaqar98 01562d2
Merge branch 'main' into test/bar-chart
MohdWaqar98 da992c0
Merge branch 'main' into test/bar-chart
MohdWaqar98 1be87da
Merge branch 'main' into test/bar-chart
MohdWaqar98 63a56c6
Merge branch 'main' into test/bar-chart
kasya fe9db4f
Fix issues and add more tests
kasya f78d576
Merge branch 'main' into test/bar-chart
kasya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,130 @@ | ||
| import { library } from '@fortawesome/fontawesome-svg-core' | ||
| import { faFire } from '@fortawesome/free-solid-svg-icons' | ||
| import { act, render, screen } from '@testing-library/react' | ||
| import '@testing-library/jest-dom' | ||
| import { ThemeProvider } from 'next-themes' | ||
| import React from 'react' | ||
|
|
||
| import BarChart from 'components/BarChart' | ||
|
|
||
| // Register FontAwesome icon | ||
| library.add(faFire) | ||
|
|
||
| // Mock ApexCharts | ||
| jest.mock('react-apexcharts', () => { | ||
| const MockChart = () => <div data-testid="mock-chart" /> | ||
| return { | ||
| __esModule: true, | ||
| default: MockChart, | ||
| } | ||
| }) | ||
|
|
||
| // Silence known warnings | ||
| const originalError = console.error | ||
MohdWaqar98 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| beforeAll(() => { | ||
| jest.spyOn(console, 'error').mockImplementation((...args) => { | ||
| const [message] = args | ||
| if ( | ||
| typeof message === 'string' && | ||
| (message.includes('act(...)') || | ||
| message.includes('not wrapped in act') || | ||
| message.includes('LoadableComponent') || | ||
| message.includes('useLayoutEffect')) | ||
| ) { | ||
| return | ||
| } | ||
| originalError(...args) | ||
| }) | ||
| }) | ||
|
|
||
| // Utility to render with act + theme | ||
| const renderWithTheme = async ( | ||
| ui: React.ReactElement, | ||
| theme: 'light' | 'dark' = 'light' | ||
| ) => { | ||
| let result: ReturnType<typeof render> | undefined | ||
| await act(async () => { | ||
| result = render( | ||
| <ThemeProvider attribute="class" forcedTheme={theme}> | ||
| {ui} | ||
| </ThemeProvider> | ||
| ) | ||
| }) | ||
| return result! | ||
| } | ||
|
|
||
| // Common test props | ||
| const mockProps = { | ||
| title: 'Calories Burned', | ||
| labels: ['Mon', 'Tue', 'Wed'], | ||
| days: [200, 150, 100], | ||
| requirements: [180, 170, 90], | ||
| } | ||
|
|
||
| describe('<BarChart />', () => { | ||
| it('renders without crashing with minimal props', async () => { | ||
| await renderWithTheme(<BarChart {...mockProps} />) | ||
| expect(screen.getByText('Calories Burned')).toBeInTheDocument() | ||
| expect(screen.getByTestId('mock-chart')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('renders with icon when provided', async () => { | ||
| // cspell:ignore fas | ||
| await renderWithTheme(<BarChart {...mockProps} icon={['fas', 'fire']} />) | ||
| expect(screen.getByText('Calories Burned')).toBeInTheDocument() | ||
| expect(document.querySelector('[data-icon="fire"]')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('renders correctly in dark mode', async () => { | ||
| await renderWithTheme(<BarChart {...mockProps} />, 'dark') | ||
| expect(screen.getByText('Calories Burned')).toBeInTheDocument() | ||
| expect(screen.getByTestId('mock-chart')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('handles empty data arrays without crashing', async () => { | ||
| const emptyProps = { | ||
| title: 'Empty Chart', | ||
| labels: [], | ||
| days: [], | ||
| requirements: [], | ||
| } | ||
| await renderWithTheme(<BarChart {...emptyProps} />) | ||
| expect(screen.getByText('Empty Chart')).toBeInTheDocument() | ||
| expect(screen.getByTestId('mock-chart')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('handles reverseColors logic correctly', async () => { | ||
| const customProps = { | ||
| ...mockProps, | ||
| reverseColors: [true, false, true], | ||
| } | ||
| await renderWithTheme(<BarChart {...customProps} />) | ||
| expect(screen.getByTestId('mock-chart')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('renders chart container with proper structure', async () => { | ||
| await renderWithTheme(<BarChart {...mockProps} />) | ||
| const chartElement = screen.getByTestId('mock-chart') | ||
| expect(chartElement).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('handles mismatched array lengths gracefully', async () => { | ||
| const mismatchedProps = { | ||
| ...mockProps, | ||
| days: [200, 150], // Only 2 entries instead of 3 | ||
| } | ||
| await renderWithTheme(<BarChart {...mismatchedProps} />) | ||
| expect(screen.getByText('Calories Burned')).toBeInTheDocument() | ||
| expect(screen.getByTestId('mock-chart')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('renders title correctly', async () => { | ||
| await renderWithTheme(<BarChart {...mockProps} />) | ||
| expect(screen.getByText('Calories Burned')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('renders chart component when data is provided', async () => { | ||
| await renderWithTheme(<BarChart {...mockProps} />) | ||
| expect(screen.getByTestId('mock-chart')).toBeInTheDocument() | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.