|
| 1 | +import { |
| 2 | + fireEvent, |
| 3 | + isInaccessible, |
| 4 | + queries, |
| 5 | + screen, |
| 6 | + waitFor, |
| 7 | + waitForElementToBeRemoved, |
| 8 | +} from '../index' |
| 9 | + |
| 10 | +const { |
| 11 | + getByText, |
| 12 | + queryByText, |
| 13 | + findByText, |
| 14 | + getAllByText, |
| 15 | + queryAllByText, |
| 16 | + findAllByText, |
| 17 | + queryAllByRole, |
| 18 | + queryByRole, |
| 19 | + findByRole, |
| 20 | +} = queries |
| 21 | + |
| 22 | +async function testQueries() { |
| 23 | + // element queries |
| 24 | + const element = document.createElement('div') |
| 25 | + getByText(element, 'foo') |
| 26 | + queryByText(element, 'foo') |
| 27 | + await findByText(element, 'foo') |
| 28 | + await findByText(element, 'foo', undefined, {timeout: 10}) |
| 29 | + getAllByText(element, 'bar') |
| 30 | + queryAllByText(element, 'bar') |
| 31 | + await findAllByText(element, 'bar') |
| 32 | + await findAllByText(element, 'bar', undefined, {timeout: 10}) |
| 33 | + |
| 34 | + // screen queries |
| 35 | + screen.getByText('foo') |
| 36 | + screen.queryByText('foo') |
| 37 | + await screen.findByText('foo') |
| 38 | + await screen.findByText('foo', undefined, {timeout: 10}) |
| 39 | + screen.debug(screen.getAllByText('bar')) |
| 40 | + screen.queryAllByText('bar') |
| 41 | + await screen.findAllByText('bar') |
| 42 | + await screen.findAllByText('bar', undefined, {timeout: 10}) |
| 43 | +} |
| 44 | + |
| 45 | +async function testByRole() { |
| 46 | + const element = document.createElement('button') |
| 47 | + element.setAttribute('aria-hidden', 'true') |
| 48 | + |
| 49 | + console.assert(queryByRole(element, 'button') === null) |
| 50 | + console.assert(queryByRole(element, 'button', {hidden: true}) !== null) |
| 51 | + |
| 52 | + console.assert(screen.queryByRole('button') === null) |
| 53 | + console.assert(screen.queryByRole('button', {hidden: true}) !== null) |
| 54 | + |
| 55 | + console.assert( |
| 56 | + (await findByRole(element, 'button', undefined, {timeout: 10})) === null, |
| 57 | + ) |
| 58 | + console.assert( |
| 59 | + (await findByRole(element, 'button', {hidden: true}, {timeout: 10})) !== |
| 60 | + null, |
| 61 | + ) |
| 62 | + |
| 63 | + console.assert( |
| 64 | + queryAllByRole(document.body, 'progressbar', {queryFallbacks: true}) |
| 65 | + .length === 1, |
| 66 | + ) |
| 67 | + |
| 68 | + // `name` option |
| 69 | + console.assert(queryByRole(element, 'button', {name: 'Logout'}) === null) |
| 70 | + console.assert(queryByRole(element, 'button', {name: /^Log/}) === null) |
| 71 | + console.assert( |
| 72 | + queryByRole(element, 'button', { |
| 73 | + name: (name, element) => |
| 74 | + name === 'Login' && element.hasAttribute('disabled'), |
| 75 | + }) === null, |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +function testA11yHelper() { |
| 80 | + const element = document.createElement('svg') |
| 81 | + console.assert(!isInaccessible(element)) |
| 82 | +} |
| 83 | + |
| 84 | +function eventTest() { |
| 85 | + fireEvent.popState(window, { |
| 86 | + location: 'http://www.example.com/?page=1', |
| 87 | + state: {page: 1}, |
| 88 | + }) |
| 89 | + |
| 90 | + // HTMLElement |
| 91 | + const element = document.createElement('div') |
| 92 | + fireEvent.click(getByText(element, 'foo')) |
| 93 | + |
| 94 | + // ChildNode |
| 95 | + const child = document.createElement('div') |
| 96 | + element.appendChild(child) |
| 97 | + if (!element.firstChild) { |
| 98 | + // Narrow Type |
| 99 | + throw new Error(`Can't find firstChild`) |
| 100 | + } |
| 101 | + fireEvent.click(element.firstChild) |
| 102 | +} |
| 103 | + |
| 104 | +async function testWaitFors() { |
| 105 | + const element = document.createElement('div') |
| 106 | + |
| 107 | + await waitFor(() => getByText(element, 'apple')) |
| 108 | + await waitFor(() => getAllByText(element, 'apple')) |
| 109 | + const result: HTMLSpanElement = await waitFor(() => |
| 110 | + getByText(element, 'apple'), |
| 111 | + ) |
| 112 | + if (!result) { |
| 113 | + // Use value |
| 114 | + throw new Error(`Can't find result`) |
| 115 | + } |
| 116 | + |
| 117 | + element.innerHTML = '<span>apple</span>' |
| 118 | + |
| 119 | + await waitForElementToBeRemoved(() => getByText(element, 'apple'), {interval: 3000, container: element, timeout: 5000}) |
| 120 | + await waitForElementToBeRemoved(getByText(element, 'apple')) |
| 121 | + await waitForElementToBeRemoved(getAllByText(element, 'apple')) |
| 122 | +} |
0 commit comments