|
| 1 | +import React from 'react'; |
| 2 | +import { Text, View } from 'react-native'; |
| 3 | + |
| 4 | +import { render, screen } from '@testing-library/react-native'; |
| 5 | + |
| 6 | +import { ThemeProvider } from '../../../contexts/themeContext/ThemeContext'; |
| 7 | +import { defaultTheme } from '../../../contexts/themeContext/utils/theme'; |
| 8 | +import { EmptySearchResult } from '../EmptySearchResult'; |
| 9 | + |
| 10 | +type EmptySearchResultProps = React.ComponentProps<typeof EmptySearchResult>; |
| 11 | + |
| 12 | +const renderComponent = (props: Partial<EmptySearchResultProps> = {}) => |
| 13 | + render( |
| 14 | + <ThemeProvider theme={defaultTheme}> |
| 15 | + <EmptySearchResult |
| 16 | + icon={<View testID='empty-search-result-icon' />} |
| 17 | + label='No results' |
| 18 | + {...props} |
| 19 | + /> |
| 20 | + </ThemeProvider>, |
| 21 | + ); |
| 22 | + |
| 23 | +describe('EmptySearchResult', () => { |
| 24 | + it('renders the empty-search-result testID', () => { |
| 25 | + renderComponent(); |
| 26 | + |
| 27 | + expect(screen.getByTestId('empty-search-result')).toBeTruthy(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('renders the icon node passed via the icon prop', () => { |
| 31 | + renderComponent({ icon: <View testID='custom-icon' /> }); |
| 32 | + |
| 33 | + expect(screen.getByTestId('custom-icon')).toBeTruthy(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('renders the label text', () => { |
| 37 | + renderComponent({ label: 'Nothing here' }); |
| 38 | + |
| 39 | + expect(screen.getByText('Nothing here')).toBeTruthy(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('honors a custom container style override from the theme', () => { |
| 43 | + const customTheme = { |
| 44 | + ...defaultTheme, |
| 45 | + emptySearchResult: { |
| 46 | + container: { backgroundColor: 'rgb(255, 0, 0)' }, |
| 47 | + text: {}, |
| 48 | + }, |
| 49 | + }; |
| 50 | + |
| 51 | + render( |
| 52 | + <ThemeProvider theme={customTheme}> |
| 53 | + <EmptySearchResult icon={<View />} label='Empty' /> |
| 54 | + </ThemeProvider>, |
| 55 | + ); |
| 56 | + |
| 57 | + const container = screen.getByTestId('empty-search-result'); |
| 58 | + const flattened = Array.isArray(container.props.style) |
| 59 | + ? Object.assign({}, ...container.props.style.flat(Infinity).filter(Boolean)) |
| 60 | + : container.props.style; |
| 61 | + expect(flattened.backgroundColor).toBe('rgb(255, 0, 0)'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('honors a custom text style override from the theme', () => { |
| 65 | + const customTheme = { |
| 66 | + ...defaultTheme, |
| 67 | + emptySearchResult: { |
| 68 | + container: {}, |
| 69 | + text: { color: 'rgb(0, 255, 0)' }, |
| 70 | + }, |
| 71 | + }; |
| 72 | + |
| 73 | + render( |
| 74 | + <ThemeProvider theme={customTheme}> |
| 75 | + <EmptySearchResult icon={<View />} label='Empty' /> |
| 76 | + </ThemeProvider>, |
| 77 | + ); |
| 78 | + |
| 79 | + const label = screen.getByText('Empty') as unknown as { props: { style: unknown } }; |
| 80 | + const flattened = Array.isArray(label.props.style) |
| 81 | + ? Object.assign({}, ...(label.props.style as unknown[]).flat(Infinity).filter(Boolean)) |
| 82 | + : label.props.style; |
| 83 | + expect((flattened as { color?: string }).color).toBe('rgb(0, 255, 0)'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('renders any node passed as icon, including text', () => { |
| 87 | + renderComponent({ icon: <Text testID='icon-as-text'>icon</Text> }); |
| 88 | + |
| 89 | + expect(screen.getByTestId('icon-as-text')).toBeTruthy(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('renders the loading testID and hides the icon/label when loading is true', () => { |
| 93 | + renderComponent({ |
| 94 | + icon: <View testID='hidden-icon' />, |
| 95 | + label: 'No results', |
| 96 | + loading: true, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(screen.getByTestId('empty-search-result-loading')).toBeTruthy(); |
| 100 | + expect(screen.queryByTestId('empty-search-result')).toBeNull(); |
| 101 | + expect(screen.queryByTestId('hidden-icon')).toBeNull(); |
| 102 | + expect(screen.queryByText('No results')).toBeNull(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('renders the icon/label when loading is false', () => { |
| 106 | + renderComponent({ loading: false }); |
| 107 | + |
| 108 | + expect(screen.getByTestId('empty-search-result')).toBeTruthy(); |
| 109 | + expect(screen.queryByTestId('empty-search-result-loading')).toBeNull(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments