|
| 1 | +--- |
| 2 | +index: 6 |
| 3 | +path: "/foundation/testing" |
| 4 | +title: 'Testing' |
| 5 | +--- |
| 6 | +# |
| 7 | +<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center"}}> |
| 8 | + <h1>Testing</h1> |
| 9 | + <label style={{ |
| 10 | + backgroundColor: "#5848ff", |
| 11 | + color: "#ffffff", |
| 12 | + borderRadius: "5px", |
| 13 | + padding: "5px 10px", |
| 14 | + margin: "10px", |
| 15 | + display: "flex", |
| 16 | + alignItems: "center" |
| 17 | + }}> |
| 18 | + <span>Experimental</span> |
| 19 | + </label> |
| 20 | +</div> |
| 21 | + |
| 22 | +Testkits allows us to test components without knowing the internal implementation, making it easier to test and reduce over head from migrations and changes in implementation. For example: |
| 23 | +* Changing the input of a `TextField` component can be done using the driver's `changeText` |
| 24 | +* Pressing a button could be achieved using the Button driver's press function. |
| 25 | +## How to use the testkits |
| 26 | +### Initializing the driver |
| 27 | +In order to initialize a test driver you pass it the renderTree and the component's testId as an object. |
| 28 | + |
| 29 | +### Example |
| 30 | +Suppose we have a form that takes a `first name`, `last name` and an `address` and we want to test the submitting of this form. Our form component will look something like this: |
| 31 | +```jsx |
| 32 | +import {Button, TextField, View} from '@wix/wix-react-native-ui-lib'; |
| 33 | + |
| 34 | +type OnSubmitHandler = (firstName: string, lastName: string, address: string) => void; |
| 35 | +const MyForm = (props: {onSubmit: OnSubmitHandler}) => { |
| 36 | + const {onSubmit} = props; |
| 37 | + const [firstName, setFirstName] = useState(''); |
| 38 | + const [lastName, setLastName] = useState(''); |
| 39 | + const [address, setAddress] = useState(''); |
| 40 | + return ( |
| 41 | + <View> |
| 42 | + <TextField label="First name" onChangeText={(value) => setFirstName(value)} value={firstName}/> |
| 43 | + <TextField label="Last name" onChangeText={(value) => setLastName(value)} value={lastName}/> |
| 44 | + <TextField label="Address" onChangeText={(value) => setAddress(value)} value={address}/> |
| 45 | + <Button label="Submit" onPress={() => onSubmit(firstName, lastName, address)}/> |
| 46 | + </View> |
| 47 | + ); |
| 48 | +}; |
| 49 | +``` |
| 50 | +### Testing our flow |
| 51 | +#### In order to test our flow we would do the following steps: |
| 52 | +1. Import the TextField and Button driver from UI-LIB's testkit |
| 53 | +```javascript |
| 54 | +import {TextFieldDriver, ButtonDriver} from '@wix/react-native-ui-lib/testkit'; |
| 55 | +``` |
| 56 | +2. render our test case |
| 57 | +```javascript |
| 58 | +const renderTree = render(<MyForm onSubmit={onSubmit}/>); |
| 59 | +``` |
| 60 | +3. Initialize our drivers for the TextFields and submit button |
| 61 | +```javascript |
| 62 | +const firstNameDriver = TextFieldDriver({renderTree, testID: 'firstName'}); |
| 63 | +const lastNameDriver = TextFieldDriver({renderTree, testID: 'lastName'}); |
| 64 | +const addressDriver = TextFieldDriver({renderTree, testID: 'address'}); |
| 65 | +const submitBtnDriver = ButtonDriver({renderTree, testID: 'submit'}); |
| 66 | +``` |
| 67 | +4. Change the text of the fields and submit the form. |
| 68 | +```javascript |
| 69 | +firstNameDriver.changeText('Musa'); |
| 70 | +lastNameDriver.changeText('The Man'); |
| 71 | +addressDriver.changeText('Yunitzman 5'); |
| 72 | +submitBtnDriver.press(); |
| 73 | +``` |
| 74 | +5. Check that the correct values were passed to the submit handler |
| 75 | +```javascript |
| 76 | +expect(onSubmit).toHaveBeenCalledWith('Musa', 'The Man', 'Yunitzman 5'); |
| 77 | +``` |
| 78 | +<details> |
| 79 | +<summary>Full test</summary> |
| 80 | + |
| 81 | +```javascript |
| 82 | +describe('My Form', () => { |
| 83 | + it('should submit MyForm with Musa The Man, Yunitzman 5', () => { |
| 84 | + const onSubmit = jest.fn(); |
| 85 | + const renderTree = render(<MyForm onSubmit={onSubmit}/>); |
| 86 | + const firstNameDriver = TextFieldDriver({renderTree, testID: 'firstName'}); |
| 87 | + const lastNameDriver = TextFieldDriver({renderTree, testID: 'lastName'}); |
| 88 | + const addressDriver = TextFieldDriver({renderTree, testID: 'address'}); |
| 89 | + const submitBtnDriver = ButtonDriver({renderTree, testID: 'submit'}); |
| 90 | + firstNameDriver.changeText('Musa'); |
| 91 | + lastNameDriver.changeText('The Man'); |
| 92 | + addressDriver.changeText('Yunitzman 5'); |
| 93 | + submitBtnDriver.press(); |
| 94 | + expect(onSubmit).toHaveBeenCalledWith('Musa', 'The Man', 'Yunitzman 5'); |
| 95 | + }); |
| 96 | +}); |
| 97 | +``` |
| 98 | +</details> |
| 99 | + |
| 100 | + |
0 commit comments