Add tests for scrolling-carousel.

master
Hasan Genc 3 years ago
parent c4460c5431
commit 16da9dda5b
  1. 69
      __tests__/scrollingCarousel.spec.tsx
  2. 9
      jest.config.js
  3. 2
      src/components/scrolling-carousel/index.tsx

@ -0,0 +1,69 @@
import React, { MouseEvent } from 'react';
import { render, cleanup } from '@testing-library/react';
import { ScrollingCarousel } from '../src';
import { carouselItemNodes } from './__fixtures__/nodes';
import * as helpers from '../src/helpers';
describe('<ScrollingCarousel />', () => {
let mockGetPageX: jest.SpyInstance<
number,
[React.TouchEvent<Element> | React.MouseEvent<Element, globalThis.MouseEvent>]
>;
afterEach(() => {
mockGetPageX.mockRestore();
jest.clearAllTimers();
jest.resetAllMocks();
cleanup();
});
beforeEach(() => {
Object.defineProperties(HTMLDivElement.prototype, {
scrollWidth: {
value: 1000,
writable: true
},
scrollLeft: {
value: 100,
writable: true
},
offsetWidth: {
value: 200,
writable: true
},
});
jest.useFakeTimers();
mockGetPageX = jest
.spyOn(helpers, 'getPageX')
.mockImplementation((_: MouseEvent) => 600);
});
it('should render right layout', async () => {
const { getByTestId } = render(
<ScrollingCarousel
children={carouselItemNodes(6)}
/>,
);
const carousel = getByTestId('carousel');
expect(carousel.firstChild);
expect(carousel.firstChild!.firstChild).toBeTruthy();
});
it('should render arrow icons', async () => {
const { getByTestId } = render(
<ScrollingCarousel
rightIcon={<i />}
children={carouselItemNodes(6)}
/>,
);
const carousel = getByTestId('carousel');
const rightArrow = carousel.querySelector('[data-arrow="right"]');
const leftArrow = carousel.querySelector('[data-arrow="left"]');
expect(carousel.firstChild);
expect(carousel.firstChild!.firstChild).toBeTruthy();
expect(rightArrow!.firstChild).toBeInstanceOf(HTMLElement);
expect(leftArrow!.firstChild).toBeInstanceOf(HTMLElement);
});
});

@ -4,13 +4,12 @@ module.exports = {
collectCoverageFrom: ['src/**/*.{ts,tsx}'],
coverageThreshold: {
global: {
branches: 93,
functions: 99,
lines: 99,
statements: 99,
branches: 83,
functions: 83,
lines: 83,
statements: 83,
},
},
coveragePathIgnorePatterns: ['<rootDir>/src/components/scrolling-carousel'],
testPathIgnorePatterns: ['<rootDir>/__tests__/__fixtures__/'],
transform: {
'^.+\\.tsx?$': 'ts-jest',

@ -130,7 +130,7 @@ export const ScrollingCarousel: FunctionComponent<SliderProps> = ({
};
return (
<div className={`${styles.sliderBase} ${className}`}>
<div className={`${styles.sliderBase} ${className}`} data-testid="carousel">
{showArrow.left && (
getArrow(SlideDirection.Right, "left", leftIcon)
)}

Loading…
Cancel
Save