Skip to content

SortableList - add driver and test #2568

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 2 commits into from
Apr 19, 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
59 changes: 25 additions & 34 deletions GestureDetectorMock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,27 @@ type Props = {
children: any;
};

const DEFAULT_DATA = {
absoluteX: 0,
absoluteY: 0,
translationX: 0,
translationY: 0,
velocityX: 0,
velocityY: 0,
x: 0,
y: 0
};

export class GestureDetectorMock extends React.Component<Props> {
render() {
switch (this.props.gesture.type) {
case 'tap':
return (
<TouchableOpacity
onPress={() => {
this.props.gesture._handlers.onTouchesDown();
this.props.gesture._handlers.onEnd();
this.props.gesture._handlers.onFinalize();
this.props.gesture._handlers.onTouchesDown?.();
this.props.gesture._handlers.onEnd?.();
this.props.gesture._handlers.onFinalize?.();
}}
>
{this.props.children}
Expand All @@ -24,37 +35,17 @@ export class GestureDetectorMock extends React.Component<Props> {
case 'pan':
return (
<TouchableOpacity
onPress={() => {
this.props.gesture._handlers.onStart({
absoluteX: 0,
absoluteY: 0,
translationX: 0,
translationY: 0,
velocityX: 0,
velocityY: 0,
x: 0,
y: 0
});
this.props.gesture._handlers.onUpdate({
absoluteX: 0,
absoluteY: 0,
translationX: 0,
translationY: 0,
velocityX: 0,
velocityY: 0,
x: 0,
y: 0
});
this.props.gesture._handlers.onEnd({
absoluteX: 0,
absoluteY: 0,
translationX: 0,
translationY: 0,
velocityX: 0,
velocityY: 0,
x: 0,
y: 0
});
onPress={(data) => {
this.props.gesture._handlers.onStart?.(DEFAULT_DATA);
if (Array.isArray(data)) {
data.forEach(info => {
this.props.gesture._handlers.onUpdate?.({...DEFAULT_DATA, ...info});
});
} else {
this.props.gesture._handlers.onUpdate?.({...DEFAULT_DATA, ...data});
}
this.props.gesture._handlers.onEnd?.(DEFAULT_DATA);
this.props.gesture._handlers.onFinalize?.(DEFAULT_DATA);
}}
>
{this.props.children}
Expand Down
5 changes: 5 additions & 0 deletions jest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ jest.mock('react-native-gesture-handler',
PanMock.onStart = getDefaultMockedHandler('onStart');
PanMock.onUpdate = getDefaultMockedHandler('onUpdate');
PanMock.onEnd = getDefaultMockedHandler('onEnd');
PanMock.onFinalize = getDefaultMockedHandler('onFinalize');
PanMock.activateAfterLongPress = getDefaultMockedHandler('activateAfterLongPress');
PanMock.enabled = getDefaultMockedHandler('enabled');
PanMock.onTouchesMove = getDefaultMockedHandler('onTouchesMove');
PanMock.prepare = jest.fn();
PanMock.initialize = jest.fn();
PanMock.toGestureArray = jest.fn(() => {
return [PanMock];
Expand Down
2 changes: 1 addition & 1 deletion src/components/button/Button.driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {TextDriver} from '../text/Text.driver';

/**
* Please run clear after each test
* */
*/
export class ButtonDriver extends ComponentDriver {
private readonly labelDriver: TextDriver;
private readonly iconDriver: ImageDriver;
Expand Down
35 changes: 35 additions & 0 deletions src/components/sortableList/SortableListItem.driver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import _ from 'lodash';
import {ComponentDriver} from '../../testkit/Component.driver';

/**
* Please run clear after each test
*/
export class SortableListItemDriver extends ComponentDriver {
dragUp = async (indices: number) => {
this.validateIndices(indices);
const data = _.times(indices, index => {
return {
translationY: -52 * (index + 1)
};
});

await this.uniDriver.selectorByTestId(this.testID).then(driver => driver.drag(data));
};

dragDown = async (indices: number) => {
this.validateIndices(indices);
const data = _.times(indices, index => {
return {
translationY: 52 * (index + 1)
};
});

await this.uniDriver.selectorByTestId(this.testID).then(driver => driver.drag(data));
};

private validateIndices = (indices: number) => {
if (indices <= 0 || !Number.isInteger(indices)) {
throw Error('indices must be a positive integer');
}
};
}
83 changes: 83 additions & 0 deletions src/components/sortableList/__tests__/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import _ from 'lodash';
import React, {useCallback} from 'react';
import Text from '../../text';
import View from '../../view';
import SortableList from '../index';
import {ComponentDriver, SortableListItemDriver} from '../../../testkit';

const defaultProps = {
testID: 'sortableList'
};

const ITEMS = _.times(5, index => {
return {
text: `${index}`,
id: `${index}`
};
});

const TestCase = props => {
const renderItem = useCallback(({item}) => {
return (
<Text center testID={`item${item.id}`}>
{item.text}
</Text>
);
}, []);

return (
<View flex>
<View flex useSafeArea>
<SortableList data={ITEMS} renderItem={renderItem} {...defaultProps} {...props}/>
</View>
</View>
);
};

describe('SortableList', () => {
beforeEach(() => {
jest.clearAllMocks();
});
afterEach(() => {
ComponentDriver.clear();
SortableListItemDriver.clear();
});

it('SortableList onOrderChange is called - down', async () => {
const onOrderChange = jest.fn();
const component = <TestCase onOrderChange={onOrderChange}/>;
const sortableListDriver = new ComponentDriver({component, testID: 'sortableList'});
expect(await sortableListDriver.exists()).toBeTruthy();
expect(onOrderChange).toHaveBeenCalledTimes(0);
const item1Driver = new SortableListItemDriver({component, testID: 'item1'});
expect(await item1Driver.exists()).toBeTruthy();
await item1Driver.dragDown(1);
expect(onOrderChange).toHaveBeenCalledTimes(1);
expect(onOrderChange).toHaveBeenCalledWith([
{id: '0', text: '0'},
{id: '2', text: '2'},
{id: '1', text: '1'},
{id: '3', text: '3'},
{id: '4', text: '4'}
]);
});

it('SortableList onOrderChange is called - up', async () => {
const onOrderChange = jest.fn();
const component = <TestCase onOrderChange={onOrderChange}/>;
const sortableListDriver = new ComponentDriver({component, testID: 'sortableList'});
expect(await sortableListDriver.exists()).toBeTruthy();
expect(onOrderChange).toHaveBeenCalledTimes(0);
const item4Driver = new SortableListItemDriver({component, testID: 'item4'});
expect(await item4Driver.exists()).toBeTruthy();
await item4Driver.dragUp(3);
expect(onOrderChange).toHaveBeenCalledTimes(1);
expect(onOrderChange).toHaveBeenCalledWith([
{id: '0', text: '0'},
{id: '4', text: '4'},
{id: '1', text: '1'},
{id: '2', text: '2'},
{id: '3', text: '3'}
]);
});
});
4 changes: 1 addition & 3 deletions src/incubator/Dialog/__tests__/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ const TestCase = props => {
);
};

const _TestCase = props => <TestCase {...props}/>;

describe('Incubator.Dialog', () => {
it('Incubator.Dialog should exist only if visible', async () => {
const onDismiss = jest.fn();
const component = _TestCase({onDismiss});
const component = <TestCase onDismiss={onDismiss}/>;
const dialogDriver = new ComponentDriver({component, testID: 'dialog'});
expect(await dialogDriver.exists()).toBeFalsy();
const openButtonDriver = new ButtonDriver({component, testID: 'openButton'});
Expand Down
10 changes: 8 additions & 2 deletions src/testkit/Component.driver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {UniDriver, UniDriverClass} from './UniDriver';
import {DragData, UniDriver, UniDriverClass} from './UniDriver';
import {TestingLibraryDriver} from './drivers/TestingLibraryDriver';

export type ComponentDriverArgs = {
Expand All @@ -9,7 +9,7 @@ export type ComponentDriverArgs = {

/**
* Please run clear after each test
* */
*/
export class ComponentDriver {
protected readonly testID: string;
protected readonly uniDriver: UniDriver;
Expand Down Expand Up @@ -45,6 +45,12 @@ export class ComponentDriver {
.then((driver) => driver.press());
};

drag = async (data: DragData | DragData[]) => {
return this.uniDriver
.selectorByTestId(this.testID)
.then((driver) => driver.drag(data));
};

focus = async () => {
return this.uniDriver
.selectorByTestId(this.testID)
Expand Down
12 changes: 12 additions & 0 deletions src/testkit/UniDriver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export type DragData = {
absoluteX?: number;
absoluteY?: number;
translationX?: number;
translationY?: number;
velocityX?: number;
velocityY?: number;
x?: number;
y?: number;
};

export interface UniDriver {
selectorByTestId(testId: string): Promise<UniDriver>;
selectorByText(text: string): Promise<UniDriver>;
Expand All @@ -7,6 +18,7 @@ export interface UniDriver {
instance(): Promise<any>;
getInstanceProps(): Promise<any>;
press(): void;
drag(data: DragData | DragData[]): void;
focus(): void;
blur(): void;
typeText(text: string): Promise<void>;
Expand Down
11 changes: 10 additions & 1 deletion src/testkit/drivers/TestingLibraryDriver.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {UniDriver} from '../UniDriver';
import {DragData, UniDriver} from '../UniDriver';
import {fireEvent, render, RenderAPI} from '@testing-library/react-native';
import {ReactTestInstance} from 'react-test-renderer';
import {act} from '@testing-library/react-hooks';
Expand Down Expand Up @@ -85,6 +85,15 @@ export class TestingLibraryDriver implements UniDriver {
fireEvent.press(this.reactTestInstances[0]);
};

drag = (data: DragData | DragData[]): void => {
if (!this.reactTestInstances) {
throw new NoSelectorException();
}
this.validateExplicitInstance();
this.validateSingleInstance();
fireEvent.press(this.reactTestInstances[0], data);
};

focus = (): void => {
if (!this.reactTestInstances) {
throw new NoSelectorException();
Expand Down
3 changes: 2 additions & 1 deletion src/testkit/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export {ComponentDriver} from './Component.driver';
export {ImageDriver} from '../components/image/Image.driver';
export {TextDriver} from '../components/Text/Text.driver';
export {TextDriver} from '../components/text/Text.driver';
export {SwitchDriver} from '../components/switch/switch.driver';
export {ButtonDriver} from '../components/button/Button.driver';
export {TextFieldDriver} from '../incubator/TextField/TextField.driver';
Expand All @@ -10,3 +10,4 @@ export {CheckboxDriver} from '../components/checkbox/Checkbox.driver';
export {HintDriver} from '../components/hint/Hint.driver';
export {RadioButtonDriver} from '../components/radioButton/RadioButton.driver';
export {RadioGroupDriver} from '../components/radioGroup/RadioGroup.driver';
export {SortableListItemDriver} from '../components/sortableList/SortableListItem.driver';