|
1 | 1 | import { createPortal } from 'react-dom'
|
2 | 2 | import React, { createElement, useRef, useState, Fragment, useEffect, useCallback } from 'react'
|
3 |
| -import { render } from '@testing-library/react' |
| 3 | +import { render, screen } from '@testing-library/react' |
4 | 4 |
|
5 | 5 | import { Dialog } from './dialog'
|
6 | 6 | import { Popover } from '../popover/popover'
|
@@ -101,6 +101,98 @@ describe('Rendering', () => {
|
101 | 101 | })
|
102 | 102 | )
|
103 | 103 |
|
| 104 | + it( |
| 105 | + 'should be able to explicitly choose role=dialog', |
| 106 | + suppressConsoleLogs(async () => { |
| 107 | + function Example() { |
| 108 | + let [isOpen, setIsOpen] = useState(false) |
| 109 | + |
| 110 | + return ( |
| 111 | + <> |
| 112 | + <button id="trigger" onClick={() => setIsOpen(true)}> |
| 113 | + Trigger |
| 114 | + </button> |
| 115 | + <Dialog open={isOpen} onClose={setIsOpen} role="dialog"> |
| 116 | + <TabSentinel /> |
| 117 | + </Dialog> |
| 118 | + </> |
| 119 | + ) |
| 120 | + } |
| 121 | + render(<Example />) |
| 122 | + |
| 123 | + assertDialog({ state: DialogState.InvisibleUnmounted }) |
| 124 | + |
| 125 | + await click(document.getElementById('trigger')) |
| 126 | + |
| 127 | + await nextFrame() |
| 128 | + |
| 129 | + assertDialog({ state: DialogState.Visible, attributes: { role: 'dialog' } }) |
| 130 | + }) |
| 131 | + ) |
| 132 | + |
| 133 | + it( |
| 134 | + 'should be able to explicitly choose role=alertdialog', |
| 135 | + suppressConsoleLogs(async () => { |
| 136 | + function Example() { |
| 137 | + let [isOpen, setIsOpen] = useState(false) |
| 138 | + |
| 139 | + return ( |
| 140 | + <> |
| 141 | + <button id="trigger" onClick={() => setIsOpen(true)}> |
| 142 | + Trigger |
| 143 | + </button> |
| 144 | + <Dialog open={isOpen} onClose={setIsOpen} role="alertdialog"> |
| 145 | + <TabSentinel /> |
| 146 | + </Dialog> |
| 147 | + </> |
| 148 | + ) |
| 149 | + } |
| 150 | + render(<Example />) |
| 151 | + |
| 152 | + assertDialog({ state: DialogState.InvisibleUnmounted }) |
| 153 | + |
| 154 | + await click(document.getElementById('trigger')) |
| 155 | + |
| 156 | + await nextFrame() |
| 157 | + |
| 158 | + assertDialog({ state: DialogState.Visible, attributes: { role: 'alertdialog' } }) |
| 159 | + }) |
| 160 | + ) |
| 161 | + |
| 162 | + it( |
| 163 | + 'should fall back to role=dialog for an invalid role', |
| 164 | + suppressConsoleLogs(async () => { |
| 165 | + function Example() { |
| 166 | + let [isOpen, setIsOpen] = useState(false) |
| 167 | + |
| 168 | + return ( |
| 169 | + <> |
| 170 | + <button id="trigger" onClick={() => setIsOpen(true)}> |
| 171 | + Trigger |
| 172 | + </button> |
| 173 | + <Dialog |
| 174 | + open={isOpen} |
| 175 | + onClose={setIsOpen} |
| 176 | + // @ts-expect-error: We explicitly type role to only accept valid options — but we still want to verify runtime behaviorr |
| 177 | + role="foobar" |
| 178 | + > |
| 179 | + <TabSentinel /> |
| 180 | + </Dialog> |
| 181 | + </> |
| 182 | + ) |
| 183 | + } |
| 184 | + render(<Example />) |
| 185 | + |
| 186 | + assertDialog({ state: DialogState.InvisibleUnmounted }) |
| 187 | + |
| 188 | + await click(document.getElementById('trigger')) |
| 189 | + |
| 190 | + await nextFrame() |
| 191 | + |
| 192 | + assertDialog({ state: DialogState.Visible, attributes: { role: 'dialog' } }) |
| 193 | + }, 'warn') |
| 194 | + ) |
| 195 | + |
104 | 196 | it(
|
105 | 197 | 'should complain when an `open` prop is provided without an `onClose` prop',
|
106 | 198 | suppressConsoleLogs(async () => {
|
|
0 commit comments