|
| 1 | +import React from 'react' |
| 2 | +import { render, cleanup, fireEvent } from 'react-testing-library' |
| 3 | +import { Provider } from 'unstated' |
| 4 | +import CounterContainer from '../../state/CounterContainer' |
| 5 | +import Counter from '../counter' |
| 6 | + |
| 7 | +afterEach(cleanup) |
| 8 | + |
| 9 | +test('Counter corectly triggers changes in counterContainer', async () => { |
| 10 | + const counterContainer = new CounterContainer() |
| 11 | + const { container, getByText } = render( |
| 12 | + <Provider inject={[counterContainer]}> |
| 13 | + <Counter /> |
| 14 | + </Provider> |
| 15 | + ) |
| 16 | + |
| 17 | + await fireEvent( |
| 18 | + getByText('+'), |
| 19 | + new MouseEvent('click', { |
| 20 | + bubbles: true, |
| 21 | + cancelable: true, |
| 22 | + }) |
| 23 | + ) |
| 24 | + expect(counterContainer.state.count).toBe(1) |
| 25 | + |
| 26 | + await fireEvent( |
| 27 | + getByText('-'), |
| 28 | + new MouseEvent('click', { |
| 29 | + bubbles: true, |
| 30 | + cancelable: true, |
| 31 | + }) |
| 32 | + ) |
| 33 | + expect(counterContainer.state.count).toBe(0) |
| 34 | +}) |
| 35 | + |
| 36 | +test('Counter corectly triggers methods of counterContainer', async () => { |
| 37 | + const counterContainer = new CounterContainer() |
| 38 | + counterContainer.increment = jest.fn() |
| 39 | + counterContainer.decrement = jest.fn() |
| 40 | + |
| 41 | + const { container, getByText } = render( |
| 42 | + <Provider inject={[counterContainer]}> |
| 43 | + <Counter /> |
| 44 | + </Provider> |
| 45 | + ) |
| 46 | + |
| 47 | + await fireEvent( |
| 48 | + getByText('+'), |
| 49 | + new MouseEvent('click', { |
| 50 | + bubbles: true, |
| 51 | + cancelable: true, |
| 52 | + }) |
| 53 | + ) |
| 54 | + |
| 55 | + expect(counterContainer.increment).toHaveBeenCalledTimes(1) |
| 56 | + |
| 57 | + await fireEvent( |
| 58 | + getByText('-'), |
| 59 | + new MouseEvent('click', { |
| 60 | + bubbles: true, |
| 61 | + cancelable: true, |
| 62 | + }) |
| 63 | + ) |
| 64 | + |
| 65 | + expect(counterContainer.decrement).toHaveBeenCalledTimes(1) |
| 66 | +}) |
0 commit comments