Skip to content

Commit 2e46199

Browse files
author
Marik Shnitman
committed
Added Basic testkit for Swtich component
1 parent 4a34d73 commit 2e46199

File tree

5 files changed

+140
-6
lines changed

5 files changed

+140
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"@testing-library/react-hooks": "^7.0.2",
7373
"@testing-library/react-native": "^7.2.0",
7474
"@types/hoist-non-react-statics": "^3.3.1",
75+
"@types/jest": "^27.4.1",
7576
"@types/lodash": "^4.0.0",
7677
"@types/prop-types": "^15.5.3",
7778
"@types/react-native": "0.66.4",

src/components/switch/Switch.spec.tsx

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import React from 'react';
2+
import {render} from '@testing-library/react-native';
3+
import Switch, {SwitchProps} from './index';
4+
import {SwitchDriverFactory} from './switch.driver';
5+
import View from '../view';
6+
7+
describe('Switch', () => {
8+
9+
const renderSwitch = (testID: string, props: Partial<SwitchProps>) => {
10+
const defaultProps: SwitchProps = {
11+
testID,
12+
value: false,
13+
onValueChange: jest.fn(),
14+
disabled: false
15+
};
16+
const component = render(<View><Switch {...defaultProps} {...props}/></View>);
17+
const driver = SwitchDriverFactory({
18+
wrappedComponent: component,
19+
testID
20+
});
21+
22+
return {driver};
23+
};
24+
25+
26+
it('Should fire onChange event', () => {
27+
const testId = 'switch-comp';
28+
const onChange = jest.fn();
29+
const {driver} = renderSwitch(testId, {onValueChange: onChange});
30+
driver.press();
31+
expect(onChange).toHaveBeenCalled();
32+
});
33+
34+
it('Should fire onChange event with false value when toggling off', () => {
35+
const testId = 'switch-comp';
36+
const onChange = jest.fn();
37+
const {driver} = renderSwitch(testId, {onValueChange: onChange, value: true});
38+
driver.press();
39+
expect(onChange).toHaveBeenCalledWith(false);
40+
});
41+
it('Should fire onChange event with true value when toggling on', () => {
42+
const testId = 'switch-comp';
43+
const onChange = jest.fn();
44+
const {driver} = renderSwitch(testId, {onValueChange: onChange, value: false});
45+
driver.press();
46+
expect(onChange).toHaveBeenCalledWith(true);
47+
});
48+
49+
it('Should not fire onChange when disabled', () => {
50+
const testId = 'switch-comp';
51+
const onValueChange = jest.fn();
52+
const {driver} = renderSwitch(testId, {disabled: true, onValueChange});
53+
54+
driver.press();
55+
expect(onValueChange).not.toHaveBeenCalled();
56+
});
57+
58+
it('Accessibility value should be true when checked', () => {
59+
const testId = 'switch-comp';
60+
const {driver} = renderSwitch(testId, {value: true});
61+
62+
expect(driver.getAccessibilityValue()).toBe(true);
63+
});
64+
65+
it('Accessibility value should be false when not checked', () => {
66+
const testId = 'switch-comp';
67+
const {driver} = renderSwitch(testId, {value: false});
68+
69+
expect(driver.isChecked()).toBe(false);
70+
});
71+
72+
it('Accessibility value should be checked when checked', () => {
73+
const testId = 'switch-comp';
74+
const {driver} = renderSwitch(testId, {value: true});
75+
76+
expect(driver.isChecked()).toBe(true);
77+
});
78+
79+
it('Accessibility value should be false when not checked', () => {
80+
const testId = 'switch-comp';
81+
const {driver} = renderSwitch(testId, {value: false});
82+
83+
expect(driver.getAccessibilityValue()).toBe(false);
84+
});
85+
86+
it('Should be disabled', () => {
87+
const testId = 'switch-comp';
88+
const {driver} = renderSwitch(testId, {disabled: true});
89+
90+
expect(driver.isDisabled()).toBe(true);
91+
});
92+
93+
it('Should be disabled', () => {
94+
const testId = 'switch-comp';
95+
const {driver} = renderSwitch(testId, {disabled: false});
96+
97+
expect(driver.isDisabled()).toBe(false);
98+
});
99+
100+
it('Should pass correct color when on', () => {
101+
const testId = 'switch-comp';
102+
const {driver} = renderSwitch(testId, {value: true, onColor: 'red'});
103+
104+
expect(driver.getColor()).toBe('red');
105+
});
106+
107+
it('Should pass correct color when off', () => {
108+
const testId = 'switch-comp';
109+
const {driver} = renderSwitch(testId, {value: false, offColor: 'red'});
110+
111+
expect(driver.getColor()).toBe('red');
112+
});
113+
});

src/components/switch/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export type SwitchProps = {
5555
*/
5656
thumbStyle?: StyleProp<ViewStyle>;
5757
style?: StyleProp<ViewStyle>;
58-
testID?: string;
58+
testID?: string;
5959
}
6060

6161
/**
@@ -65,13 +65,13 @@ export type SwitchProps = {
6565
*/
6666
class Switch extends Component<SwitchProps> {
6767
static displayName = 'Switch';
68-
68+
6969
state = {
7070
thumbPosition: new Animated.Value(this.props.value ? 1 : 0)
7171
};
7272

7373
styles = createStyles(this.props);
74-
74+
7575
componentDidUpdate(prevProps: SwitchProps) {
7676
const {value} = this.props;
7777
if (prevProps.value !== value) {
@@ -82,11 +82,11 @@ class Switch extends Component<SwitchProps> {
8282
getAccessibilityProps() {
8383
const {disabled, value} = this.props;
8484

85-
85+
8686
return {
8787
accessible: true,
8888
accessibilityRole: 'switch',
89-
accessibilityStates: disabled ? ['disabled'] : value ? ['checked'] : ['unchecked'],
89+
accessibilityState: disabled ? ['disabled'] : value ? ['checked'] : ['unchecked'],
9090
accessibilityValue: {text: value ? '1' : '0'}
9191
};
9292
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {RenderAPI, fireEvent} from '@testing-library/react-native';
2+
3+
4+
export const SwitchDriverFactory = ({wrappedComponent, testID}: {wrappedComponent: RenderAPI, testID: string}) => {
5+
6+
const switchComp = wrappedComponent.getByTestId(testID);
7+
8+
return {
9+
exists: () => !!switchComp,
10+
getRootElement: () => switchComp,
11+
press: () => fireEvent(switchComp, 'press'),
12+
getAccessibilityValue: () => switchComp.props.accessibilityValue.text === '1',
13+
isDisabled: () => switchComp.props.accessibilityState.disabled === true,
14+
isChecked: () => switchComp.props.accessibilityState['0'] === 'checked',
15+
getColor: () => switchComp.props.style.backgroundColor
16+
};
17+
18+
};

src/testkit/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export {SwitchDriverFactory} from '../components/switch/switch.driver';
2+
13
export {default as ButtonDriverFactory} from '../components/button/Button.driver';
24
export {default as TextDriverFactory} from '../components/Text/Text.driver';
3-
export {default as ImageDriver} from '../components/image/Image.driver';
5+
export {default as ImageDriverFactory} from '../components/image/Image.driver';

0 commit comments

Comments
 (0)