|
| 1 | +import {render} from './helpers/test-utils' |
| 2 | + |
| 3 | +test('it should work as expected', () => { |
| 4 | + const {queryByTestId} = render(` |
| 5 | + <select id="fruits" data-testid="select"> |
| 6 | + <option value="">Select a fruit...</option> |
| 7 | + <option value="ananas">Ananas</option> |
| 8 | + <option value="banana">Banana</option> |
| 9 | + <option value="avocado">Avocado</option> |
| 10 | + </select> |
| 11 | + `) |
| 12 | + |
| 13 | + expect(queryByTestId('select')).toHaveDisplayValue('Select a fruit...') |
| 14 | + expect(queryByTestId('select')).not.toHaveDisplayValue('Banana') |
| 15 | + expect(() => |
| 16 | + expect(queryByTestId('select')).not.toHaveDisplayValue('Select a fruit...'), |
| 17 | + ).toThrow() |
| 18 | + expect(() => |
| 19 | + expect(queryByTestId('select')).toHaveDisplayValue('Ananas'), |
| 20 | + ).toThrow() |
| 21 | + |
| 22 | + queryByTestId('select').value = 'banana' |
| 23 | + expect(queryByTestId('select')).toHaveDisplayValue('Banana') |
| 24 | +}) |
| 25 | + |
| 26 | +test('it should work with select multiple', () => { |
| 27 | + const {queryByTestId} = render(` |
| 28 | + <select id="fruits" data-testid="select" multiple> |
| 29 | + <option value="">Select a fruit...</option> |
| 30 | + <option value="ananas" selected>Ananas</option> |
| 31 | + <option value="banana">Banana</option> |
| 32 | + <option value="avocado" selected>Avocado</option> |
| 33 | + </select> |
| 34 | + `) |
| 35 | + |
| 36 | + expect(queryByTestId('select')).toHaveDisplayValue(['Ananas', 'Avocado']) |
| 37 | + expect(() => |
| 38 | + expect(queryByTestId('select')).not.toHaveDisplayValue([ |
| 39 | + 'Ananas', |
| 40 | + 'Avocado', |
| 41 | + ]), |
| 42 | + ).toThrow() |
| 43 | + |
| 44 | + expect(queryByTestId('select')).not.toHaveDisplayValue('Ananas') |
| 45 | + expect(() => |
| 46 | + expect(queryByTestId('select')).toHaveDisplayValue('Ananas'), |
| 47 | + ).toThrow() |
| 48 | + |
| 49 | + Array.from(queryByTestId('select').options).forEach(option => { |
| 50 | + option.selected = ['ananas', 'banana'].includes(option.value) |
| 51 | + }) |
| 52 | + |
| 53 | + expect(queryByTestId('select')).toHaveDisplayValue(['Ananas', 'Banana']) |
| 54 | +}) |
| 55 | + |
| 56 | +test('it should work with input elements', () => { |
| 57 | + const {queryByTestId} = render(` |
| 58 | + <input type="text" data-testid="input" value="Luca" /> |
| 59 | + `) |
| 60 | + |
| 61 | + expect(queryByTestId('input')).toHaveDisplayValue('Luca') |
| 62 | + |
| 63 | + queryByTestId('input').value = 'Piero' |
| 64 | + expect(queryByTestId('input')).toHaveDisplayValue('Piero') |
| 65 | +}) |
| 66 | + |
| 67 | +test('it should work with textarea elements', () => { |
| 68 | + const {queryByTestId} = render( |
| 69 | + '<textarea data-testid="textarea-example">An example description here.</textarea>', |
| 70 | + ) |
| 71 | + |
| 72 | + expect(queryByTestId('textarea-example')).toHaveDisplayValue( |
| 73 | + 'An example description here.', |
| 74 | + ) |
| 75 | + |
| 76 | + queryByTestId('textarea-example').value = 'Another example' |
| 77 | + expect(queryByTestId('textarea-example')).toHaveDisplayValue( |
| 78 | + 'Another example', |
| 79 | + ) |
| 80 | +}) |
| 81 | + |
| 82 | +test('it should throw if element is not valid', () => { |
| 83 | + const {queryByTestId} = render(` |
| 84 | + <div data-testid="div">Banana</div> |
| 85 | + <input type="radio" data-testid="radio" value="Something" /> |
| 86 | + <input type="checkbox" data-testid="checkbox" /> |
| 87 | + `) |
| 88 | + |
| 89 | + let errorMessage |
| 90 | + try { |
| 91 | + expect(queryByTestId('div')).toHaveDisplayValue('Banana') |
| 92 | + } catch (err) { |
| 93 | + errorMessage = err.message |
| 94 | + } |
| 95 | + |
| 96 | + expect(errorMessage).toMatchInlineSnapshot( |
| 97 | + `".toHaveDisplayValue() currently supports only input, textarea or select elements, try with another matcher instead."`, |
| 98 | + ) |
| 99 | + |
| 100 | + try { |
| 101 | + expect(queryByTestId('radio')).toHaveDisplayValue('Something') |
| 102 | + } catch (err) { |
| 103 | + errorMessage = err.message |
| 104 | + } |
| 105 | + |
| 106 | + expect(errorMessage).toMatchInlineSnapshot( |
| 107 | + `".toHaveDisplayValue() currently does not support input[type=\\"radio\\"], try with another matcher instead."`, |
| 108 | + ) |
| 109 | + |
| 110 | + try { |
| 111 | + expect(queryByTestId('checkbox')).toHaveDisplayValue(true) |
| 112 | + } catch (err) { |
| 113 | + errorMessage = err.message |
| 114 | + } |
| 115 | + |
| 116 | + expect(errorMessage).toMatchInlineSnapshot( |
| 117 | + `".toHaveDisplayValue() currently does not support input[type=\\"checkbox\\"], try with another matcher instead."`, |
| 118 | + ) |
| 119 | +}) |
0 commit comments