Skip to content

testkit new useScrollabe driver, new Carousel driver for testings #2713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/components/carousel/Carousel.driver.new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {CarouselProps} from './types';
import {useComponentDriver, ComponentProps} from '../../testkit/new/Component.driver';
import {useScrollableDriver, ScrollProps} from '../../testkit/new/useScrollable.driver';

export const CarouselDriver = (props: ComponentProps) => {
const driver = useScrollableDriver<CarouselProps>(useComponentDriver(props));

const scroll = (props: ScrollProps) => {
return driver.scroll(props);
};

return {...driver, scroll};
};
34 changes: 16 additions & 18 deletions src/components/carousel/__tests__/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@ import {map} from 'lodash';
import React from 'react';
import {Text, View} from 'react-native';
import {fireOnMomentumScrollEnd} from '../../../uilib-test-renderer';
import {render} from '@testing-library/react-native';
import Carousel from '../index';
import {Constants} from '../../../commons/new';
import {CarouselDriver} from '../Carousel.driver';
import {CarouselDriver} from '../Carousel.driver.new';

const numberOfPagesShown = 5;
const onChangePageMock = jest.fn();
const onScrollMock = jest.fn();
const testID = 'carousel';
const TestCase = (props: any) => {
return (
<Carousel
testID={testID}
onChangePage={onChangePageMock}
onScroll={onScrollMock}
{...props}
>
<Carousel testID={testID} onChangePage={onChangePageMock} onScroll={onScrollMock} {...props}>
{map([...Array(numberOfPagesShown)], (_, index) => (
<Page key={index}>
<Text testID={`page-${index}`}>Page #{index}</Text>
Expand All @@ -27,7 +23,7 @@ const TestCase = (props: any) => {
);
};

const Page = ({children, ...others}:{children: React.ReactNode, others?: any}) => {
const Page = ({children, ...others}: {children: React.ReactNode; others?: any}) => {
return (
<View {...others} style={{flex: 1}}>
{children}
Expand All @@ -36,41 +32,43 @@ const Page = ({children, ...others}:{children: React.ReactNode, others?: any}) =
};

describe('Carousel render tests', () => {
afterEach(() => CarouselDriver.clear());

describe('initialPage', () => {
it('should be set to default initialPage', async () => {
const driver = new CarouselDriver({component: <TestCase/>, testID});
const renderTree = render(<TestCase/>);
const driver = CarouselDriver({renderTree, testID});

expect((await driver.getContentOffset()).x).toBe(0);
});

it('should be set to initialPage = 2', async () => {
const driver = new CarouselDriver({component: <TestCase initialPage={2}/>, testID});
const renderTree = render(<TestCase initialPage={2}/>);
const driver = CarouselDriver({renderTree, testID});

expect((await driver.getContentOffset()).x).toBe(Constants.screenWidth * 2);
});
});

describe('onScroll', () => {
it('should trigger onScroll from the second scroll', async () => {
const driver = new CarouselDriver({component: <TestCase/>, testID});
const renderTree = render(<TestCase/>);
const driver = CarouselDriver({renderTree, testID});

await driver.scroll(Constants.screenWidth); //NOTE: first scroll doesn't fire onScroll
await driver.scroll({x: Constants.screenWidth}); //NOTE: first scroll doesn't fire onScroll
expect(onScrollMock).not.toHaveBeenCalled();

await driver.scroll(Constants.screenWidth);
await driver.scroll({x: Constants.screenWidth});
expect(onScrollMock).toHaveBeenCalled();
});
});

describe('onChangePage', () => {
it('should trigger onChangePage with current page', async () => {
const driver = new CarouselDriver({component: <TestCase/>, testID});
const renderTree = render(<TestCase/>);
const driver = CarouselDriver({renderTree, testID});
const scrollView = await driver.getElement();

await driver.scroll(Constants.screenWidth); //NOTE: first scroll doesn't fire onScroll
await driver.scroll(Constants.screenWidth);
await driver.scroll({x: Constants.screenWidth}); //NOTE: first scroll doesn't fire onScroll
await driver.scroll({x: Constants.screenWidth});
expect(onChangePageMock).not.toHaveBeenCalled();

fireOnMomentumScrollEnd(scrollView, {x: Constants.screenWidth});
Expand Down
38 changes: 38 additions & 0 deletions src/testkit/new/useScrollable.driver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {fireEvent} from '@testing-library/react-native';
import {ComponentDriverResult} from './Component.driver';
import {ScrollViewProps, NativeScrollEvent, NativeScrollPoint} from 'react-native';

type ScrollableDriverOptions = Omit<NativeScrollEvent, 'contentOffset'>;

type ContentOffset = Partial<NativeScrollPoint>;

export type ScrollProps = ContentOffset & {options?: ScrollableDriverOptions};

export interface ScrollableDriverResult<Props> extends ComponentDriverResult<Props> {
scroll: (contentOffset: ContentOffset, options?: ScrollableDriverOptions) => void;
}

export type ScrollableDriverProps = Partial<Pick<ScrollViewProps, 'contentOffset'>>;

export const useScrollableDriver = <
Props extends ScrollableDriverProps,
DriverProps extends ComponentDriverResult<Props> = ComponentDriverResult<Props> // Allows for chaining multiple drivers
>(
driver: DriverProps
): ScrollableDriverResult<Props> & DriverProps => {
const getContentOffset = async () => await driver.getElement().props.contentOffset;
const scroll = ({x = 0, y = 0}, options?: ScrollableDriverOptions) => {
fireEvent.scroll(driver.getElement(), {
nativeEvent: {
...options,
contentOffset: {x, y}
}
});
};

return {
...driver,
getContentOffset,
scroll
};
};