Skip to content

Commit

Permalink
chore(workspaces): clean-up store and add new unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gribnoysup committed Jun 25, 2024
1 parent 5280ce9 commit 4ede15a
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export const canReplaceTab = (tab: WorkspaceTab) => {
return resolveTabDestroyState('replace', tab);
};

const setTabDestroyHandler = (
/**
* Exported only for testing purposes
* @internal
*/
export const setTabDestroyHandler = (
type: 'close' | 'replace',
tabId: string,
handler: WorkspaceDestroyHandler
Expand Down
11 changes: 5 additions & 6 deletions packages/compass-workspaces/src/components/workspaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
getActiveTab,
getLocalAppRegistryForTab,
moveTab,
openFallbackTab,
openFallbackWorkspace,
openTabFromCurrent,
selectNextTab,
selectPrevTab,
Expand Down Expand Up @@ -132,8 +132,7 @@ type CompassWorkspacesProps = {
onCreateTab(defaultTab?: OpenWorkspaceOptions | null): void;
onCloseTab(at: number): void;
onNamespaceNotFound(
tab: WorkspaceTab,
connectionId: string,
tab: Extract<WorkspaceTab, { namespace: string }>,
fallbackNamespace: string | null
): void;
};
Expand Down Expand Up @@ -262,7 +261,7 @@ const CompassWorkspaces: React.FunctionComponent<CompassWorkspacesProps> = ({
<NamespaceProvider
namespace={activeTab.namespace}
onNamespaceFallbackSelect={(ns) => {
onNamespaceNotFound(activeTab, activeTab.connectionId, ns);
onNamespaceNotFound(activeTab, ns);
}}
>
<Component namespace={activeTab.namespace}></Component>
Expand All @@ -279,7 +278,7 @@ const CompassWorkspaces: React.FunctionComponent<CompassWorkspacesProps> = ({
<NamespaceProvider
namespace={activeTab.namespace}
onNamespaceFallbackSelect={(ns) => {
onNamespaceNotFound(activeTab, activeTab.connectionId, ns);
onNamespaceNotFound(activeTab, ns);
}}
>
<Component tabId={id} {...collectionMetadata}></Component>
Expand Down Expand Up @@ -360,6 +359,6 @@ export default connect(
onMoveTab: moveTab,
onCreateTab: openTabFromCurrent,
onCloseTab: closeTab,
onNamespaceNotFound: openFallbackTab,
onNamespaceNotFound: openFallbackWorkspace,
}
)(CompassWorkspaces);
65 changes: 38 additions & 27 deletions packages/compass-workspaces/src/stores/workspaces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TestMongoDBInstanceManager } from '@mongodb-js/compass-app-stores/provi
import { ConnectionsManager } from '@mongodb-js/compass-connections/provider';
import type { ConnectionInfo } from '../../../connection-info/dist';
import type { WorkspaceTab } from '../../dist';
import { setTabDestroyHandler } from '../components/workspace-close-handler';

describe('tabs behavior', function () {
const instance = {
Expand Down Expand Up @@ -72,7 +73,7 @@ describe('tabs behavior', function () {
databaseRemoved,
connectionDisconnected,
collectionSubtabSelected,
openFallbackTab,
openFallbackWorkspace: openFallbackTab,
getActiveTab,
} = workspacesSlice;

Expand All @@ -99,6 +100,37 @@ describe('tabs behavior', function () {
expect(state).to.have.property('activeTabId', state.tabs[0].id);
});

it('should open the workspace over existing tab by default', function () {
const store = configureStore();
for (const ns of ['foo', 'bar', 'buz', 'bla']) {
store.dispatch(
openWorkspace({
type: 'Collections',
namespace: ns,
connectionId: 'abc',
})
);
}
expect(store.getState()).to.have.property('tabs').have.lengthOf(1);
});

it('should open the workspace in new tab if any `replace` handlers returned `false`', function () {
const store = configureStore();
for (const ns of ['foo', 'bar', 'buz', 'bla']) {
store.dispatch(
openWorkspace({
type: 'Collections',
namespace: ns,
connectionId: 'abc',
})
);
setTabDestroyHandler('replace', store.getState().activeTabId!, () => {
return false;
});
}
expect(store.getState()).to.have.property('tabs').have.lengthOf(4);
});

it('when `newTab` is `true` should open a workspace in new tab even if another exists', function () {
const store = configureStore();
store.dispatch(openWorkspace({ type: 'My Queries' }));
Expand All @@ -110,23 +142,6 @@ describe('tabs behavior', function () {
expect(state).to.have.property('activeTabId', state.tabs[1].id);
});

it('when `newTab` is `false` should always open a workspace in the same tab', function () {});

it('should open workspace in the same tab if type is the same, but other workspace options are different', function () {
const store = configureStore();
openTabs(store);
store.dispatch(
openWorkspace({
type: 'Collection',
connectionId: '1',
namespace: 'test.bar',
})
);
const state = store.getState();
expect(state).to.have.property('tabs').have.lengthOf(3);
expect(state).to.have.nested.property('tabs[2].namespace', 'test.bar');
});

it('should select already opened tab when trying to open a new one with the same attributes', function () {
const store = configureStore();
openTabs(store);
Expand All @@ -140,6 +155,7 @@ describe('tabs behavior', function () {
connectionId: 'connection1',
})
);

expect(store.getState()).to.eq(currentState1);

// opening "My Queries" so that the current active workspace type is
Expand Down Expand Up @@ -406,10 +422,9 @@ describe('tabs behavior', function () {
type: 'Databases',
connectionId,
};
openTabs(store, {});
store.dispatch(openWorkspace(connectionTab));
store.dispatch(openWorkspace({ type: 'My Queries' }));
store.dispatch(openWorkspace(connectionTab)); // this is to make the first tab active
store.dispatch(openWorkspace({ type: 'My Queries' }, { newTab: true }));
store.dispatch(selectPrevTab());

expect(getActiveTab(store.getState())).to.have.property(
'type',
Expand Down Expand Up @@ -481,9 +496,7 @@ describe('tabs behavior', function () {
);

// Replace collection tab with collections list one
store.dispatch(
openFallbackTab(getActiveTab(store.getState())!, '1', 'foo')
);
store.dispatch(openFallbackTab(getActiveTab(store.getState())!, 'foo'));
expect(store.getState().tabs).to.have.lengthOf(1);
expect(getActiveTab(store.getState())).to.have.property(
'type',
Expand All @@ -495,9 +508,7 @@ describe('tabs behavior', function () {
);

// Replace collections list tab with the databases list
store.dispatch(
openFallbackTab(getActiveTab(store.getState())!, '1', null)
);
store.dispatch(openFallbackTab(getActiveTab(store.getState())!, null));
expect(store.getState().tabs).to.have.lengthOf(1);
expect(getActiveTab(store.getState())).to.have.property(
'type',
Expand Down
Loading

0 comments on commit 4ede15a

Please sign in to comment.