Skip to content

Commit 63709e4

Browse files
committed
FDG-8051 test changes for coverage boost
1 parent 6aca1a3 commit 63709e4

File tree

1 file changed

+65
-17
lines changed

1 file changed

+65
-17
lines changed
Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
import React from 'react';
2-
import { render, fireEvent } from '@testing-library/react';
2+
import { render, fireEvent, waitFor } from '@testing-library/react';
33
import BarGraph from './bar';
44
import { staggeredData } from '../helpers/helpersData';
55
import helpers from './helpers/helpers';
66

7-
class ResizeObserver {
8-
observe() {}
9-
unobserve() {}
10-
disconnect() {}
11-
}
7+
jest.mock('@nivo/bar', () => ({
8+
Bar: jest.fn(({ barComponent: BarComponent, data, indexBy, keys }) => {
9+
return (
10+
<div data-testid="mock-nivo-bar">
11+
{data.map((item, index) => (
12+
<div key={index}>{BarComponent && <BarComponent index={index} value={item[keys[0]]} data={item} />}</div>
13+
))}
14+
</div>
15+
);
16+
}),
17+
}));
18+
19+
jest.mock('./bar-component/bar-component', () => ({ index, value, handleTempValueChange }) => {
20+
return <button data-testid={`custom-bar-trigger-${index}`} onClick={() => handleTempValueChange(index, value)} />;
21+
});
1222

1323
describe('BarGraph component', () => {
14-
window.ResizeObserver = ResizeObserver;
1524
it('does not render anything if invalid params are detected', () => {
1625
const { queryByTestId } = render(<BarGraph />);
1726
expect(queryByTestId('barGraph')).toBeNull();
@@ -24,23 +33,62 @@ describe('BarGraph component', () => {
2433
});
2534

2635
describe('BarGraph component - Custom bar graph', () => {
27-
window.ResizeObserver = ResizeObserver;
2836
const mouseEnterPropSpy = jest.fn();
29-
30-
const barGraph = (
31-
<BarGraph graphData={staggeredData} graphIndex="year" valueKeys={['value']} useCustomBarComponent mouseEnter={mouseEnterPropSpy} />
32-
);
37+
const mockSetTempValue = jest.fn();
38+
const mockSetTempDate = jest.fn();
3339
const mouseEnterSpy = jest.spyOn(helpers, 'mouseEnterEvent');
3440
const mouseLeaveSpy = jest.spyOn(helpers, 'mouseLeaveEvent');
3541

36-
it('triggers mouseEnter and mouseLeave events', () => {
42+
const barGraphProps = {
43+
graphData: staggeredData,
44+
graphIndex: 'year',
45+
valueKeys: ['value'],
46+
useCustomBarComponent: true,
47+
mouseEnter: mouseEnterPropSpy,
48+
setTempValue: mockSetTempValue,
49+
setTempDate: mockSetTempDate,
50+
dateField: 'year',
51+
};
52+
53+
beforeEach(() => {
54+
jest.clearAllMocks();
55+
// needed so mouseLeave executes callback
56+
mouseLeaveSpy.mockImplementation((cardId, callback) => {
57+
if (callback && typeof callback === 'function') {
58+
callback();
59+
}
60+
});
61+
});
62+
63+
it('triggers the mouseEnter and mouseLeave events', () => {
3764
jest.clearAllMocks();
38-
const { getByTestId } = render(barGraph);
39-
const container = getByTestId('barGraph');
40-
fireEvent.mouseEnter(container);
65+
const { getByTestId } = render(<BarGraph {...barGraphProps} />);
66+
const chartContainer = getByTestId('barGraph');
67+
fireEvent.mouseEnter(chartContainer);
4168
expect(mouseEnterSpy).toHaveBeenCalledTimes(1);
4269
expect(mouseEnterPropSpy).toHaveBeenCalled();
43-
fireEvent.mouseLeave(container);
70+
fireEvent.mouseLeave(chartContainer);
71+
expect(mouseLeaveSpy).toHaveBeenCalledTimes(1);
72+
});
73+
74+
it('calls setTempValue and setTempDate with the correct values when the bar is clicked', async () => {
75+
const { getByTestId } = render(<BarGraph {...barGraphProps} />);
76+
const firstBar = getByTestId('custom-bar-trigger-0');
77+
fireEvent.click(firstBar);
78+
await waitFor(() => {
79+
expect(mockSetTempValue).toHaveBeenCalledWith('500');
80+
expect(mockSetTempDate).toHaveBeenCalledWith('2017');
81+
});
82+
});
83+
84+
it('resets temp states when mouse leaves the bar', async () => {
85+
const { getByTestId } = render(<BarGraph {...barGraphProps} />);
86+
const chartContainer = getByTestId('barGraph');
87+
fireEvent.mouseLeave(chartContainer);
4488
expect(mouseLeaveSpy).toHaveBeenCalledTimes(1);
89+
await waitFor(() => {
90+
expect(mockSetTempValue).toHaveBeenCalledWith(null);
91+
expect(mockSetTempDate).toHaveBeenCalledWith(null);
92+
});
4593
});
4694
});

0 commit comments

Comments
 (0)