@@ -5,10 +5,12 @@ import React, {
5
5
useState ,
6
6
Component ,
7
7
useEffect ,
8
- Fragment
8
+ Fragment ,
9
+ useId
9
10
} from 'preact/compat' ;
10
11
import { setupScratch , teardown } from '../../../test/_util/helpers' ;
11
12
import { setupRerender , act } from 'preact/test-utils' ;
13
+ import { expect } from 'chai' ;
12
14
13
15
/* eslint-disable react/jsx-boolean-value, react/display-name, prefer-arrow-callback */
14
16
@@ -212,6 +214,38 @@ describe('Portal', () => {
212
214
expect ( scratch . firstChild . firstChild . childNodes . length ) . to . equal ( 0 ) ;
213
215
} ) ;
214
216
217
+ it ( 'should have unique ids for each portal' , ( ) => {
218
+ let root = document . createElement ( 'div' ) ;
219
+ let dialog = document . createElement ( 'div' ) ;
220
+ dialog . id = 'container' ;
221
+
222
+ scratch . appendChild ( root ) ;
223
+ scratch . appendChild ( dialog ) ;
224
+
225
+ function Id ( ) {
226
+ const id = useId ( ) ;
227
+ return id ;
228
+ }
229
+
230
+ function Dialog ( ) {
231
+ return < Id /> ;
232
+ }
233
+
234
+ function App ( ) {
235
+ return (
236
+ < div >
237
+ < Id />
238
+ { createPortal ( < Dialog /> , dialog ) }
239
+ </ div >
240
+ ) ;
241
+ }
242
+
243
+ render ( < App /> , root ) ;
244
+ expect ( scratch . innerHTML ) . to . equal (
245
+ '<div><div>P0-0</div></div><div id="container">P0-1</div>'
246
+ ) ;
247
+ } ) ;
248
+
215
249
it ( 'should unmount Portal' , ( ) => {
216
250
let root = document . createElement ( 'div' ) ;
217
251
let dialog = document . createElement ( 'div' ) ;
@@ -237,8 +271,8 @@ describe('Portal', () => {
237
271
it ( 'should leave a working root after the portal' , ( ) => {
238
272
/** @type {() => void } */
239
273
let toggle ,
240
- /** @type {() => void } */
241
- toggle2 ;
274
+ /** @type {() => void } */
275
+ toggle2 ;
242
276
243
277
function Foo ( props ) {
244
278
const [ mounted , setMounted ] = useState ( false ) ;
@@ -289,8 +323,8 @@ describe('Portal', () => {
289
323
it ( 'should work with stacking portals' , ( ) => {
290
324
/** @type {() => void } */
291
325
let toggle ,
292
- /** @type {() => void } */
293
- toggle2 ;
326
+ /** @type {() => void } */
327
+ toggle2 ;
294
328
295
329
function Foo ( props ) {
296
330
const [ mounted , setMounted ] = useState ( false ) ;
@@ -377,8 +411,8 @@ describe('Portal', () => {
377
411
it ( 'should work with replacing placeholder portals' , ( ) => {
378
412
/** @type {() => void } */
379
413
let toggle ,
380
- /** @type {() => void } */
381
- toggle2 ;
414
+ /** @type {() => void } */
415
+ toggle2 ;
382
416
383
417
function Foo ( props ) {
384
418
const [ mounted , setMounted ] = useState ( false ) ;
@@ -463,8 +497,9 @@ describe('Portal', () => {
463
497
it ( 'should support nested portals' , ( ) => {
464
498
/** @type {() => void } */
465
499
let toggle ,
466
- /** @type {() => void } */
467
- toggle2 , inner ;
500
+ /** @type {() => void } */
501
+ toggle2 ,
502
+ inner ;
468
503
469
504
function Bar ( ) {
470
505
const [ mounted , setMounted ] = useState ( false ) ;
0 commit comments