Skip to content

Commit d1ca3a9

Browse files
committed
fix false positive log when running tests
Let's wrap the test in `act` to get rid of the warning. In practice (while testing in the browser) the actual warning doesn't seem to affect the user experience at all. The `act` function is typed in a strange way (`Promise<undefined> & void`). Yet the actual contents of the `act` callback is returned as expected. Therefore we overrode the type of `act` to make sure this reflects reality better. (Thanks @thecrypticace!) Also added an additional check to make sure the actual `container` is available to extra ensure we are not lying by overriding the type.
1 parent 569cec7 commit d1ca3a9

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

packages/@headlessui-react/src/components/transitions/transition.test.tsx

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import React, { Fragment, useState, useRef, useLayoutEffect, useEffect } from 'react'
2-
import { render, fireEvent } from '@testing-library/react'
2+
import { render, fireEvent, act as _act } from '@testing-library/react'
33

44
import { suppressConsoleLogs } from '../../test-utils/suppress-console-logs'
55
import { Transition } from './transition'
66

77
import { executeTimeline } from '../../test-utils/execute-timeline'
88

9+
let act = _act as unknown as <T>(fn: () => T) => PromiseLike<T>
10+
911
function nextFrame() {
1012
return new Promise<void>((resolve) => {
1113
requestAnimationFrame(() => {
@@ -423,22 +425,26 @@ describe('Setup API', () => {
423425
`)
424426
})
425427

426-
it('should be possible to passthrough the transition classes and immediately apply the enter transitions when appear is set to true', () => {
427-
let { container } = render(
428-
<Transition
429-
show={true}
430-
appear={true}
431-
enter="enter"
432-
enterFrom="enter-from"
433-
enterTo="enter-to"
434-
leave="leave"
435-
leaveFrom="leave-from"
436-
leaveTo="leave-to"
437-
>
438-
Children
439-
</Transition>
428+
it('should be possible to passthrough the transition classes and immediately apply the enter transitions when appear is set to true', async () => {
429+
let { container } = await act(() =>
430+
render(
431+
<Transition
432+
show={true}
433+
appear={true}
434+
enter="enter"
435+
enterFrom="enter-from"
436+
enterTo="enter-to"
437+
leave="leave"
438+
leaveFrom="leave-from"
439+
leaveTo="leave-to"
440+
>
441+
Children
442+
</Transition>
443+
)
440444
)
441445

446+
expect(container).toBeDefined()
447+
442448
expect(container.firstChild).toMatchInlineSnapshot(`
443449
<div
444450
class="enter enter-from"

0 commit comments

Comments
 (0)