Skip to content

Commit 090e236

Browse files
Switch - refactor driver to use new implementation (#2891)
* Switch - refactor driver to use new implementation * rename const * remove wrapper view * Update src/components/switch/switch.driver.ts remove log Co-authored-by: Adi Mordo <[email protected]> --------- Co-authored-by: Adi Mordo <[email protected]>
1 parent 878a458 commit 090e236

File tree

3 files changed

+54
-69
lines changed

3 files changed

+54
-69
lines changed
Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,97 @@
11
import React from 'react';
2+
import {render} from '@testing-library/react-native';
23
import Switch, {SwitchProps} from '../index';
3-
import View from '../../view';
44
import {SwitchDriver} from '../Switch.driver';
55

6-
describe('Switch', () => {
7-
afterEach(() => {
8-
SwitchDriver.clear();
9-
});
10-
11-
const switchDriver = (testID: string, props: Partial<SwitchProps>) => {
12-
const defaultProps: SwitchProps = {
13-
testID,
14-
value: false,
15-
onValueChange: jest.fn(),
16-
disabled: false
17-
};
18-
19-
const component = (<View><Switch {...defaultProps} {...props}/></View>);
20-
return new SwitchDriver({
21-
component,
22-
testID
23-
});
24-
};
6+
const testID = 'switch';
7+
const defaultProps: SwitchProps = {
8+
value: false,
9+
onValueChange: jest.fn(),
10+
disabled: false
11+
};
12+
const onColor = 'red';
13+
const offColor = 'grey';
2514

15+
const testCase = (testID: string, props: Partial<SwitchProps>) => {
16+
const renderTree = render(<Switch testID={testID} {...defaultProps} {...props}/>);
17+
return SwitchDriver({renderTree, testID});
18+
};
2619

20+
describe('Switch', () => {
2721
it('Should fire onChange event', async () => {
28-
const testId = 'switch-comp';
2922
const onChange = jest.fn();
30-
const driver = await switchDriver(testId, {onValueChange: onChange});
31-
await driver.press();
23+
const driver = testCase(testID, {onValueChange: onChange});
24+
driver.press();
3225
expect(onChange).toHaveBeenCalled();
3326
});
3427

3528
it('Should fire onChange event with false value when toggling off', async () => {
36-
const testId = 'switch-comp';
3729
const onChange = jest.fn();
38-
const driver = await switchDriver(testId, {onValueChange: onChange, value: true});
39-
await driver.press();
30+
const driver = testCase(testID, {onValueChange: onChange, value: true});
31+
driver.press();
4032
expect(onChange).toHaveBeenCalledWith(false);
4133
});
4234

4335
it('Should fire onChange event with true value when toggling on', async () => {
44-
const testId = 'switch-comp';
4536
const onChange = jest.fn();
46-
const driver = await switchDriver(testId, {onValueChange: onChange, value: false});
47-
await driver.press();
37+
const driver = testCase(testID, {onValueChange: onChange, value: false});
38+
driver.press();
4839
expect(onChange).toHaveBeenCalledWith(true);
4940
});
5041

5142
it('Should not fire onChange when disabled', async () => {
52-
const testId = 'switch-comp';
5343
const onValueChange = jest.fn();
54-
const driver = await switchDriver(testId, {disabled: true, onValueChange});
44+
const driver = testCase(testID, {disabled: true, onValueChange});
5545

56-
await driver.press();
46+
driver.press();
5747
expect(onValueChange).not.toHaveBeenCalled();
5848
});
5949

6050
it('Accessibility value should be true when checked', async () => {
61-
const testId = 'switch-comp';
62-
const driver = await switchDriver(testId, {value: true});
51+
const driver = testCase(testID, {value: true});
6352

64-
expect(await driver.getAccessibilityValue()).toBe(true);
53+
expect(driver.getAccessibilityValue()).toBe(true);
6554
});
6655

6756
it('Accessibility value should be false when not checked', async () => {
68-
const testId = 'switch-comp';
69-
const driver = await switchDriver(testId, {value: false});
57+
const driver = testCase(testID, {value: false});
7058

71-
expect(await driver.isChecked()).toBe(false);
59+
expect(driver.isChecked()).toBe(false);
7260
});
7361

7462
it('Accessibility value should be checked when checked', async () => {
75-
const testId = 'switch-comp';
76-
const driver = await switchDriver(testId, {value: true});
63+
const driver = testCase(testID, {value: true});
7764

78-
expect(await driver.isChecked()).toBe(true);
65+
expect(driver.isChecked()).toBe(true);
7966
});
8067

8168
it('Accessibility value should be false when not checked', async () => {
82-
const testId = 'switch-comp';
83-
const driver = await switchDriver(testId, {value: false});
69+
const driver = testCase(testID, {value: false});
8470

85-
expect(await driver.getAccessibilityValue()).toBe(false);
71+
expect(driver.getAccessibilityValue()).toBe(false);
8672
});
8773

8874
it('Should be disabled', async () => {
89-
const testId = 'switch-comp';
90-
const driver = await switchDriver(testId, {disabled: true});
75+
const driver = testCase(testID, {disabled: true});
9176

92-
expect(await driver.isDisabled()).toBe(true);
77+
expect(driver.isDisabled()).toBe(true);
9378
});
9479

9580
it('Should be disabled', async () => {
96-
const testId = 'switch-comp';
97-
const driver = await switchDriver(testId, {disabled: false});
81+
const driver = testCase(testID, {disabled: false});
9882

99-
expect(await driver.isDisabled()).toBe(false);
83+
expect(driver.isDisabled()).toBe(false);
10084
});
10185

10286
it('Should pass correct color when on', async () => {
103-
const testId = 'switch-comp';
104-
const driver = await switchDriver(testId, {value: true, onColor: 'red'});
105-
106-
expect(await driver.getColor()).toBe('red');
87+
const driver = testCase(testID, {value: true, onColor, offColor});
88+
89+
expect(driver.getStyle()?.backgroundColor).toBe(onColor);
10790
});
10891

10992
it('Should pass correct color when off', async () => {
110-
const testId = 'switch-comp';
111-
const driver = await switchDriver(testId, {value: false, offColor: 'red'});
93+
const driver = testCase(testID, {value: false, onColor, offColor});
11294

113-
expect(await driver.getColor()).toBe('red');
95+
expect(driver.getStyle()?.backgroundColor).toBe(offColor);
11496
});
11597
});
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
import {ViewStyle} from 'react-native';
12
import {SwitchProps} from './index';
2-
import {ComponentDriver} from '../../testkit/Component.driver';
3-
4-
export class SwitchDriver extends ComponentDriver<SwitchProps> {
5-
getAccessibilityValue = async () => (await this.getElementProps()).accessibilityValue?.text === '1';
6-
7-
isDisabled = async () => (await this.getElementProps()).accessibilityState?.disabled === true;
8-
9-
isChecked = async () => (await this.getElementProps()).accessibilityValue?.text === '1';
10-
11-
// @ts-ignore
12-
getColor = async () => (await this.getElementProps()).style.backgroundColor;
13-
}
3+
import {useComponentDriver, ComponentProps} from '../../testkit/new/Component.driver';
4+
import {usePressableDriver} from '../../testkit/new/usePressable.driver';
5+
6+
export const SwitchDriver = (props: ComponentProps) => {
7+
const driver = usePressableDriver<SwitchProps>(useComponentDriver(props));
8+
9+
const getStyle = () => driver.getProps().style as ViewStyle;
10+
const getAccessibilityValue = () => driver.getProps().accessibilityValue?.text === '1';
11+
const isDisabled = () => driver.getProps().accessibilityState?.disabled === true;
12+
const isChecked = () => driver.getProps().accessibilityValue?.text === '1';
13+
14+
return {...driver, getStyle, getAccessibilityValue, isDisabled, isChecked};
15+
};

src/testkit/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export {TextFieldDriver} from '../components/textField/TextField.driver.new';
1212
export {ViewDriver} from '../components/view/View.driver.new';
1313
export {ButtonDriver} from '../components/button/Button.driver.new';
1414
export {ImageDriver} from '../components/image/Image.driver.new';
15+
export {SwitchDriver} from '../components/switch/switch.driver';

0 commit comments

Comments
 (0)