|
| 1 | +import React, {useState, useCallback} from 'react'; |
| 2 | +import {render, fireEvent} from '@testing-library/react-native'; |
| 3 | +import Checkbox from '../index'; |
| 4 | + |
| 5 | +const testID = 'checkbox'; |
| 6 | +const onValueChange = jest.fn(); |
| 7 | + |
| 8 | +const TestCase = props => { |
| 9 | + const {value, onValueChange, ...others} = props; |
| 10 | + |
| 11 | + const [_value, _setValue] = useState(value); |
| 12 | + const _onValueChange = useCallback(newValue => { |
| 13 | + _setValue(newValue); |
| 14 | + onValueChange?.(newValue); |
| 15 | + }, |
| 16 | + [_setValue, onValueChange]); |
| 17 | + |
| 18 | + return <Checkbox {...others} onValueChange={_onValueChange} value={_value} testID={testID}/>; |
| 19 | +}; |
| 20 | + |
| 21 | +describe('Checkbox renderer test', () => { |
| 22 | + beforeEach(() => { |
| 23 | + jest.clearAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('Default value is false', () => { |
| 27 | + const props = {onValueChange}; |
| 28 | + |
| 29 | + const {getByTestId} = render(<TestCase {...props}/>); |
| 30 | + |
| 31 | + const checkbox = getByTestId(testID); |
| 32 | + fireEvent.press(checkbox); |
| 33 | + expect(onValueChange).toHaveBeenCalledTimes(1); |
| 34 | + expect(onValueChange).toHaveBeenCalledWith(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('Send value (false)', () => { |
| 38 | + const props = {onValueChange, value: false}; |
| 39 | + |
| 40 | + const {getByTestId} = render(<TestCase {...props}/>); |
| 41 | + |
| 42 | + const checkbox = getByTestId(testID); |
| 43 | + fireEvent.press(checkbox); |
| 44 | + expect(onValueChange).toHaveBeenCalledTimes(1); |
| 45 | + expect(onValueChange).toHaveBeenCalledWith(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it('Send value (true)', () => { |
| 49 | + const props = {onValueChange, value: true}; |
| 50 | + |
| 51 | + const {getByTestId} = render(<TestCase {...props}/>); |
| 52 | + |
| 53 | + const checkbox = getByTestId(testID); |
| 54 | + fireEvent.press(checkbox); |
| 55 | + expect(onValueChange).toHaveBeenCalledTimes(1); |
| 56 | + expect(onValueChange).toHaveBeenCalledWith(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it('Multiple clicks', () => { |
| 60 | + const props = {onValueChange}; |
| 61 | + |
| 62 | + const {getByTestId} = render(<TestCase {...props}/>); |
| 63 | + |
| 64 | + const checkbox = getByTestId(testID); |
| 65 | + fireEvent.press(checkbox); |
| 66 | + expect(onValueChange).toHaveBeenCalledTimes(1); |
| 67 | + expect(onValueChange).toHaveBeenCalledWith(true); |
| 68 | + fireEvent.press(checkbox); |
| 69 | + expect(onValueChange.mock.calls).toEqual([[true], [false]]); |
| 70 | + fireEvent.press(checkbox); |
| 71 | + expect(onValueChange.mock.calls).toEqual([[true], [false], [true]]); |
| 72 | + }); |
| 73 | + |
| 74 | + it('Disabled (value = false)', () => { |
| 75 | + const props = {onValueChange, value: false, disabled: true}; |
| 76 | + |
| 77 | + const {getByTestId} = render(<TestCase {...props}/>); |
| 78 | + |
| 79 | + const checkbox = getByTestId(testID); |
| 80 | + fireEvent.press(checkbox); |
| 81 | + expect(onValueChange).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('Disabled (value = true)', () => { |
| 85 | + const props = {onValueChange, value: true, disabled: true}; |
| 86 | + const {getByTestId} = render(<TestCase {...props}/>); |
| 87 | + |
| 88 | + const checkbox = getByTestId(testID); |
| 89 | + fireEvent.press(checkbox); |
| 90 | + expect(onValueChange).not.toHaveBeenCalled(); |
| 91 | + }); |
| 92 | +}); |
0 commit comments