Skip to content
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

Prevent activation of previous workspace when launching Connect via deep link to a different cluster #50063

Merged
merged 23 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
51c37e9
Refactor `setActiveWorkspace` to async/await
gzdunek Dec 10, 2024
f43b0ae
Keep all the logic that restores a single workspace state in `getWork…
gzdunek Dec 10, 2024
dc70bdf
Separate restoring state from setting active workspace
gzdunek Dec 10, 2024
a6bb953
Allow the dialog to reopen documents to be closed without any decision
gzdunek Dec 11, 2024
e941385
Add a test that verifies if the correct workspace is activated
gzdunek Dec 11, 2024
9fc2003
Docs improvements
gzdunek Dec 13, 2024
cc2644a
Return early if there's no restoredWorkspace
gzdunek Dec 13, 2024
3dde323
Fix logger name
gzdunek Dec 13, 2024
8c4dbec
Improve test name
gzdunek Dec 13, 2024
8776788
Make restored state immutable
gzdunek Dec 16, 2024
f5be332
Fix comment
gzdunek Dec 17, 2024
35194aa
Do not wait for functions to be called in tests
gzdunek Dec 17, 2024
706b0f0
Add tests for discarding documents reopen dialog
gzdunek Dec 17, 2024
4bb34e3
Move setting `isInitialized` to a separate method
gzdunek Dec 17, 2024
23f82b8
Remove restored workspace when logging out so that we won't try to re…
gzdunek Dec 17, 2024
26c1dac
Do not try to restore previous documents if the user already opened n…
gzdunek Dec 17, 2024
9e810c1
Do not close dialog in test
gzdunek Dec 20, 2024
0753223
Improve `isInitialized` comment
gzdunek Dec 20, 2024
e275bbb
Call `setActiveWorkspace` again when we fail to change the workspace
gzdunek Dec 20, 2024
e339d09
Fix the logic around restoring previous documents
gzdunek Dec 20, 2024
c20ec38
Try to reopen documents also after `cluster-connect` is canceled
gzdunek Dec 30, 2024
a9bec27
canRestoreDocuments -> hasDocumentsToReopen
gzdunek Dec 30, 2024
aef61d0
Disallow parallel `setActiveWorkspace` calls (#50384)
gzdunek Dec 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 97 additions & 9 deletions web/packages/teleterm/src/ui/AppInitializer/AppInitializer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import 'jest-canvas-mock';
import { render } from 'design/utils/testing';
import { screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { mockIntersectionObserver } from 'jsdom-testing-mocks';

import { MockAppContext } from 'teleterm/ui/fixtures/mocks';
import { makeRootCluster } from 'teleterm/services/tshd/testHelpers';
Expand All @@ -29,9 +30,11 @@ import { VnetContextProvider } from 'teleterm/ui/Vnet';
import Logger, { NullService } from 'teleterm/logger';
import { MockedUnaryCall } from 'teleterm/services/tshd/cloneableClient';
import { ResourcesContextProvider } from 'teleterm/ui/DocumentCluster/resourcesContext';
import { IAppContext } from 'teleterm/ui/types';

import { AppInitializer } from './AppInitializer';

mockIntersectionObserver();
beforeAll(() => {
Logger.init(new NullService());
});
Expand Down Expand Up @@ -148,12 +151,97 @@ test('activating a workspace via deep link overrides the previously active works
).toBeVisible();
});

//TODO(gzdunek): Replace with Promise.withResolvers after upgrading to Node.js 22.
function withPromiseResolver() {
let resolver: () => void;
const promise = new Promise<void>(resolve => (resolver = resolve));
return {
resolve: resolver,
promise,
};
}
test.each<{
name: string;
action(appContext: IAppContext): Promise<void>;
expectDocumentsRestoredOrDiscarded: boolean;
}>([
{
name: 'closing documents reopen dialog via close button discards previous documents',
action: async () => {
await userEvent.click(await screen.findByTitle('Close'));
},
expectDocumentsRestoredOrDiscarded: true,
},
{
name: 'starting new session in document reopen dialog discards previous documents',
action: async () => {
await userEvent.click(
await screen.findByRole('button', { name: 'Start New Session' })
);
},
expectDocumentsRestoredOrDiscarded: true,
},
{
name: 'overwriting document reopen dialog with another regular dialog does not discard documents',
action: async appContext => {
act(() => {
const { closeDialog } = appContext.modalsService.openRegularDialog({
kind: 'change-access-request-kind',
onConfirm() {},
onCancel() {},
});
closeDialog();
gzdunek marked this conversation as resolved.
Show resolved Hide resolved
});
},
expectDocumentsRestoredOrDiscarded: false,
},
])('$name', async testCase => {
const rootCluster = makeRootCluster();
const appContext = new MockAppContext();
jest
.spyOn(appContext.statePersistenceService, 'getWorkspacesState')
.mockReturnValue({
rootClusterUri: rootCluster.uri,
workspaces: {
[rootCluster.uri]: {
localClusterUri: rootCluster.uri,
documents: [
{
kind: 'doc.access_requests',
uri: '/docs/123',
state: 'browsing',
clusterUri: rootCluster.uri,
requestId: '',
title: 'Access Requests',
},
],
location: undefined,
},
},
});
appContext.mainProcessClient.configService.set(
'usageReporting.enabled',
false
);
jest.spyOn(appContext.tshd, 'listRootClusters').mockReturnValue(
new MockedUnaryCall({
clusters: [rootCluster],
})
);

render(
<MockAppContextProvider appContext={appContext}>
<ConnectionsContextProvider>
<VnetContextProvider>
<ResourcesContextProvider>
<AppInitializer />
</ResourcesContextProvider>
</VnetContextProvider>
</ConnectionsContextProvider>
</MockAppContextProvider>
);

expect(
await screen.findByText(
'Do you want to reopen tabs from the previous session?'
)
).toBeInTheDocument();

await testCase.action(appContext);

expect(
appContext.workspacesService.getWorkspace(rootCluster.uri)
.documentsRestoredOrDiscarded
).toBe(testCase.expectDocumentsRestoredOrDiscarded);
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function DocumentsReopen(props: {
<ButtonIcon
type="button"
onClick={props.onDiscard}
title="Close"
color="text.slightlyMuted"
>
<Cross size="medium" />
Expand Down