Skip to content

Commit

Permalink
(tests) O3-4152 Add tests for range selector component
Browse files Browse the repository at this point in the history
  • Loading branch information
CynthiaKamau committed Nov 13, 2024
1 parent 7b6eb1a commit 21e43c8
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';
import RangeSelector from './range-selector.component';

describe('RangeSelector', () => {
const mockSetLowerRange = jest.fn();

beforeEach(() => {
mockSetLowerRange.mockClear();
});

it('should render all range tabs', () => {
const upperRange = new Date('2024-12-31');
render(<RangeSelector setLowerRange={mockSetLowerRange} upperRange={upperRange} />);

expect(screen.getByRole('tab', { name: '1 day' })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: '5 days' })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: '1 month' })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: '6 months' })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: '1 year' })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: '5 years' })).toBeInTheDocument();
expect(screen.getByRole('tab', { name: 'All' })).toBeInTheDocument();
});

it.each([
['1 day', 1],
['5 days', 5],
['1 month', 30],
['6 months', 182],
['1 year', 365],
['5 years', 5 * 365],
])('should set lower range correctly for %s', async (label, days) => {
const upperRange = new Date('2024-12-31');
render(<RangeSelector setLowerRange={mockSetLowerRange} upperRange={upperRange} />);

const expectedLowerRange = new Date(upperRange.getTime() - days * 24 * 3600 * 1000);

await userEvent.click(screen.getByText(label));
expect(mockSetLowerRange).toHaveBeenCalledWith(expectedLowerRange);
});

it('should set lower range to start of epoch for "All" range', async () => {
const upperRange = new Date('2024-12-31');
render(<RangeSelector setLowerRange={mockSetLowerRange} upperRange={upperRange} />);

await userEvent.click(screen.getByRole('tab', { name: 'All' }));
expect(mockSetLowerRange).toHaveBeenCalledWith(new Date(0));
});
});

0 comments on commit 21e43c8

Please sign in to comment.