Repository navigation
refactor(exchange): use core-react sync hooks for the server connection #2213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
21e4ca0
test(exchange): keep the server connection when leaving mid-sync
gmaclennan dc22913
fix(exchange): keep server connection when leaving screen mid-sync
gmaclennan afac571
refactor(exchange): drop the intermediate variable for the disconnect…
gmaclennan ceac969
refactor(exchange): use core-react sync hooks for the server connection
gmaclennan 6d0fa77
test(exchange): cover connecting servers on focus and disconnecting o…
gmaclennan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
src/frontend/screens/Exchange/ExchangeScreenContent.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import * as React from 'react'; | ||
| import type {SyncState} from '@comapeo/core-react'; | ||
| import {NavigationContainer} from '@react-navigation/native'; | ||
| import {createNativeStackNavigator} from '@react-navigation/native-stack'; | ||
| import {QueryClient, QueryClientProvider} from '@tanstack/react-query'; | ||
| import {render, screen} from '@testing-library/react-native'; | ||
| import {IntlProvider} from 'react-intl'; | ||
|
|
||
| import {ExchangeScreenContent} from './ExchangeScreenContent'; | ||
|
|
||
| let mockDataSyncProgress: number | null = null; | ||
|
|
||
| const mockSync = { | ||
| connectServers: jest.fn(), | ||
| disconnectServers: jest.fn(), | ||
| setAutostopDataSyncTimeout: jest.fn(), | ||
| stop: jest.fn(), | ||
| }; | ||
|
|
||
| jest.mock('@comapeo/core-react', () => ({ | ||
| ...jest.requireActual('@comapeo/core-react'), | ||
| useDataSyncProgress: () => mockDataSyncProgress, | ||
| useIsArchiveDevice: () => ({data: false}), | ||
| useStartSync: () => ({mutate: jest.fn()}), | ||
| })); | ||
|
|
||
| jest.mock('../../contexts/ActiveProjectContext', () => ({ | ||
| useActiveProject: () => ({ | ||
| projectId: 'project-id', | ||
| projectApi: {$sync: mockSync}, | ||
| }), | ||
| })); | ||
|
|
||
| jest.mock('../../hooks/server/projects', () => ({ | ||
| useActiveArchiveServer: () => undefined, | ||
| })); | ||
|
|
||
| jest.mock('../../hooks/useLocalDiscoveryState', () => ({ | ||
| useLocalDiscoveryState: () => 'CoMapeo Test Wi-Fi', | ||
| })); | ||
|
|
||
| const serverPeerSyncState = { | ||
| initial: {isSyncEnabled: true, want: 0, wanted: 0}, | ||
| data: {isSyncEnabled: true, want: 5, wanted: 0}, | ||
| }; | ||
|
|
||
| const syncingWithServer = { | ||
| initial: {isSyncEnabled: true}, | ||
| data: {isSyncEnabled: true}, | ||
| remoteDeviceSyncState: {server: serverPeerSyncState}, | ||
| } satisfies SyncState; | ||
|
|
||
| const Stack = createNativeStackNavigator<{Sync: undefined}>(); | ||
|
|
||
| const queryClient = new QueryClient(); | ||
|
|
||
| const Wrapper = ({children}: {children: React.ReactNode}) => ( | ||
| <QueryClientProvider client={queryClient}> | ||
| <IntlProvider locale="en">{children}</IntlProvider> | ||
| </QueryClientProvider> | ||
| ); | ||
|
|
||
| async function renderExchangeScreen(syncState: SyncState) { | ||
| const {unmount} = await render( | ||
| <React.Suspense fallback={null}> | ||
| <NavigationContainer> | ||
| <Stack.Navigator> | ||
| <Stack.Screen name="Sync"> | ||
| {() => <ExchangeScreenContent syncState={syncState} />} | ||
| </Stack.Screen> | ||
| </Stack.Navigator> | ||
| </NavigationContainer> | ||
| </React.Suspense>, | ||
| {wrapper: Wrapper}, | ||
| ); | ||
| return unmount; | ||
| } | ||
|
|
||
| describe('ExchangeScreenContent server connection on leaving the screen', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test('keeps the server connected while data sync is still in progress', async () => { | ||
| mockDataSyncProgress = 0.4; | ||
| const unmount = await renderExchangeScreen(syncingWithServer); | ||
|
|
||
| await expect(screen.findByText('Exchanging...')).resolves.toBeVisible(); | ||
| expect(mockSync.connectServers).toHaveBeenCalled(); | ||
|
|
||
| await unmount(); | ||
|
|
||
| expect(mockSync.disconnectServers).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('disconnects the server once data sync is complete', async () => { | ||
| mockDataSyncProgress = 1; | ||
| const unmount = await renderExchangeScreen(syncingWithServer); | ||
|
|
||
| await expect(screen.findByText('Complete!')).resolves.toBeVisible(); | ||
|
|
||
| await unmount(); | ||
|
|
||
| expect(mockSync.disconnectServers).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('disconnects the server when data sync is not enabled', async () => { | ||
| mockDataSyncProgress = null; | ||
| const unmount = await renderExchangeScreen({ | ||
| ...syncingWithServer, | ||
| data: {isSyncEnabled: false}, | ||
| }); | ||
|
|
||
| await expect(screen.findByText('Devices found.')).resolves.toBeVisible(); | ||
|
|
||
| await unmount(); | ||
|
|
||
| expect(mockSync.disconnectServers).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this variable, or could we just use
shouldDisconnectServersRef.current, and avoid the useEffect to update it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Half of it. The variable is gone (afac571), but the effect has to stay: writing
ref.currentduring render trips thereact-hooks/refsrule in this repo's eslint config ("Cannot update ref during render"), so the only lint-clean way to keep the latest value readable from the focus cleanup is an effect. It now runs without a deps array and assigns directly, which is as small as I could make it.