123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
- import Carousel from 'sentry/components/carousel';
- describe('Carousel', function () {
- let intersectionOnbserverCb: (entries: Partial<IntersectionObserverEntry>[]) => void =
- jest.fn();
- window.IntersectionObserver = class IntersectionObserver {
- root = null;
- rootMargin = '';
- thresholds = [];
- takeRecords = jest.fn();
- constructor(cb: IntersectionObserverCallback) {
-
- intersectionOnbserverCb = cb;
- }
- observe() {}
- unobserve() {}
- disconnect() {}
- };
- it('hides arrows if content does not overflow in x', function () {
- render(
- <Carousel>
- <div data-test-id="child-1" />
- </Carousel>
- );
-
- act(() =>
- intersectionOnbserverCb([
- {target: screen.getByTestId('child-1'), intersectionRatio: 1},
- ])
- );
- expect(screen.queryByRole('button', {name: 'Scroll left'})).not.toBeInTheDocument();
- expect(screen.queryByRole('button', {name: 'Scroll right'})).not.toBeInTheDocument();
- });
- it('shows right arrow when elements exist to the right', async function () {
- render(
- <Carousel>
- <div data-test-id="child-1" />
- <div data-test-id="child-2" />
- <div data-test-id="child-3" />
- </Carousel>
- );
- const elements = [
- screen.getByTestId('child-1'),
- screen.getByTestId('child-2'),
- screen.getByTestId('child-3'),
- ];
-
- act(() =>
- intersectionOnbserverCb([
- {target: elements[0], intersectionRatio: 1},
- {target: elements[1], intersectionRatio: 0.5},
- {target: elements[2], intersectionRatio: 0},
- ])
- );
- const rightButton = screen.getByRole('button', {name: 'Scroll right'});
- expect(screen.queryByRole('button', {name: 'Scroll left'})).not.toBeInTheDocument();
-
- elements[1].scrollIntoView = jest.fn();
- await userEvent.click(rightButton);
- expect(elements[1].scrollIntoView).toHaveBeenCalled();
- });
- it('shows left arrow when elements exist to the left', async function () {
- render(
- <Carousel>
- <div data-test-id="child-1" />
- <div data-test-id="child-2" />
- <div data-test-id="child-3" />
- </Carousel>
- );
- const elements = [
- screen.getByTestId('child-1'),
- screen.getByTestId('child-2'),
- screen.getByTestId('child-3'),
- ];
-
- act(() =>
- intersectionOnbserverCb([
- {target: elements[0], intersectionRatio: 0},
- {target: elements[1], intersectionRatio: 1},
- {target: elements[2], intersectionRatio: 1},
- ])
- );
- const leftButton = screen.getByRole('button', {name: 'Scroll left'});
- expect(screen.queryByRole('button', {name: 'Scroll right'})).not.toBeInTheDocument();
-
- elements[0].scrollIntoView = jest.fn();
- await userEvent.click(leftButton);
- expect(elements[0].scrollIntoView).toHaveBeenCalled();
- });
- it('skips an element when it is past the visibleRatio', async function () {
- render(
- <Carousel visibleRatio={0.9}>
- <div data-test-id="child-1" />
- <div data-test-id="child-2" />
- <div data-test-id="child-3" />
- </Carousel>
- );
- const elements = [
- screen.getByTestId('child-1'),
- screen.getByTestId('child-2'),
- screen.getByTestId('child-3'),
- ];
-
- act(() =>
- intersectionOnbserverCb([
- {target: elements[0], intersectionRatio: 1},
- {target: elements[1], intersectionRatio: 0.95},
- {target: elements[2], intersectionRatio: 0},
- ])
- );
- const rightButton = screen.getByRole('button', {name: 'Scroll right'});
- expect(screen.queryByRole('button', {name: 'Scroll left'})).not.toBeInTheDocument();
-
- elements[2].scrollIntoView = jest.fn();
- await userEvent.click(rightButton);
- expect(elements[2].scrollIntoView).toHaveBeenCalled();
- });
- });
|