|
| 1 | +--- |
| 2 | +id: redux-integration |
| 3 | +title: Redux Integration |
| 4 | +--- |
| 5 | + |
| 6 | +This section deals with testing RN applications developed with Redux. We will be developing a simple TODO application capable of adding and removing an item. Once included, the timestamp is included. |
| 7 | + |
| 8 | +## Setting up |
| 9 | + |
| 10 | +An example of setting up can be found [here](https://github.com/callstack/react-native-testing-library/tree/master/examples/redux). |
| 11 | + |
| 12 | +## Test cases |
| 13 | + |
| 14 | +Our test is on the components that either dispatch actions on the redux store or read some data from the redux store. This means we will test `./components/TodoElem.js` and `./components/TodoList.js`. Thus we will create `./components/AddTodo.test.js` and `./components/TodoList.test.js` |
| 15 | + |
| 16 | +For `./components/AddTodo.test.js` |
| 17 | + |
| 18 | +```jsx |
| 19 | +import React from 'react'; |
| 20 | +import { Provider } from 'react-redux'; |
| 21 | +import { cleanup, fireEvent, render } from 'react-native-testing-library'; |
| 22 | +import configureStore from '../store'; |
| 23 | +import AddTodo from './AddTodo'; |
| 24 | + |
| 25 | +describe('AddTodo component test', () => { |
| 26 | + test('adds a new TODO when the button is pressed', () => { |
| 27 | + const store = configureStore(); |
| 28 | + |
| 29 | + const component = ( |
| 30 | + <Provider store={store}> |
| 31 | + <AddTodo /> |
| 32 | + </Provider> |
| 33 | + ); |
| 34 | + |
| 35 | + const { getByPlaceholder, getByText } = render(component); |
| 36 | + |
| 37 | + // There is a TextInput. |
| 38 | + // https://github.com/callstack/react-native-testing-library/blob/ae3d4af370487e1e8fedd8219f77225690aefc59/examples/redux/components/AddTodo.js#L24 |
| 39 | + const input = getByPlaceholder(/repository/i); |
| 40 | + expect(input).toBeTruthy(); |
| 41 | + |
| 42 | + const textToEnter = 'This is a random element'; |
| 43 | + fireEvent.changeText(input, textToEnter); |
| 44 | + fireEvent.press(getByText('Submit form')); |
| 45 | + |
| 46 | + const todosState = store.getState().todos; |
| 47 | + |
| 48 | + expect(todosState.length).toEqual(1); |
| 49 | + |
| 50 | + expect(todosState).toEqual( |
| 51 | + expect.arrayContaining([ |
| 52 | + expect.objectContaining({ |
| 53 | + id: 1, |
| 54 | + text: textToEnter, |
| 55 | + date: expect.any(Date), |
| 56 | + }), |
| 57 | + ]) |
| 58 | + ); |
| 59 | + }); |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +For the `./components/TodoList.js` |
| 64 | + |
| 65 | +```jsx |
| 66 | +import React from 'react'; |
| 67 | +import { Provider } from 'react-redux'; |
| 68 | +import { fireEvent, render } from 'react-native-testing-library'; |
| 69 | +import configureStore from '../store'; |
| 70 | +import TodoList from './TodoList'; |
| 71 | + |
| 72 | +describe('TodoList component test', () => { |
| 73 | + test('it should execute with a store with 4 elements', () => { |
| 74 | + const initialState = { |
| 75 | + todos: [ |
| 76 | + { id: 1, text: 'Sing something', date: new Date() }, |
| 77 | + { id: 2, text: 'Dance something', date: new Date() }, |
| 78 | + { id: 3, text: 'Sleep something', date: new Date() }, |
| 79 | + { id: 4, text: 'Sleep something', date: new Date() }, |
| 80 | + ], |
| 81 | + }; |
| 82 | + const store = configureStore(initialState); |
| 83 | + |
| 84 | + const component = ( |
| 85 | + <Provider store={store}> |
| 86 | + <TodoList /> |
| 87 | + </Provider> |
| 88 | + ); |
| 89 | + |
| 90 | + const { getAllByText } = render(component); |
| 91 | + const todoElems = getAllByText(/something/i); |
| 92 | + |
| 93 | + expect(todoElems.length).toEqual(4); |
| 94 | + }); |
| 95 | + |
| 96 | + test('should execute with 2 elements and end up with 1 after delete', () => { |
| 97 | + const initialState = { |
| 98 | + todos: [ |
| 99 | + { id: 1, text: 'Sing something', date: new Date() }, |
| 100 | + { id: 2, text: 'Dance something', date: new Date() }, |
| 101 | + ], |
| 102 | + }; |
| 103 | + const store = configureStore(initialState); |
| 104 | + |
| 105 | + const component = ( |
| 106 | + <Provider store={store}> |
| 107 | + <TodoList /> |
| 108 | + </Provider> |
| 109 | + ); |
| 110 | + |
| 111 | + const { getAllByText } = render(component); |
| 112 | + const todoElems = getAllByText(/something/i); |
| 113 | + |
| 114 | + expect(todoElems.length).toBe(2); |
| 115 | + |
| 116 | + const buttons = getAllByText('Delete'); |
| 117 | + expect(buttons.length).toBe(2); |
| 118 | + |
| 119 | + fireEvent.press(buttons[0]); |
| 120 | + expect(getAllByText('Delete').length).toBe(1); |
| 121 | + }); |
| 122 | +}); |
| 123 | +``` |
| 124 | + |
| 125 | +## Running tests |
| 126 | + |
| 127 | +To run the tests, place a test script inside your package.json |
| 128 | + |
| 129 | +```json |
| 130 | +{ |
| 131 | + "scripts": { |
| 132 | + "test": "jest" |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +And run the test script with npm test or yarn test. |
0 commit comments