Skip to content

Commit c66f55b

Browse files
authored
fix(collection): hide edit view button in readonly mode COMPASS-7688 (#5945)
1 parent 6668493 commit c66f55b

File tree

2 files changed

+66
-23
lines changed

2 files changed

+66
-23
lines changed

packages/compass-collection/src/components/collection-header-actions/collection-header-actions.spec.tsx

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,38 @@ import {
66
WorkspacesServiceProvider,
77
type WorkspacesService,
88
} from '@mongodb-js/compass-workspaces/provider';
9-
import CollectionHeaderActions from '../collection-header-actions';
9+
import type { PreferencesAccess } from 'compass-preferences-model';
10+
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
11+
import { PreferencesProvider } from 'compass-preferences-model/provider';
1012

11-
function renderCollectionHeaderActions(
12-
props: Partial<ComponentProps<typeof CollectionHeaderActions>> = {},
13-
workspaceService: Partial<WorkspacesService> = {}
14-
) {
15-
return render(
16-
<WorkspacesServiceProvider value={workspaceService as WorkspacesService}>
17-
<CollectionHeaderActions
18-
namespace="test.test"
19-
isReadonly={false}
20-
{...props}
21-
/>
22-
</WorkspacesServiceProvider>
23-
);
24-
}
13+
import CollectionHeaderActions from '../collection-header-actions';
2514

2615
describe('CollectionHeaderActions [Component]', function () {
27-
let sandbox: sinon.SinonSandbox;
28-
beforeEach(function () {
29-
sandbox = sinon.createSandbox();
16+
let preferences: PreferencesAccess;
17+
beforeEach(async function () {
18+
preferences = await createSandboxFromDefaultPreferences();
3019
});
31-
this.afterEach(function () {
32-
sandbox.restore();
20+
afterEach(function () {
21+
sinon.restore();
3322
});
23+
24+
const renderCollectionHeaderActions = (
25+
props: Partial<ComponentProps<typeof CollectionHeaderActions>> = {},
26+
workspaceService: Partial<WorkspacesService> = {}
27+
) => {
28+
return render(
29+
<WorkspacesServiceProvider value={workspaceService as WorkspacesService}>
30+
<PreferencesProvider value={preferences}>
31+
<CollectionHeaderActions
32+
namespace="test.test"
33+
isReadonly={false}
34+
{...props}
35+
/>
36+
</PreferencesProvider>
37+
</WorkspacesServiceProvider>
38+
);
39+
};
40+
3441
context('when the collection is not readonly', function () {
3542
beforeEach(function () {
3643
renderCollectionHeaderActions({
@@ -52,10 +59,43 @@ describe('CollectionHeaderActions [Component]', function () {
5259
});
5360
});
5461

62+
context('Compass readonly mode', function () {
63+
it('does not render edit view buttons when in readonly mode', async function () {
64+
await preferences.savePreferences({ readOnly: true });
65+
66+
renderCollectionHeaderActions({
67+
isReadonly: true,
68+
namespace: 'db.coll2',
69+
sourceName: 'db.someSource',
70+
sourcePipeline: [{ $match: { a: 1 } }],
71+
});
72+
73+
expect(
74+
screen.queryByTestId('collection-header-actions-edit-button')
75+
).to.not.exist;
76+
expect(
77+
screen.queryByTestId('collection-header-actions-return-to-view-button')
78+
).to.not.exist;
79+
});
80+
81+
it('renders edit view buttons when not in readonly mode', function () {
82+
renderCollectionHeaderActions({
83+
isReadonly: true,
84+
namespace: 'db.coll2',
85+
sourceName: 'db.someSource',
86+
sourcePipeline: [{ $match: { a: 1 } }],
87+
});
88+
89+
expect(
90+
screen.getByTestId('collection-header-actions-edit-button')
91+
).to.be.visible;
92+
});
93+
});
94+
5595
context('when the collection is a view', function () {
5696
let openEditViewWorkspaceStub: sinon.SinonStub;
5797
beforeEach(function () {
58-
openEditViewWorkspaceStub = sandbox.stub();
98+
openEditViewWorkspaceStub = sinon.stub();
5999
renderCollectionHeaderActions(
60100
{
61101
isReadonly: true,
@@ -96,7 +136,7 @@ describe('CollectionHeaderActions [Component]', function () {
96136
context('when the collection is editing a view', function () {
97137
let openCollectionWorkspaceStub: sinon.SinonStub;
98138
beforeEach(function () {
99-
openCollectionWorkspaceStub = sandbox.stub();
139+
openCollectionWorkspaceStub = sinon.stub();
100140
renderCollectionHeaderActions(
101141
{
102142
isReadonly: false,

packages/compass-collection/src/components/collection-header-actions/collection-header-actions.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { useConnectionInfo } from '@mongodb-js/compass-connections/provider';
99
import { useOpenWorkspace } from '@mongodb-js/compass-workspaces/provider';
1010
import React from 'react';
11+
import { usePreference } from 'compass-preferences-model/provider';
1112

1213
const collectionHeaderActionsStyles = css({
1314
display: 'flex',
@@ -35,12 +36,14 @@ const CollectionHeaderActions: React.FunctionComponent<
3536
}: CollectionHeaderActionsProps) => {
3637
const { id: connectionId } = useConnectionInfo();
3738
const { openCollectionWorkspace, openEditViewWorkspace } = useOpenWorkspace();
39+
const preferencesReadOnly = usePreference('readOnly');
40+
3841
return (
3942
<div
4043
className={collectionHeaderActionsStyles}
4144
data-testid="collection-header-actions"
4245
>
43-
{isReadonly && sourceName && !editViewName && (
46+
{isReadonly && sourceName && !editViewName && !preferencesReadOnly && (
4447
<Button
4548
data-testid="collection-header-actions-edit-button"
4649
size={ButtonSize.Small}

0 commit comments

Comments
 (0)