|
| 1 | +import { fireEvent, render } from '@testing-library/react' |
| 2 | +import { DevupThemeTypography } from 'node_modules/@devup-ui/react/dist/types/typography' |
| 3 | + |
| 4 | +import { ClearButton, Input } from '..' |
| 5 | +import { Controlled } from '../Controlled' |
| 6 | +import { GlassIcon } from '../GlassIcon' |
| 7 | + |
| 8 | +describe('Input', () => { |
| 9 | + it('should render with default props', () => { |
| 10 | + const { container } = render(<Input />) |
| 11 | + expect(container).toMatchSnapshot() |
| 12 | + }) |
| 13 | + |
| 14 | + it('should render with disabled prop', () => { |
| 15 | + const { container } = render(<Input disabled />) |
| 16 | + expect(container).toMatchSnapshot() |
| 17 | + }) |
| 18 | + |
| 19 | + it('should render with allowClear prop', () => { |
| 20 | + const { container } = render(<Input allowClear />) |
| 21 | + expect(container).toMatchSnapshot() |
| 22 | + expect(container.querySelector('[aria-label="input"]')).toHaveClass( |
| 23 | + 'padding-right-0-36px--1', |
| 24 | + ) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should not have padding right when allowClear is false', () => { |
| 28 | + const { container } = render(<Input allowClear={false} />) |
| 29 | + expect(container).toMatchSnapshot() |
| 30 | + expect(container.querySelector('[aria-label="input"]')).not.toHaveClass( |
| 31 | + 'padding-right-0-36px--1', |
| 32 | + ) |
| 33 | + }) |
| 34 | + |
| 35 | + it('should show clear button when value is not empty', () => { |
| 36 | + const { container } = render(<Input />) |
| 37 | + expect(container).toMatchSnapshot() |
| 38 | + fireEvent.change(container.querySelector('input')!, { |
| 39 | + target: { value: 'test' }, |
| 40 | + }) |
| 41 | + expect(container.querySelector('button')).toBeInTheDocument() |
| 42 | + }) |
| 43 | + |
| 44 | + it('should not show clear button when value is empty', () => { |
| 45 | + const { container } = render(<Input />) |
| 46 | + expect(container).toMatchSnapshot() |
| 47 | + }) |
| 48 | + |
| 49 | + it('should be able to clear value by clicking clear button', () => { |
| 50 | + const { container } = render(<Input allowClear />) |
| 51 | + fireEvent.change(container.querySelector('input')!, { |
| 52 | + target: { value: 'test' }, |
| 53 | + }) |
| 54 | + expect(container.querySelector('button')).toBeInTheDocument() |
| 55 | + fireEvent.click(container.querySelector('button')!) |
| 56 | + expect(container.querySelector('input')!.value).toBe('') |
| 57 | + }) |
| 58 | + |
| 59 | + it('should be able to render with icon', () => { |
| 60 | + const { container } = render( |
| 61 | + <Input icon={<GlassIcon data-testid="icon" />} />, |
| 62 | + ) |
| 63 | + expect(container.querySelector('[data-testid="icon"]')).toBeInTheDocument() |
| 64 | + }) |
| 65 | + |
| 66 | + it('should render error style when error is true', () => { |
| 67 | + const { container } = render(<Input error />) |
| 68 | + expect(container).toMatchSnapshot() |
| 69 | + expect(container.querySelector('[aria-label="input"]')).toHaveClass( |
| 70 | + 'border-color-0-var(--error,light-dark(#D52B2E,#FF5B5E))--1', |
| 71 | + ) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should be able to render with error message', () => { |
| 75 | + const { container } = render(<Input errorMessage="Error message" />) |
| 76 | + expect( |
| 77 | + container.querySelector('[aria-label="error-message"]'), |
| 78 | + ).toBeInTheDocument() |
| 79 | + }) |
| 80 | + |
| 81 | + it('should pass colors prop', () => { |
| 82 | + const { container } = render( |
| 83 | + <Input |
| 84 | + colors={{ |
| 85 | + primary: 'red', |
| 86 | + error: 'blue', |
| 87 | + text: 'green', |
| 88 | + }} |
| 89 | + />, |
| 90 | + ) |
| 91 | + const input = container.querySelector('[aria-label="input"]') |
| 92 | + expect(input).toHaveStyle({ |
| 93 | + '--primary': 'red', |
| 94 | + '--error': 'blue', |
| 95 | + '--text': 'green', |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + it('should have typography when typography is provided', () => { |
| 100 | + const { container } = render( |
| 101 | + <Input typography={'inlineLabelS' as keyof DevupThemeTypography} />, |
| 102 | + ) |
| 103 | + expect(container).toMatchSnapshot() |
| 104 | + expect(container.querySelector('input')).toHaveClass('typo-inlineLabelS') |
| 105 | + }) |
| 106 | + |
| 107 | + it('should pass className prop to error message component', () => { |
| 108 | + const { container } = render( |
| 109 | + <Input |
| 110 | + classNames={{ |
| 111 | + errorMessage: 'error-message', |
| 112 | + }} |
| 113 | + errorMessage="Error message" |
| 114 | + />, |
| 115 | + ) |
| 116 | + expect(container).toMatchSnapshot() |
| 117 | + expect(container.querySelector('[aria-label="error-message"]')).toHaveClass( |
| 118 | + 'error-message', |
| 119 | + ) |
| 120 | + }) |
| 121 | + |
| 122 | + it('should pass className prop to icon component', () => { |
| 123 | + const { container } = render( |
| 124 | + <Input |
| 125 | + classNames={{ |
| 126 | + icon: 'icon', |
| 127 | + }} |
| 128 | + icon={<GlassIcon />} |
| 129 | + />, |
| 130 | + ) |
| 131 | + expect(container).toMatchSnapshot() |
| 132 | + expect(container.querySelector('[aria-label="icon"]')).toHaveClass('icon') |
| 133 | + }) |
| 134 | + |
| 135 | + it('should pass props to ClearButton component', async () => { |
| 136 | + const { container } = render(<ClearButton />) |
| 137 | + expect(container).toMatchSnapshot() |
| 138 | + const clearButton = container.querySelector('[aria-label="clear-button"]') |
| 139 | + expect(clearButton).toBeInTheDocument() |
| 140 | + }) |
| 141 | + |
| 142 | + it('should render disabled icon style when disabled is true', () => { |
| 143 | + const { container } = render(<Input disabled icon={<GlassIcon />} />) |
| 144 | + expect(container).toMatchSnapshot() |
| 145 | + expect(container.querySelector('[aria-label="icon"]')).toHaveClass( |
| 146 | + 'color-0-var(--inputDisabledText,light-dark(#D6D7DE,#373737))--1', |
| 147 | + ) |
| 148 | + }) |
| 149 | + |
| 150 | + it('should call onChange prop when it is provided andvalue is changed', () => { |
| 151 | + const onChange = vi.fn() |
| 152 | + const { container } = render(<Input onChange={onChange} />) |
| 153 | + fireEvent.change(container.querySelector('input')!, { |
| 154 | + target: { value: 'test' }, |
| 155 | + }) |
| 156 | + expect(onChange).toHaveBeenCalledWith(expect.any(Object)) |
| 157 | + }) |
| 158 | +}) |
| 159 | + |
| 160 | +describe('Controlled Input', () => { |
| 161 | + it('should render with value', () => { |
| 162 | + const { container } = render(<Controlled />) |
| 163 | + expect(container).toMatchSnapshot() |
| 164 | + }) |
| 165 | + |
| 166 | + it('should update value when it is changed', () => { |
| 167 | + const { container } = render(<Controlled />) |
| 168 | + fireEvent.change(container.querySelector('input')!, { |
| 169 | + target: { value: 'test' }, |
| 170 | + }) |
| 171 | + expect(container.querySelector('input')!.value).toBe('test') |
| 172 | + }) |
| 173 | +}) |
0 commit comments