|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent, render, screen } from '@testing-library/react-native'; |
| 3 | + |
| 4 | +import { Text } from '../text/text.component'; |
| 5 | +import { Checkbox } from './checkbox.component'; |
| 6 | +import { CheckboxGroup } from './group.component'; |
| 7 | +import { Box } from '../box/box.component'; |
| 8 | +import { defaultTheme } from '../../theme/theme.default'; |
| 9 | + |
| 10 | +describe('Checkbox component', () => { |
| 11 | + it('should render component without error', () => { |
| 12 | + const checkboxId = 'checkboxId'; |
| 13 | + |
| 14 | + render( |
| 15 | + <Checkbox |
| 16 | + testID={checkboxId} |
| 17 | + value={1} |
| 18 | + prefix={<Text flex={1}>Option 1</Text>} |
| 19 | + /> |
| 20 | + ); |
| 21 | + |
| 22 | + expect(screen.getByTestId(checkboxId)).toBeDefined(); |
| 23 | + expect(screen.getByText('Option 1')).toBeTruthy(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should option press trigger onChecked function', () => { |
| 27 | + const checkboxId = 'checkboxId'; |
| 28 | + const textId = 'textId'; |
| 29 | + const onCheckedMock = jest.fn(); |
| 30 | + render( |
| 31 | + <Checkbox |
| 32 | + testID={checkboxId} |
| 33 | + onChecked={onCheckedMock} |
| 34 | + value={1} |
| 35 | + prefix={ |
| 36 | + <Text testID={textId} flex={1}> |
| 37 | + Option 1 |
| 38 | + </Text> |
| 39 | + } |
| 40 | + /> |
| 41 | + ); |
| 42 | + |
| 43 | + const text = screen.getByTestId(textId); |
| 44 | + |
| 45 | + expect(onCheckedMock).toHaveBeenCalledTimes(0); |
| 46 | + fireEvent.press(text); |
| 47 | + expect(onCheckedMock).toHaveBeenCalledTimes(1); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should group component render with children without errors ', () => { |
| 51 | + const checkboxGroupId = 'checkboxGroupId'; |
| 52 | + render( |
| 53 | + <CheckboxGroup testID={checkboxGroupId} colorScheme="green"> |
| 54 | + <Checkbox value={1}>Option 1</Checkbox> |
| 55 | + <Checkbox value={2}>Option 2</Checkbox> |
| 56 | + </CheckboxGroup> |
| 57 | + ); |
| 58 | + |
| 59 | + const group = screen.getByTestId(checkboxGroupId); |
| 60 | + const option1 = screen.getByText('Option 1'); |
| 61 | + const option2 = screen.getByText('Option 2'); |
| 62 | + |
| 63 | + expect(group).toBeDefined(); |
| 64 | + expect(option1).toBeVisible(); |
| 65 | + expect(option2).toBeVisible(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should checkbox state affect rendering', () => { |
| 69 | + render( |
| 70 | + <CheckboxGroup flexDirection="row"> |
| 71 | + {['Option 1', 'Option 2'].map((item) => ( |
| 72 | + <Checkbox value={item}> |
| 73 | + {({ isChecked }) => ( |
| 74 | + <Box |
| 75 | + bg={isChecked ? 'blue.600' : 'blue.100'} |
| 76 | + px="xl" |
| 77 | + py="md" |
| 78 | + mr="md" |
| 79 | + borderRadius="full" |
| 80 | + > |
| 81 | + <Text color={isChecked ? 'white' : 'red.800'}>{item}</Text> |
| 82 | + </Box> |
| 83 | + )} |
| 84 | + </Checkbox> |
| 85 | + ))} |
| 86 | + </CheckboxGroup> |
| 87 | + ); |
| 88 | + |
| 89 | + const option1 = screen.getByText('Option 1'); |
| 90 | + const option2 = screen.getByText('Option 2'); |
| 91 | + |
| 92 | + const red800 = defaultTheme.colors?.red[800]; |
| 93 | + const white = defaultTheme.colors?.white as string; |
| 94 | + |
| 95 | + expect(option1).toBeVisible(); |
| 96 | + expect(option2).toBeVisible(); |
| 97 | + |
| 98 | + expect(option1).toHaveStyle({ |
| 99 | + color: red800, |
| 100 | + }); |
| 101 | + |
| 102 | + fireEvent.press(option2); |
| 103 | + |
| 104 | + expect(option2).toHaveStyle({ |
| 105 | + color: white, |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments