Skip to content
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 Jul 29, 2025
5fb29a1
Merge branch 'main' into test/bar-chart
MohdWaqar98 Jul 29, 2025
2e2e56e
Add unit tests for BarChart component
MohdWaqar98 Jul 29, 2025
25985ce
Merge branch 'main' into test/bar-chart
kasya Jul 30, 2025
c3375ac
Merge branch 'main' into test/bar-chart
MohdWaqar98 Jul 30, 2025
eafd291
Add unit tests for BarChart component
MohdWaqar98 Jul 30, 2025
074f35f
Merge branch 'main' into test/bar-chart
MohdWaqar98 Jul 30, 2025
4524897
Merge branch 'main' into test/bar-chart
kasya Jul 31, 2025
6a48ae7
Fix linting issues
kasya Jul 31, 2025
52f99ca
Merge branch 'main' into test/bar-chart
kasya Jul 31, 2025
6351257
Merge branch 'main' into test/bar-chart
MohdWaqar98 Jul 31, 2025
4652037
Merge branch 'main' into test/bar-chart
MohdWaqar98 Jul 31, 2025
52cf954
Merge branch 'main' into test/bar-chart
MohdWaqar98 Aug 1, 2025
0050b77
Fixed unit tests for BarChart component
MohdWaqar98 Aug 1, 2025
a935130
Merge branch 'main' into test/bar-chart
MohdWaqar98 Aug 1, 2025
8346cf4
Fixed unit tests for BarChart component By CodeRabbit
MohdWaqar98 Aug 1, 2025
404ae8f
Fixed unit tests for BarChart component By CodeRabbit
MohdWaqar98 Aug 1, 2025
4ac3e2c
Merge branch 'main' into test/bar-chart
kasya Aug 2, 2025
9c591d6
Fix tests and make check issues
kasya Aug 2, 2025
dd5cfe8
Merge branch 'main' into test/bar-chart
kasya Aug 2, 2025
a5dfaa7
Fixed unit tests for BarChart component
MohdWaqar98 Aug 2, 2025
e7cc35e
Merge branch 'main' into test/bar-chart
MohdWaqar98 Aug 3, 2025
968d4cc
Merge branch 'main' into test/bar-chart
MohdWaqar98 Aug 3, 2025
576c6cb
Merge branch 'main' into test/bar-chart
MohdWaqar98 Aug 4, 2025
01562d2
Merge branch 'main' into test/bar-chart
MohdWaqar98 Aug 6, 2025
da992c0
Merge branch 'main' into test/bar-chart
MohdWaqar98 Aug 6, 2025
1be87da
Merge branch 'main' into test/bar-chart
MohdWaqar98 Aug 7, 2025
63a56c6
Merge branch 'main' into test/bar-chart
kasya Aug 13, 2025
fe9db4f
Fix issues and add more tests
kasya Aug 16, 2025
f78d576
Merge branch 'main' into test/bar-chart
kasya Aug 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions frontend/__tests__/unit/components/BarChart.test.tsx
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
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()
})
})
Loading