Skip to content

Commit 680cbee

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

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
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

+11-5
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 {
@@ -20,15 +21,13 @@ describe('runAfterTransition', () => {
2021
afterAll(() => {
2122
global.TransitionEvent = originalTransitionEvent;
2223
});
23-
24+
2425
beforeEach(() => {
25-
jest
26-
.spyOn(window, 'requestAnimationFrame')
27-
// @ts-expect-error -- mock
28-
.mockImplementation((cb) => cb());
26+
jest.useFakeTimers();
2927
});
3028

3129
afterEach(() => {
30+
jest.useRealTimers();
3231
jest.restoreAllMocks();
3332
cleanupElements();
3433
});
@@ -49,6 +48,7 @@ describe('runAfterTransition', () => {
4948
it('calls callback immediately when no transition is running', () => {
5049
const callback = jest.fn();
5150
runAfterTransition(callback);
51+
act(() => {jest.runOnlyPendingTimers();});
5252
expect(callback).toHaveBeenCalled();
5353
});
5454

@@ -64,7 +64,10 @@ describe('runAfterTransition', () => {
6464
})
6565
);
6666

67+
6768
runAfterTransition(callback);
69+
act(() => {jest.runOnlyPendingTimers();});
70+
6871
// Callback should not be called immediately since a transition is active.
6972
expect(callback).not.toHaveBeenCalled();
7073

@@ -91,7 +94,9 @@ describe('runAfterTransition', () => {
9194
);
9295

9396
runAfterTransition(callback1);
97+
act(() => {jest.runOnlyPendingTimers();});
9498
runAfterTransition(callback2);
99+
act(() => {jest.runOnlyPendingTimers();});
95100
// Callbacks should not be called during transition.
96101
expect(callback1).not.toHaveBeenCalled();
97102
expect(callback2).not.toHaveBeenCalled();
@@ -123,6 +128,7 @@ describe('runAfterTransition', () => {
123128
cleanupElements();
124129

125130
runAfterTransition(callback);
131+
act(() => {jest.runOnlyPendingTimers();});
126132

127133
expect(callback).toHaveBeenCalled();
128134
});

0 commit comments

Comments
 (0)