Skip to content

Commit 59d2092

Browse files
authored
testkit new useScrollabe driver, new Carousel driver for testings (#2713)
* testkit new useScrollabe driver, new Carousel driver for testings * Fixed review notes * fixed review notes
1 parent d8ebfc9 commit 59d2092

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {CarouselProps} from './types';
2+
import {useComponentDriver, ComponentProps} from '../../testkit/new/Component.driver';
3+
import {useScrollableDriver, ScrollProps} from '../../testkit/new/useScrollable.driver';
4+
5+
export const CarouselDriver = (props: ComponentProps) => {
6+
const driver = useScrollableDriver<CarouselProps>(useComponentDriver(props));
7+
8+
const scroll = (props: ScrollProps) => {
9+
return driver.scroll(props);
10+
};
11+
12+
return {...driver, scroll};
13+
};

src/components/carousel/__tests__/index.spec.tsx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@ import {map} from 'lodash';
22
import React from 'react';
33
import {Text, View} from 'react-native';
44
import {fireOnMomentumScrollEnd} from '../../../uilib-test-renderer';
5+
import {render} from '@testing-library/react-native';
56
import Carousel from '../index';
67
import {Constants} from '../../../commons/new';
7-
import {CarouselDriver} from '../Carousel.driver';
8+
import {CarouselDriver} from '../Carousel.driver.new';
89

910
const numberOfPagesShown = 5;
1011
const onChangePageMock = jest.fn();
1112
const onScrollMock = jest.fn();
1213
const testID = 'carousel';
1314
const TestCase = (props: any) => {
1415
return (
15-
<Carousel
16-
testID={testID}
17-
onChangePage={onChangePageMock}
18-
onScroll={onScrollMock}
19-
{...props}
20-
>
16+
<Carousel testID={testID} onChangePage={onChangePageMock} onScroll={onScrollMock} {...props}>
2117
{map([...Array(numberOfPagesShown)], (_, index) => (
2218
<Page key={index}>
2319
<Text testID={`page-${index}`}>Page #{index}</Text>
@@ -27,7 +23,7 @@ const TestCase = (props: any) => {
2723
);
2824
};
2925

30-
const Page = ({children, ...others}:{children: React.ReactNode, others?: any}) => {
26+
const Page = ({children, ...others}: {children: React.ReactNode; others?: any}) => {
3127
return (
3228
<View {...others} style={{flex: 1}}>
3329
{children}
@@ -36,41 +32,43 @@ const Page = ({children, ...others}:{children: React.ReactNode, others?: any}) =
3632
};
3733

3834
describe('Carousel render tests', () => {
39-
afterEach(() => CarouselDriver.clear());
40-
4135
describe('initialPage', () => {
4236
it('should be set to default initialPage', async () => {
43-
const driver = new CarouselDriver({component: <TestCase/>, testID});
37+
const renderTree = render(<TestCase/>);
38+
const driver = CarouselDriver({renderTree, testID});
4439

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

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

5147
expect((await driver.getContentOffset()).x).toBe(Constants.screenWidth * 2);
5248
});
5349
});
5450

5551
describe('onScroll', () => {
5652
it('should trigger onScroll from the second scroll', async () => {
57-
const driver = new CarouselDriver({component: <TestCase/>, testID});
53+
const renderTree = render(<TestCase/>);
54+
const driver = CarouselDriver({renderTree, testID});
5855

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

62-
await driver.scroll(Constants.screenWidth);
59+
await driver.scroll({x: Constants.screenWidth});
6360
expect(onScrollMock).toHaveBeenCalled();
6461
});
6562
});
6663

6764
describe('onChangePage', () => {
6865
it('should trigger onChangePage with current page', async () => {
69-
const driver = new CarouselDriver({component: <TestCase/>, testID});
66+
const renderTree = render(<TestCase/>);
67+
const driver = CarouselDriver({renderTree, testID});
7068
const scrollView = await driver.getElement();
7169

72-
await driver.scroll(Constants.screenWidth); //NOTE: first scroll doesn't fire onScroll
73-
await driver.scroll(Constants.screenWidth);
70+
await driver.scroll({x: Constants.screenWidth}); //NOTE: first scroll doesn't fire onScroll
71+
await driver.scroll({x: Constants.screenWidth});
7472
expect(onChangePageMock).not.toHaveBeenCalled();
7573

7674
fireOnMomentumScrollEnd(scrollView, {x: Constants.screenWidth});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {fireEvent} from '@testing-library/react-native';
2+
import {ComponentDriverResult} from './Component.driver';
3+
import {ScrollViewProps, NativeScrollEvent, NativeScrollPoint} from 'react-native';
4+
5+
type ScrollableDriverOptions = Omit<NativeScrollEvent, 'contentOffset'>;
6+
7+
type ContentOffset = Partial<NativeScrollPoint>;
8+
9+
export type ScrollProps = ContentOffset & {options?: ScrollableDriverOptions};
10+
11+
export interface ScrollableDriverResult<Props> extends ComponentDriverResult<Props> {
12+
scroll: (contentOffset: ContentOffset, options?: ScrollableDriverOptions) => void;
13+
}
14+
15+
export type ScrollableDriverProps = Partial<Pick<ScrollViewProps, 'contentOffset'>>;
16+
17+
export const useScrollableDriver = <
18+
Props extends ScrollableDriverProps,
19+
DriverProps extends ComponentDriverResult<Props> = ComponentDriverResult<Props> // Allows for chaining multiple drivers
20+
>(
21+
driver: DriverProps
22+
): ScrollableDriverResult<Props> & DriverProps => {
23+
const getContentOffset = async () => await driver.getElement().props.contentOffset;
24+
const scroll = ({x = 0, y = 0}, options?: ScrollableDriverOptions) => {
25+
fireEvent.scroll(driver.getElement(), {
26+
nativeEvent: {
27+
...options,
28+
contentOffset: {x, y}
29+
}
30+
});
31+
};
32+
33+
return {
34+
...driver,
35+
getContentOffset,
36+
scroll
37+
};
38+
};

0 commit comments

Comments
 (0)