Skip to content
Merged
Changes from 15 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
206 changes: 206 additions & 0 deletions frontend/__tests__/unit/components/BarChart.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
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 = ({ options, series }: any) => (
<div
data-testid="mock-chart"
data-options={JSON.stringify(options)}
data-series={JSON.stringify(series)}
/>
)
return {
__esModule: true,
default: MockChart,
}
})

// 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 custom icon when provided', async () => {
// cspell:ignore fas
await renderWithTheme(<BarChart {...mockProps} icon={['fas', 'fire']} />)
expect(screen.getByText('Calories Burned')).toBeInTheDocument()

// Check for custom FontAwesome fire icon
const fireIconElement = document.querySelector('svg[data-icon="fire"]')
expect(fireIconElement).toBeInTheDocument()

// Component renders both default and custom icons
const defaultIconElement = document.querySelector('svg[data-icon="link"]')
expect(defaultIconElement).toBeInTheDocument()

// Should have multiple icons when custom icon is provided
const allIcons = document.querySelectorAll('svg[data-icon]')
expect(allIcons.length).toBeGreaterThan(1)
})

it('renders with default icon when icon prop not provided', async () => {
await renderWithTheme(<BarChart {...mockProps} />)
expect(screen.getByText('Calories Burned')).toBeInTheDocument()

// Should render default link icon when no icon prop is provided
const defaultIconElement = document.querySelector('svg[data-icon="link"]')
expect(defaultIconElement).toBeInTheDocument()

// Should only have the default icon
const allIcons = document.querySelectorAll('svg[data-icon]')
expect(allIcons.length).toBe(1)
})

it('renders correctly in light mode', async () => {
await renderWithTheme(<BarChart {...mockProps} />, 'light')
expect(screen.getByText('Calories Burned')).toBeInTheDocument()
expect(screen.getByTestId('mock-chart')).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 single data point correctly', async () => {
const singleDataProps = {
title: 'Single Day',
labels: ['Mon'],
days: [200],
requirements: [180],
}
await renderWithTheme(<BarChart {...singleDataProps} />)
expect(screen.getByText('Single Day')).toBeInTheDocument()
expect(screen.getByTestId('mock-chart')).toBeInTheDocument()
})

it('handles reverseColors when provided', async () => {
const reverseColorsProps = {
...mockProps,
reverseColors: [true, false, true],
}
await renderWithTheme(<BarChart {...reverseColorsProps} />)
expect(screen.getByTestId('mock-chart')).toBeInTheDocument()
})

it('handles reverseColors when not provided', async () => {
await renderWithTheme(<BarChart {...mockProps} />)
expect(screen.getByTestId('mock-chart')).toBeInTheDocument()
})

it('handles mismatched array lengths gracefully', async () => {
const mismatchedProps = {
...mockProps,
days: [200, 150], // Only 2 entries instead of 3
requirements: [180], // Only 1 entry
}
await renderWithTheme(<BarChart {...mismatchedProps} />)
expect(screen.getByText('Calories Burned')).toBeInTheDocument()
expect(screen.getByTestId('mock-chart')).toBeInTheDocument()
})

it('passes correct data to chart component', async () => {
await renderWithTheme(<BarChart {...mockProps} />)
const chartElement = screen.getByTestId('mock-chart')

// Verify that chart receives the data
expect(chartElement).toHaveAttribute('data-series')
expect(chartElement).toHaveAttribute('data-options')
})

it('renders with different title', async () => {
const customTitleProps = {
...mockProps,
title: 'Custom Chart Title',
}
await renderWithTheme(<BarChart {...customTitleProps} />)
expect(screen.getByText('Custom Chart Title')).toBeInTheDocument()
expect(screen.queryByText('Calories Burned')).not.toBeInTheDocument()
})

it('handles zero values in data arrays', async () => {
const zeroDataProps = {
...mockProps,
days: [0, 150, 0],
requirements: [0, 170, 0],
}
await renderWithTheme(<BarChart {...zeroDataProps} />)
expect(screen.getByText('Calories Burned')).toBeInTheDocument()
expect(screen.getByTestId('mock-chart')).toBeInTheDocument()
})

it('handles negative values in data arrays', async () => {
const negativeDataProps = {
...mockProps,
days: [-50, 150, -30],
requirements: [-40, 170, -20],
}
await renderWithTheme(<BarChart {...negativeDataProps} />)
expect(screen.getByText('Calories Burned')).toBeInTheDocument()
expect(screen.getByTestId('mock-chart')).toBeInTheDocument()
})

it('handles large numbers in data arrays', async () => {
const largeDataProps = {
...mockProps,
days: [20000, 15000, 10000],
requirements: [18000, 17000, 9000],
}
await renderWithTheme(<BarChart {...largeDataProps} />)
expect(screen.getByText('Calories Burned')).toBeInTheDocument()
expect(screen.getByTestId('mock-chart')).toBeInTheDocument()
})

it('renders chart container with proper accessibility', async () => {
await renderWithTheme(<BarChart {...mockProps} />)
const chartElement = screen.getByTestId('mock-chart')
expect(chartElement).toBeInTheDocument()
expect(chartElement).toBeVisible()
})
})