Skip to content

Commit 878a458

Browse files
authored
new button driver, image driver, testing (#2894)
* new button driver, image driver, testing * changed to driver new, imports from testkit index * button driver refactor * fixed review notes
1 parent 35d588f commit 878a458

File tree

4 files changed

+99
-73
lines changed

4 files changed

+99
-73
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {ButtonProps} from './ButtonTypes';
2+
import {useComponentDriver, ComponentProps, usePressableDriver, TextDriver, ImageDriver} from '../../testkit';
3+
4+
export const ButtonDriver = (props: ComponentProps) => {
5+
const driver = usePressableDriver<ButtonProps>(useComponentDriver(props));
6+
7+
const labelDriver = TextDriver({
8+
renderTree: props.renderTree,
9+
testID: `${props.testID}.label`
10+
});
11+
12+
const iconDriver = ImageDriver({
13+
renderTree: props.renderTree,
14+
testID: `${props.testID}.icon`
15+
});
16+
17+
const getLabel = () => {
18+
return labelDriver;
19+
};
20+
21+
const getIcon = () => {
22+
return iconDriver;
23+
};
24+
25+
return {getLabel, getIcon, ...driver};
26+
};

src/components/button/__tests__/index.driver.spec.tsx

Lines changed: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,147 @@
11
import React, {useState} from 'react';
2-
import {waitFor} from '@testing-library/react-native';
2+
import {waitFor, render} from '@testing-library/react-native';
33
import View from '../../view';
44
import Text from '../../text';
55
import Button from '../index';
66
import {ImageSourcePropType} from 'react-native';
7-
import {ButtonDriver} from '../Button.driver';
8-
import {TextDriver} from '../../text/Text.driver';
7+
import {ButtonDriver} from '../Button.driver.new';
8+
import {TextDriver} from '../../text/Text.driver.new';
99

1010
const BUTTON_ID = 'button_test_id';
1111
const CHILDREN_TEXT_ID = 'children_test_id';
1212
const CHILDREN_TEXT = 'custom button text';
1313

1414
// TODO: This tests are flaky and only fail on CI - we should investigate why
1515
describe('Button', () => {
16-
afterEach(() => {
17-
ButtonDriver.clear();
18-
});
19-
2016
it('should render a button', async () => {
21-
const component = WrapperScreenWithButton();
22-
const buttonDriver = new ButtonDriver({component, testID: BUTTON_ID});
23-
17+
const renderTree = render(<WrapperScreenWithButton/>);
18+
const buttonDriver = ButtonDriver({renderTree, testID: 'button_test_id'});
2419
expect(await buttonDriver.exists()).toBeTruthy();
2520
});
2621

2722
describe('custom button', () => {
2823
it('should render a custom button', async () => {
29-
const component = WrapperScreenWithCustomButton();
30-
const buttonDriver = new ButtonDriver({component, testID: BUTTON_ID});
31-
24+
const renderTree = render(<WrapperScreenWithCustomButton/>);
25+
const buttonDriver = ButtonDriver({renderTree, testID: 'button_test_id'});
3226
expect(buttonDriver.exists()).toBeTruthy();
3327
});
3428

3529
it('should render the children with correct text', async () => {
36-
const component = WrapperScreenWithCustomButton();
37-
const buttonDriver = new ButtonDriver({component, testID: BUTTON_ID});
38-
30+
const renderTree = render(<WrapperScreenWithCustomButton/>);
31+
const buttonDriver = ButtonDriver({renderTree, testID: 'button_test_id'});
3932
expect(buttonDriver.exists()).toBeTruthy();
40-
41-
const childrenTextDriver = new TextDriver({component, testID: CHILDREN_TEXT_ID});
42-
43-
expect(await childrenTextDriver.getTextContent()).toEqual(CHILDREN_TEXT);
33+
const childrenTextDriver = TextDriver({renderTree, testID: CHILDREN_TEXT_ID});
34+
expect(await childrenTextDriver.getText()).toEqual(CHILDREN_TEXT);
4435
});
4536
});
4637

4738
describe('OnPress', () => {
4839
let onPressCallback: jest.Mock;
49-
beforeEach(() => onPressCallback = jest.fn());
40+
beforeEach(() => (onPressCallback = jest.fn()));
5041
afterEach(() => onPressCallback.mockClear());
5142

52-
it.skip('should trigger onPress callback', async () => {
53-
const component = WrapperScreenWithButton({onPress: onPressCallback});
54-
const buttonDriver = new ButtonDriver({component, testID: BUTTON_ID});
55-
43+
it('should trigger onPress callback', async () => {
44+
const props = {onPress: onPressCallback};
45+
const renderTree = render(<WrapperScreenWithButton {...props}/>);
46+
const buttonDriver = ButtonDriver({renderTree, testID: 'button_test_id'});
5647
buttonDriver.press();
57-
5848
await waitFor(() => expect(onPressCallback).toHaveBeenCalledTimes(1));
5949
});
6050

6151
it('should not trigger onPress callback if button disabled', async () => {
62-
const component = WrapperScreenWithButton({onPress: onPressCallback, disabled: true});
63-
const buttonDriver = new ButtonDriver({component, testID: BUTTON_ID});
64-
52+
const props = {disabled: true, onPress: onPressCallback};
53+
const renderTree = render(<WrapperScreenWithButton {...props}/>);
54+
const buttonDriver = ButtonDriver({renderTree, testID: 'button_test_id'});
6555
buttonDriver.press();
66-
6756
await waitFor(() => expect(onPressCallback).toHaveBeenCalledTimes(0));
6857
});
6958
});
7059

7160
describe('label', () => {
7261
const LABEL = 'mock label';
7362
it('should render a button with correct content', async () => {
74-
const component = WrapperScreenWithButton({label: LABEL});
75-
const buttonDriver = new ButtonDriver({component, testID: BUTTON_ID});
76-
77-
expect(await buttonDriver.getLabelContent()).toEqual(LABEL);
63+
const props = {label: LABEL};
64+
const renderTree = render(<WrapperScreenWithButton {...props}/>);
65+
const buttonDriver = ButtonDriver({renderTree, testID: 'button_test_id'});
66+
expect(await buttonDriver.getLabel().getText()).toEqual(LABEL);
7867
});
7968

8069
it('should render a button with correct label content. ', async () => {
81-
const component = WrapperScreenWithButton({label: LABEL});
82-
const buttonDriver = new ButtonDriver({component, testID: BUTTON_ID});
83-
84-
expect(await buttonDriver.getLabelContent()).toEqual(LABEL);
70+
const props = {label: LABEL};
71+
const renderTree = render(<WrapperScreenWithButton {...props}/>);
72+
const buttonDriver = ButtonDriver({renderTree, testID: 'button_test_id'});
73+
expect(await buttonDriver.getLabel().getText()).toEqual(LABEL);
8574
});
8675

8776
it('should render a button without label. ', async () => {
88-
const component = WrapperScreenWithButton();
89-
const buttonDriver = new ButtonDriver({component, testID: BUTTON_ID});
90-
91-
expect(await buttonDriver.isLabelExists()).toBeFalsy();
77+
const renderTree = render(<WrapperScreenWithButton/>);
78+
const buttonDriver = ButtonDriver({renderTree, testID: 'button_test_id'});
79+
expect(await buttonDriver.getLabel().exists()).toBeFalsy();
9280
});
9381
});
9482

9583
describe('icon', () => {
9684
it('should render a button without an icon. ', async () => {
97-
const component = WrapperScreenWithButton();
98-
const buttonDriver = new ButtonDriver({component, testID: BUTTON_ID});
99-
100-
expect(await buttonDriver.isIconExists()).toBeFalsy();
85+
const renderTree = render(<WrapperScreenWithButton/>);
86+
const buttonDriver = ButtonDriver({renderTree, testID: 'button_test_id'});
87+
expect(await buttonDriver.getIcon().exists()).toBeFalsy();
10188
});
10289

10390
it('should render a button with icon. ', async () => {
10491
const ICON = 12;
105-
const component = WrapperScreenWithButton({iconSource: ICON});
106-
const buttonDriver = new ButtonDriver({component, testID: BUTTON_ID});
92+
const props = {iconSource: ICON};
93+
const renderTree = render(<WrapperScreenWithButton {...props}/>);
94+
const buttonDriver = ButtonDriver({renderTree, testID: 'button_test_id'});
10795

108-
expect(await buttonDriver.isIconExists()).toBeTruthy();
96+
expect(await buttonDriver.getIcon().exists()).toBeTruthy();
10997
});
11098
});
11199

112100
describe('more complicated screen', () => {
113101
//todo take it out of this file. to the demo screens maybe
114102
it('should change text values according to state changes from buttons pressing', async () => {
115-
const component = StatefulScreenWithTextsAndButtonss();
116-
const text1Driver = new TextDriver({testID: `text_1`, component});
117-
const text2Driver = new TextDriver({testID: `text_2`, component});
118-
const button1Driver = new ButtonDriver({testID: `${BUTTON_ID}1`, component});
119-
const button2Driver = new ButtonDriver({testID: `${BUTTON_ID}2`, component});
103+
const renderTree = render(StatefulScreenWithTextsAndButtonss());
104+
const text1Driver = TextDriver({testID: `text_1`, renderTree});
105+
const text2Driver = TextDriver({testID: `text_2`, renderTree});
106+
const button2Driver = ButtonDriver({testID: `${BUTTON_ID}2`, renderTree});
107+
const button1Driver = ButtonDriver({testID: `${BUTTON_ID}1`, renderTree});
120108

121-
expect(await text1Driver.getTextContent()).toBe('button 1 pressed 0 times');
122-
expect(await text2Driver.getTextContent()).toBe('button 2 pressed 0 times');
109+
expect(await text1Driver.getText()).toBe('button 1 pressed 0 times');
110+
expect(await text2Driver.getText()).toBe('button 2 pressed 0 times');
123111

124112
await button1Driver.press();
125113
await button1Driver.press();
126114
await button2Driver.press();
127115

128-
await waitFor(async () => expect(await text1Driver.getTextContent()).toBe('button 1 pressed 2 times'));
129-
await waitFor(async () => expect(await text2Driver.getTextContent()).toBe('button 2 pressed 1 times'));
116+
await waitFor(async () => expect(await text1Driver.getText()).toBe('button 1 pressed 2 times'));
117+
await waitFor(async () => expect(await text2Driver.getText()).toBe('button 2 pressed 1 times'));
130118
});
131119
});
132120
});
133121

134122
function WrapperScreenWithButton(buttonProps: {
135-
onPress?: () => void;
136-
label?: string;
137-
iconSource?: ImageSourcePropType;
138-
disabled?: boolean;
139-
} = {}) {
123+
onPress?: () => void;
124+
label?: string;
125+
iconSource?: ImageSourcePropType;
126+
disabled?: boolean;
127+
} = {}) {
140128
const {onPress, label, iconSource, disabled} = buttonProps;
141-
return (<View testID={'wrapper_screen_test_id'}>
142-
<Button testID={BUTTON_ID} onPress={onPress} label={label} iconSource={iconSource} disabled={disabled}/>
143-
</View>);
129+
return (
130+
<View testID={'wrapper_screen_test_id'}>
131+
<Button testID={BUTTON_ID} onPress={onPress} label={label} iconSource={iconSource} disabled={disabled}/>
132+
</View>
133+
);
144134
}
145135

146136
function WrapperScreenWithCustomButton(buttonProps: {onPress?: () => void} = {}) {
147137
const {onPress} = buttonProps;
148-
return (<View testID={'wrapper_screen_test_id'}>
149-
<Button testID={BUTTON_ID} onPress={onPress}>
150-
<Text testID={CHILDREN_TEXT_ID}>{CHILDREN_TEXT}</Text>
151-
</Button>
152-
</View>);
138+
return (
139+
<View testID={'wrapper_screen_test_id'}>
140+
<Button testID={BUTTON_ID} onPress={onPress}>
141+
<Text testID={CHILDREN_TEXT_ID}>{CHILDREN_TEXT}</Text>
142+
</Button>
143+
</View>
144+
);
153145
}
154146

155147
const StatefulScreenWithTextsAndButtonss = () => <StatefulScreenWithTextsAndButtons/>;
@@ -159,8 +151,8 @@ const StatefulScreenWithTextsAndButtons = () => {
159151
const [count2, setCount2] = useState(0);
160152
return (
161153
<View testID={'stateful_wrapper_screen_test_id'}>
162-
<Text testID={'text_1'} >{`button 1 pressed ${count1} times`}</Text>
163-
<Text testID={'text_2'} >{`button 2 pressed ${count2} times`}</Text>
154+
<Text testID={'text_1'}>{`button 1 pressed ${count1} times`}</Text>
155+
<Text testID={'text_2'}>{`button 2 pressed ${count2} times`}</Text>
164156
<Button testID={`${BUTTON_ID}1`} onPress={() => setCount1(count1 + 1)}/>
165157
<Button testID={`${BUTTON_ID}2`} onPress={() => setCount2(count2 + 1)}/>
166158
</View>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {useComponentDriver, ComponentProps} from '../../testkit/new/Component.driver';
2+
3+
export const ImageDriver = (props: ComponentProps) => {
4+
const driver = useComponentDriver(props);
5+
return driver;
6+
};

src/testkit/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ export {SortableListItemDriver} from '../components/sortableList/SortableListIte
1010
export {TextDriver} from '../components/text/Text.driver.new';
1111
export {TextFieldDriver} from '../components/textField/TextField.driver.new';
1212
export {ViewDriver} from '../components/view/View.driver.new';
13+
export {ButtonDriver} from '../components/button/Button.driver.new';
14+
export {ImageDriver} from '../components/image/Image.driver.new';

0 commit comments

Comments
 (0)