|
| 1 | +import {render} from './helpers/test-utils' |
| 2 | + |
| 3 | +test('find asynchronously finds elements', async () => { |
| 4 | + const { |
| 5 | + findByLabelText, |
| 6 | + findAllByLabelText, |
| 7 | + |
| 8 | + findByPlaceholderText, |
| 9 | + findAllByPlaceholderText, |
| 10 | + |
| 11 | + findByText, |
| 12 | + findAllByText, |
| 13 | + |
| 14 | + findByAltText, |
| 15 | + findAllByAltText, |
| 16 | + |
| 17 | + findByTitle, |
| 18 | + findAllByTitle, |
| 19 | + |
| 20 | + findByDisplayValue, |
| 21 | + findAllByDisplayValue, |
| 22 | + |
| 23 | + findByRole, |
| 24 | + findAllByRole, |
| 25 | + |
| 26 | + findByTestId, |
| 27 | + findAllByTestId, |
| 28 | + } = render(` |
| 29 | + <div> |
| 30 | + <div data-testid="test-id" aria-label="test-label">test text content</div> |
| 31 | + <select><option>display value</option></select> |
| 32 | + <input placeholder="placeholder" /> |
| 33 | + <img alt="test alt text" src="/lucy-ricardo.png" /> |
| 34 | + <span title="test title" /> |
| 35 | + <div role="dialog"></div> |
| 36 | + </div> |
| 37 | + `) |
| 38 | + await expect(findByLabelText('test-label')).resolves.toBeTruthy() |
| 39 | + await expect(findAllByLabelText('test-label')).resolves.toHaveLength(1) |
| 40 | + |
| 41 | + await expect(findByPlaceholderText('placeholder')).resolves.toBeTruthy() |
| 42 | + await expect(findAllByPlaceholderText('placeholder')).resolves.toHaveLength(1) |
| 43 | + |
| 44 | + await expect(findByText('test text content')).resolves.toBeTruthy() |
| 45 | + await expect(findAllByText('test text content')).resolves.toHaveLength(1) |
| 46 | + |
| 47 | + await expect(findByAltText('test alt text')).resolves.toBeTruthy() |
| 48 | + await expect(findAllByAltText('test alt text')).resolves.toHaveLength(1) |
| 49 | + |
| 50 | + await expect(findByTitle('test title')).resolves.toBeTruthy() |
| 51 | + await expect(findAllByTitle('test title')).resolves.toHaveLength(1) |
| 52 | + |
| 53 | + await expect(findByDisplayValue('display value')).resolves.toBeTruthy() |
| 54 | + await expect(findAllByDisplayValue('display value')).resolves.toHaveLength(1) |
| 55 | + |
| 56 | + await expect(findByRole('dialog')).resolves.toBeTruthy() |
| 57 | + await expect(findAllByRole('dialog')).resolves.toHaveLength(1) |
| 58 | + |
| 59 | + await expect(findByTestId('test-id')).resolves.toBeTruthy() |
| 60 | + await expect(findAllByTestId('test-id')).resolves.toHaveLength(1) |
| 61 | +}) |
| 62 | + |
| 63 | +test('find rejects when something cannot be found', async () => { |
| 64 | + const { |
| 65 | + findByLabelText, |
| 66 | + findAllByLabelText, |
| 67 | + |
| 68 | + findByPlaceholderText, |
| 69 | + findAllByPlaceholderText, |
| 70 | + |
| 71 | + findByText, |
| 72 | + findAllByText, |
| 73 | + |
| 74 | + findByAltText, |
| 75 | + findAllByAltText, |
| 76 | + |
| 77 | + findByTitle, |
| 78 | + findAllByTitle, |
| 79 | + |
| 80 | + findByDisplayValue, |
| 81 | + findAllByDisplayValue, |
| 82 | + |
| 83 | + findByRole, |
| 84 | + findAllByRole, |
| 85 | + |
| 86 | + findByTestId, |
| 87 | + findAllByTestId, |
| 88 | + } = render(`<div />`) |
| 89 | + |
| 90 | + // I just don't want multiple lines for these. |
| 91 | + // qo = queryOptions |
| 92 | + // wo = waitForElementOptions |
| 93 | + const qo = {} // query options |
| 94 | + const wo = {timeout: 10} // wait options |
| 95 | + |
| 96 | + await expect(findByLabelText('x', qo, wo)).rejects.toThrow('x') |
| 97 | + await expect(findAllByLabelText('x', qo, wo)).rejects.toThrow('x') |
| 98 | + |
| 99 | + await expect(findByPlaceholderText('x', qo, wo)).rejects.toThrow('x') |
| 100 | + await expect(findAllByPlaceholderText('x', qo, wo)).rejects.toThrow('x') |
| 101 | + |
| 102 | + await expect(findByText('x', qo, wo)).rejects.toThrow('x') |
| 103 | + await expect(findAllByText('x', qo, wo)).rejects.toThrow('x') |
| 104 | + |
| 105 | + await expect(findByAltText('x', qo, wo)).rejects.toThrow('x') |
| 106 | + await expect(findAllByAltText('x', qo, wo)).rejects.toThrow('x') |
| 107 | + |
| 108 | + await expect(findByTitle('x', qo, wo)).rejects.toThrow('x') |
| 109 | + await expect(findAllByTitle('x', qo, wo)).rejects.toThrow('x') |
| 110 | + |
| 111 | + await expect(findByDisplayValue('x', qo, wo)).rejects.toThrow('x') |
| 112 | + await expect(findAllByDisplayValue('x', qo, wo)).rejects.toThrow('x') |
| 113 | + |
| 114 | + await expect(findByRole('x', qo, wo)).rejects.toThrow('x') |
| 115 | + await expect(findAllByRole('x', qo, wo)).rejects.toThrow('x') |
| 116 | + |
| 117 | + await expect(findByTestId('x', qo, wo)).rejects.toThrow('x') |
| 118 | + await expect(findAllByTestId('x', qo, wo)).rejects.toThrow('x') |
| 119 | +}) |
| 120 | + |
| 121 | +test('actually works with async code', async () => { |
| 122 | + const {findByTestId, container, rerender} = render(`<div />`) |
| 123 | + setTimeout(() => rerender(`<div data-testid="div">correct dom</div>`), 20) |
| 124 | + await expect(findByTestId('div', {}, {container})).resolves.toBeTruthy() |
| 125 | +}) |
0 commit comments