From 50c4eb2c421a525cfb49a1c46285b0b7a51ea5de Mon Sep 17 00:00:00 2001 From: Karl Sander Date: Thu, 20 Jul 2023 11:03:16 +0200 Subject: [PATCH 1/2] replace series function used to queue async callbacks --- packages/native/src/useLinking.tsx | 32 ++++-------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/packages/native/src/useLinking.tsx b/packages/native/src/useLinking.tsx index e07ccdd5e8..e4952da40c 100644 --- a/packages/native/src/useLinking.tsx +++ b/packages/native/src/useLinking.tsx @@ -60,35 +60,11 @@ const findMatchingState = ( /** * Run async function in series as it's called. */ -const series = (cb: () => Promise) => { - // Whether we're currently handling a callback - let handling = false; - let queue: (() => Promise)[] = []; - - const callback = async () => { - try { - if (handling) { - // If we're currently handling a previous event, wait before handling this one - // Add the callback to the beginning of the queue - queue.unshift(callback); - return; - } - - handling = true; - - await cb(); - } finally { - handling = false; - - if (queue.length) { - // If we have queued items, handle the last one - const last = queue.pop(); - - last?.(); - } - } +export const series = (cb: () => Promise) => { + let queue = Promise.resolve(); + const callback = () => { + queue = queue.then(cb); }; - return callback; }; From 82acbeed7edcffabc31b03e277da9c19745d4622 Mon Sep 17 00:00:00 2001 From: Karl Sander Date: Tue, 29 Aug 2023 11:50:30 +0200 Subject: [PATCH 2/2] update tests --- .../__tests__/NavigationContainer.test.tsx | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/packages/native/src/__tests__/NavigationContainer.test.tsx b/packages/native/src/__tests__/NavigationContainer.test.tsx index 5ab3387914..eea7b90e08 100644 --- a/packages/native/src/__tests__/NavigationContainer.test.tsx +++ b/packages/native/src/__tests__/NavigationContainer.test.tsx @@ -6,7 +6,7 @@ import { TabRouter, useNavigationBuilder, } from '@react-navigation/core'; -import { act, render } from '@testing-library/react-native'; +import { act, render, waitFor } from '@testing-library/react-native'; import * as React from 'react'; import { window } from '../__mocks__/window'; @@ -18,9 +18,7 @@ Object.assign(global, window); // eslint-disable-next-line import/extensions jest.mock('../useLinking', () => require('../useLinking.tsx')); -it('integrates with the history API', () => { - jest.useFakeTimers(); - +it('integrates with the history API', async () => { const createStackNavigator = createNavigatorFactory((props: any) => { const { state, descriptors, NavigationContent } = useNavigationBuilder( StackRouter, @@ -104,51 +102,44 @@ it('integrates with the history API', () => { act(() => navigation.current?.navigate('Profile', { user: 'jane' })); - expect(window.location.pathname).toBe('/jane'); + await waitFor(() => expect(window.location.pathname).toBe('/jane')); act(() => navigation.current?.navigate('Updates')); - expect(window.location.pathname).toBe('/updates'); + await waitFor(() => expect(window.location.pathname).toBe('/updates')); act(() => navigation.current?.goBack()); - jest.runAllTimers(); - - expect(window.location.pathname).toBe('/jane'); + await waitFor(() => expect(window.location.pathname).toBe('/jane')); act(() => { window.history.back(); - jest.runAllTimers(); }); - expect(window.location.pathname).toBe('/feed'); + await waitFor(() => expect(window.location.pathname).toBe('/feed')); act(() => { window.history.forward(); - jest.runAllTimers(); }); - expect(window.location.pathname).toBe('/jane'); + await waitFor(() => expect(window.location.pathname).toBe('/jane')); act(() => navigation.current?.navigate('Settings')); - expect(window.location.pathname).toBe('/edit'); + await waitFor(() => expect(window.location.pathname).toBe('/edit')); act(() => { window.history.go(-2); - jest.runAllTimers(); }); - expect(window.location.pathname).toBe('/feed'); + await waitFor(() => expect(window.location.pathname).toBe('/feed')); act(() => navigation.current?.navigate('Settings')); act(() => navigation.current?.navigate('Chat')); - expect(window.location.pathname).toBe('/chat'); + await waitFor(() => expect(window.location.pathname).toBe('/chat')); act(() => navigation.current?.navigate('Home')); - jest.runAllTimers(); - - expect(window.location.pathname).toBe('/edit'); + await waitFor(() => expect(window.location.pathname).toBe('/edit')); });