|
1 | 1 | import React from 'react';
|
| 2 | +import {render} from '@testing-library/react-native'; |
2 | 3 | import Switch, {SwitchProps} from '../index';
|
3 |
| -import View from '../../view'; |
4 | 4 | import {SwitchDriver} from '../Switch.driver';
|
5 | 5 |
|
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'; |
25 | 14 |
|
| 15 | +const testCase = (testID: string, props: Partial<SwitchProps>) => { |
| 16 | + const renderTree = render(<Switch testID={testID} {...defaultProps} {...props}/>); |
| 17 | + return SwitchDriver({renderTree, testID}); |
| 18 | +}; |
26 | 19 |
|
| 20 | +describe('Switch', () => { |
27 | 21 | it('Should fire onChange event', async () => {
|
28 |
| - const testId = 'switch-comp'; |
29 | 22 | 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(); |
32 | 25 | expect(onChange).toHaveBeenCalled();
|
33 | 26 | });
|
34 | 27 |
|
35 | 28 | it('Should fire onChange event with false value when toggling off', async () => {
|
36 |
| - const testId = 'switch-comp'; |
37 | 29 | 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(); |
40 | 32 | expect(onChange).toHaveBeenCalledWith(false);
|
41 | 33 | });
|
42 | 34 |
|
43 | 35 | it('Should fire onChange event with true value when toggling on', async () => {
|
44 |
| - const testId = 'switch-comp'; |
45 | 36 | 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(); |
48 | 39 | expect(onChange).toHaveBeenCalledWith(true);
|
49 | 40 | });
|
50 | 41 |
|
51 | 42 | it('Should not fire onChange when disabled', async () => {
|
52 |
| - const testId = 'switch-comp'; |
53 | 43 | const onValueChange = jest.fn();
|
54 |
| - const driver = await switchDriver(testId, {disabled: true, onValueChange}); |
| 44 | + const driver = testCase(testID, {disabled: true, onValueChange}); |
55 | 45 |
|
56 |
| - await driver.press(); |
| 46 | + driver.press(); |
57 | 47 | expect(onValueChange).not.toHaveBeenCalled();
|
58 | 48 | });
|
59 | 49 |
|
60 | 50 | 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}); |
63 | 52 |
|
64 |
| - expect(await driver.getAccessibilityValue()).toBe(true); |
| 53 | + expect(driver.getAccessibilityValue()).toBe(true); |
65 | 54 | });
|
66 | 55 |
|
67 | 56 | 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}); |
70 | 58 |
|
71 |
| - expect(await driver.isChecked()).toBe(false); |
| 59 | + expect(driver.isChecked()).toBe(false); |
72 | 60 | });
|
73 | 61 |
|
74 | 62 | 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}); |
77 | 64 |
|
78 |
| - expect(await driver.isChecked()).toBe(true); |
| 65 | + expect(driver.isChecked()).toBe(true); |
79 | 66 | });
|
80 | 67 |
|
81 | 68 | 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}); |
84 | 70 |
|
85 |
| - expect(await driver.getAccessibilityValue()).toBe(false); |
| 71 | + expect(driver.getAccessibilityValue()).toBe(false); |
86 | 72 | });
|
87 | 73 |
|
88 | 74 | 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}); |
91 | 76 |
|
92 |
| - expect(await driver.isDisabled()).toBe(true); |
| 77 | + expect(driver.isDisabled()).toBe(true); |
93 | 78 | });
|
94 | 79 |
|
95 | 80 | 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}); |
98 | 82 |
|
99 |
| - expect(await driver.isDisabled()).toBe(false); |
| 83 | + expect(driver.isDisabled()).toBe(false); |
100 | 84 | });
|
101 | 85 |
|
102 | 86 | 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); |
107 | 90 | });
|
108 | 91 |
|
109 | 92 | 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}); |
112 | 94 |
|
113 |
| - expect(await driver.getColor()).toBe('red'); |
| 95 | + expect(driver.getStyle()?.backgroundColor).toBe(offColor); |
114 | 96 | });
|
115 | 97 | });
|
0 commit comments