|
| 1 | +import {render} from './helpers/test-utils' |
| 2 | + |
| 3 | +describe('.toHaveValue', () => { |
| 4 | + test('handles value of text input', () => { |
| 5 | + const {queryByTestId} = render(` |
| 6 | + <input type="text" value="foo" data-testid="value" /> |
| 7 | + <input type="text" value="" data-testid="empty" /> |
| 8 | + <input type="text" data-testid="without" /> |
| 9 | + `) |
| 10 | + |
| 11 | + expect(queryByTestId('value')).toHaveValue('foo') |
| 12 | + expect(queryByTestId('value')).toHaveValue() |
| 13 | + expect(queryByTestId('value')).not.toHaveValue('bar') |
| 14 | + expect(queryByTestId('value')).not.toHaveValue('') |
| 15 | + |
| 16 | + expect(queryByTestId('empty')).toHaveValue('') |
| 17 | + expect(queryByTestId('empty')).not.toHaveValue() |
| 18 | + expect(queryByTestId('empty')).not.toHaveValue('foo') |
| 19 | + |
| 20 | + expect(queryByTestId('without')).toHaveValue('') |
| 21 | + expect(queryByTestId('without')).not.toHaveValue() |
| 22 | + expect(queryByTestId('without')).not.toHaveValue('foo') |
| 23 | + queryByTestId('without').value = 'bar' |
| 24 | + expect(queryByTestId('without')).toHaveValue('bar') |
| 25 | + }) |
| 26 | + |
| 27 | + test('handles value of number input', () => { |
| 28 | + const {queryByTestId} = render(` |
| 29 | + <input type="number" value="5" data-testid="number" /> |
| 30 | + <input type="number" value="" data-testid="empty" /> |
| 31 | + <input type="number" data-testid="without" /> |
| 32 | + `) |
| 33 | + |
| 34 | + expect(queryByTestId('number')).toHaveValue(5) |
| 35 | + expect(queryByTestId('number')).toHaveValue() |
| 36 | + expect(queryByTestId('number')).not.toHaveValue(4) |
| 37 | + expect(queryByTestId('number')).not.toHaveValue('5') |
| 38 | + |
| 39 | + expect(queryByTestId('empty')).toHaveValue(null) |
| 40 | + expect(queryByTestId('empty')).not.toHaveValue() |
| 41 | + expect(queryByTestId('empty')).not.toHaveValue('5') |
| 42 | + |
| 43 | + expect(queryByTestId('without')).toHaveValue(null) |
| 44 | + expect(queryByTestId('without')).not.toHaveValue() |
| 45 | + expect(queryByTestId('without')).not.toHaveValue('10') |
| 46 | + queryByTestId('without').value = 10 |
| 47 | + expect(queryByTestId('without')).toHaveValue(10) |
| 48 | + }) |
| 49 | + |
| 50 | + test('handles value of select element', () => { |
| 51 | + const {queryByTestId} = render(` |
| 52 | + <select data-testid="single"> |
| 53 | + <option value="first">First Value</option> |
| 54 | + <option value="second" selected>Second Value</option> |
| 55 | + <option value="third">Third Value</option> |
| 56 | + </select> |
| 57 | + |
| 58 | + <select data-testid="multiple" multiple> |
| 59 | + <option value="first">First Value</option> |
| 60 | + <option value="second" selected>Second Value</option> |
| 61 | + <option value="third" selected>Third Value</option> |
| 62 | + </select> |
| 63 | + |
| 64 | + <select data-testid="not-selected" > |
| 65 | + <option value="" disabled selected>- Select some value - </option> |
| 66 | + <option value="first">First Value</option> |
| 67 | + <option value="second">Second Value</option> |
| 68 | + <option value="third">Third Value</option> |
| 69 | + </select> |
| 70 | + `) |
| 71 | + |
| 72 | + expect(queryByTestId('single')).toHaveValue('second') |
| 73 | + expect(queryByTestId('single')).toHaveValue() |
| 74 | + |
| 75 | + expect(queryByTestId('multiple')).toHaveValue(['second', 'third']) |
| 76 | + expect(queryByTestId('multiple')).toHaveValue() |
| 77 | + |
| 78 | + expect(queryByTestId('not-selected')).not.toHaveValue() |
| 79 | + expect(queryByTestId('not-selected')).toHaveValue('') |
| 80 | + |
| 81 | + queryByTestId('single').children[0].setAttribute('selected', true) |
| 82 | + expect(queryByTestId('single')).toHaveValue('first') |
| 83 | + }) |
| 84 | + |
| 85 | + test('handles value of textarea element', () => { |
| 86 | + const {queryByTestId} = render(` |
| 87 | + <textarea data-testid="textarea">text value</textarea> |
| 88 | + `) |
| 89 | + expect(queryByTestId('textarea')).toHaveValue('text value') |
| 90 | + }) |
| 91 | + |
| 92 | + test('throws when passed checkbox or radio', () => { |
| 93 | + const {queryByTestId} = render(` |
| 94 | + <input data-testid="checkbox" type="checkbox" name="checkbox" value="val" checked /> |
| 95 | + <input data-testid="radio" type="radio" name="radio" value="val" checked /> |
| 96 | + `) |
| 97 | + |
| 98 | + expect(() => { |
| 99 | + expect(queryByTestId('checkbox')).toHaveValue('') |
| 100 | + }).toThrow() |
| 101 | + |
| 102 | + expect(() => { |
| 103 | + expect(queryByTestId('radio')).toHaveValue('') |
| 104 | + }).toThrow() |
| 105 | + }) |
| 106 | +}) |
0 commit comments