Skip to content

Commit

Permalink
test(carousel): adds test to increase coverage (#6393)
Browse files Browse the repository at this point in the history
* feat(carousel): starts adding tests

* refactor(carousel): removes utils file

* feat(carousel): adds onScroll tests

* feat(carousel): adds reset scroll test and wheel disable

* feat(carousel): adds tests

---------

Co-authored-by: Anamika T S <[email protected]>
  • Loading branch information
AlexanderMelox and anamikaanu96 authored Dec 3, 2024
1 parent cc15060 commit b2298b5
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion packages/ibm-products/src/components/Carousel/Carousel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import React from 'react';
import { render, screen } from '@testing-library/react'; // https://testing-library.com/docs/react-testing-library/intro
import { fireEvent, render, screen, waitFor } from '@testing-library/react'; // https://testing-library.com/docs/react-testing-library/intro

import { pkg } from '../../settings';
import uuidv4 from '../../global/js/utils/uuidv4';
Expand Down Expand Up @@ -78,4 +78,63 @@ describe(componentName, () => {
componentName
);
});

it('calls the onScroll prop and returns value from 0 to 1', async () => {
const onScroll = jest.fn().mockReturnValue(0.2);
render(
<Carousel
data-testid={dataTestId}
// eslint-disable-next-line
style={{ width: '10px', height: '20px' }} // we need to set width and height here to trigger scrollbars
onScroll={onScroll}
>
Very long paragraph to trigger scrolling.
</Carousel>
);

const element = screen.getByTestId(dataTestId);
expect(element).not.toBeNull();

await waitFor(() =>
fireEvent.scroll(element, { target: { scrollX: '20px' } })
);
expect(onScroll).toHaveBeenCalled();
expect(onScroll()).toBe(0.2);
});

it('resets the scroll when the window size changes', async () => {
const onScroll = jest.fn().mockReturnValue(0.2);

render(
<Carousel
data-testid={dataTestId}
// eslint-disable-next-line
style={{ width: '10px', height: '20px' }} // we need to set width and height here to trigger scrollbars
onScroll={onScroll}
>
Very long paragraph to trigger scrolling.
</Carousel>
);

const element = screen.getByTestId(dataTestId);
expect(onScroll).toHaveBeenCalledTimes(0);
expect(element.scrollLeft).toBe(0);

const scrollXAmount = 20;
await waitFor(() =>
fireEvent.scroll(element, { target: { scrollX: scrollXAmount } })
);
element.scrollLeft = scrollXAmount;
expect(element.scrollLeft).toBe(scrollXAmount);

// Change the viewport to 500px.
global.innerWidth = 500;

// Trigger the window resize event.
global.dispatchEvent(new Event('resize'));

expect(onScroll).toHaveBeenCalled();
element.scrollLeft = 0;
expect(element.scrollLeft).toBe(0);
});
});

0 comments on commit b2298b5

Please sign in to comment.