Skip to content

Commit 6685edb

Browse files
committed
converts to isConnected and uses non-mock RAF for tests
1 parent f07a118 commit 6685edb

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

packages/@react-aria/utils/src/runAfterTransition.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ if (typeof document !== 'undefined') {
9898
*/
9999
function cleanupDetachedElements() {
100100
for (const [element] of transitionsByElement) {
101-
if (element instanceof HTMLElement && !document.contains(element)) {
101+
if (element instanceof HTMLElement && !element.isConnected) {
102102
transitionsByElement.delete(element);
103103
}
104104
}

packages/@react-aria/utils/test/runAfterTransition.test.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {act} from '@testing-library/react';
12
import {runAfterTransition} from '../src/runAfterTransition';
23

34
class MockTransitionEvent extends Event {
@@ -22,10 +23,7 @@ describe('runAfterTransition', () => {
2223
});
2324

2425
beforeEach(() => {
25-
jest
26-
.spyOn(window, 'requestAnimationFrame')
27-
// @ts-expect-error -- mock
28-
.mockImplementation((cb) => cb());
26+
jest.useFakeTimers();
2927
});
3028

3129
afterEach(() => {
@@ -49,6 +47,7 @@ describe('runAfterTransition', () => {
4947
it('calls callback immediately when no transition is running', () => {
5048
const callback = jest.fn();
5149
runAfterTransition(callback);
50+
act(() => {jest.runOnlyPendingTimers();});
5251
expect(callback).toHaveBeenCalled();
5352
});
5453

@@ -64,7 +63,10 @@ describe('runAfterTransition', () => {
6463
})
6564
);
6665

66+
6767
runAfterTransition(callback);
68+
act(() => {jest.runOnlyPendingTimers();});
69+
6870
// Callback should not be called immediately since a transition is active.
6971
expect(callback).not.toHaveBeenCalled();
7072

@@ -91,7 +93,9 @@ describe('runAfterTransition', () => {
9193
);
9294

9395
runAfterTransition(callback1);
96+
act(() => {jest.runOnlyPendingTimers();});
9497
runAfterTransition(callback2);
98+
act(() => {jest.runOnlyPendingTimers();});
9599
// Callbacks should not be called during transition.
96100
expect(callback1).not.toHaveBeenCalled();
97101
expect(callback2).not.toHaveBeenCalled();
@@ -123,6 +127,7 @@ describe('runAfterTransition', () => {
123127
cleanupElements();
124128

125129
runAfterTransition(callback);
130+
act(() => {jest.runOnlyPendingTimers();});
126131

127132
expect(callback).toHaveBeenCalled();
128133
});

0 commit comments

Comments
 (0)