|
| 1 | +import {fireEvent, render} from '@testing-library/react-native' |
| 2 | +import React from 'react' |
| 3 | +import {Switch} from '../components' |
| 4 | + |
| 5 | +describe('Switch', () => { |
| 6 | + const value = true |
| 7 | + const onValueChange = jest.fn() |
| 8 | + |
| 9 | + it('renders without errors', () => { |
| 10 | + const {getByTestId} = render(<Switch value={value} onValueChange={onValueChange} />) |
| 11 | + const switchContainer = getByTestId('switch-container') |
| 12 | + |
| 13 | + expect(switchContainer).toBeDefined() |
| 14 | + }) |
| 15 | + |
| 16 | + it('calls onValueChange when pressed', () => { |
| 17 | + const {getByTestId} = render(<Switch value={value} onValueChange={onValueChange} />) |
| 18 | + const switchContainer = getByTestId('switch-container') |
| 19 | + |
| 20 | + fireEvent.press(switchContainer) |
| 21 | + expect(onValueChange).toHaveBeenCalled() |
| 22 | + }) |
| 23 | + |
| 24 | + it('renders with the correct track color', () => { |
| 25 | + const trackColor = 'red' |
| 26 | + const {getByTestId} = render( |
| 27 | + <Switch value={value} onValueChange={onValueChange} trackColor={trackColor} />, |
| 28 | + ) |
| 29 | + const trackActive = getByTestId('track-active') |
| 30 | + const trackInActive = getByTestId('track-in-active') |
| 31 | + |
| 32 | + expect(trackActive.props.style.backgroundColor).toBe(trackColor) |
| 33 | + expect(trackInActive.props.style.backgroundColor).toBe(trackColor) |
| 34 | + }) |
| 35 | + |
| 36 | + it('renders with the correct object track color', () => { |
| 37 | + const trackColor = {active: 'red', inActive: 'grey'} |
| 38 | + const {getByTestId} = render( |
| 39 | + <Switch value={value} onValueChange={onValueChange} trackColor={trackColor} />, |
| 40 | + ) |
| 41 | + const trackActive = getByTestId('track-active') |
| 42 | + const trackInActive = getByTestId('track-in-active') |
| 43 | + |
| 44 | + expect(trackActive.props.style.backgroundColor).toBe(trackColor.active) |
| 45 | + expect(trackInActive.props.style.backgroundColor).toBe(trackColor.inActive) |
| 46 | + }) |
| 47 | + |
| 48 | + it('renders with the thembSize prop', () => { |
| 49 | + const thumbSize = 20 |
| 50 | + const {getByTestId} = render(<Switch value={value} onValueChange={onValueChange} thumbSize={thumbSize} />) |
| 51 | + const trackActive = getByTestId('track-active') |
| 52 | + const trackInActive = getByTestId('track-in-active') |
| 53 | + const thumb = getByTestId('thumb') |
| 54 | + |
| 55 | + expect(trackActive.props.thumbSize).toBe(thumbSize) |
| 56 | + expect(trackInActive.props.thumbSize).toBe(thumbSize) |
| 57 | + expect(thumb.props.thumbSize).toBe(thumbSize) |
| 58 | + }) |
| 59 | + |
| 60 | + it('renders with the thumbColor prop', () => { |
| 61 | + const thumbColor = 'red' |
| 62 | + const {getByTestId} = render( |
| 63 | + <Switch value={value} onValueChange={onValueChange} thumbColor={thumbColor} />, |
| 64 | + ) |
| 65 | + const thumb = getByTestId('thumb') |
| 66 | + |
| 67 | + expect(thumb.props.thumbColor).toBe(thumbColor) |
| 68 | + }) |
| 69 | + |
| 70 | + it('disables the component when disabled prop is true', () => { |
| 71 | + const {getByTestId} = render(<Switch value={value} onValueChange={onValueChange} disabled />) |
| 72 | + const switchContainer = getByTestId('switch-container') |
| 73 | + |
| 74 | + fireEvent.press(switchContainer) |
| 75 | + expect(onValueChange).toHaveBeenCalled() |
| 76 | + }) |
| 77 | + |
| 78 | + it('renders with the correct text inside for active and inactive states', () => { |
| 79 | + const textInside = { |
| 80 | + active: 'Active', |
| 81 | + inActive: 'Inactive', |
| 82 | + } |
| 83 | + const {getByText} = render( |
| 84 | + <Switch value={value} onValueChange={onValueChange} textInside={textInside} variant="inside" />, |
| 85 | + ) |
| 86 | + const labelActive = getByText(textInside.active) |
| 87 | + const labelInActive = getByText(textInside.inActive) |
| 88 | + |
| 89 | + expect(labelActive).toBeDefined() |
| 90 | + expect(labelInActive).toBeDefined() |
| 91 | + }) |
| 92 | + |
| 93 | + it('renders with custom text inside color', () => { |
| 94 | + const textInsideColor = { |
| 95 | + active: 'green', |
| 96 | + inActive: 'red', |
| 97 | + } |
| 98 | + const {getByTestId} = render( |
| 99 | + <Switch |
| 100 | + value={value} |
| 101 | + onValueChange={onValueChange} |
| 102 | + textInsideColor={textInsideColor} |
| 103 | + variant="inside" |
| 104 | + />, |
| 105 | + ) |
| 106 | + const labelActive = getByTestId('label-active') |
| 107 | + const labelInActive = getByTestId('label-in-active') |
| 108 | + |
| 109 | + expect(labelActive.props.color).toBe(textInsideColor.active) |
| 110 | + expect(labelInActive.props.color).toBe(textInsideColor.inActive) |
| 111 | + }) |
| 112 | + |
| 113 | + it('renders with the trackPaddingInside prop with variant inside', () => { |
| 114 | + const trackPaddingInside = 4 |
| 115 | + const {getByTestId} = render( |
| 116 | + <Switch |
| 117 | + value={value} |
| 118 | + onValueChange={onValueChange} |
| 119 | + trackPaddingInside={trackPaddingInside} |
| 120 | + variant="inside" |
| 121 | + />, |
| 122 | + ) |
| 123 | + const switchContainer = getByTestId('switch-container') |
| 124 | + |
| 125 | + expect(switchContainer.props.trackMargin).toBe(trackPaddingInside) |
| 126 | + }) |
| 127 | +}) |
0 commit comments