Skip to content

Commit 56f6f82

Browse files
committed
fix: add renderWithQueryBar hook
1 parent c828f28 commit 56f6f82

File tree

4 files changed

+119
-123
lines changed

4 files changed

+119
-123
lines changed

packages/compass-crud/src/components/crud-toolbar.spec.tsx

Lines changed: 34 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
import React from 'react';
22
import { expect } from 'chai';
33
import sinon from 'sinon';
4-
import {
5-
render,
6-
screen,
7-
cleanup,
8-
within,
9-
userEvent,
10-
} from '@mongodb-js/testing-library-compass';
4+
import { screen, within, userEvent } from '@mongodb-js/testing-library-compass';
115
import type { PreferencesAccess } from 'compass-preferences-model';
126
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
137
import { CrudToolbar } from './crud-toolbar';
14-
import { PreferencesProvider } from 'compass-preferences-model/provider';
15-
import QueryBarPlugin from '@mongodb-js/compass-query-bar';
16-
import {
17-
compassFavoriteQueryStorageAccess,
18-
compassRecentQueryStorageAccess,
19-
} from '@mongodb-js/my-queries-storage';
8+
import { renderWithQueryBar } from '../../test/render-with-query-bar';
209

2110
const noop = () => {
2211
/* noop */
@@ -25,21 +14,6 @@ const noop = () => {
2514
const testOutdatedMessageId = 'crud-outdated-message-id';
2615
const testErrorMessageId = 'document-list-error-summary';
2716

28-
const MockQueryBarPlugin = QueryBarPlugin.withMockServices({
29-
dataService: {
30-
sample() {
31-
return Promise.resolve([]);
32-
},
33-
getConnectionString() {
34-
return { hosts: [] } as any;
35-
},
36-
},
37-
instance: { on() {}, removeListener() {} } as any,
38-
favoriteQueryStorageAccess: compassFavoriteQueryStorageAccess,
39-
recentQueryStorageAccess: compassRecentQueryStorageAccess,
40-
atlasAiService: {} as any,
41-
});
42-
4317
const addDataText = 'Add Data';
4418
const updateDataText = 'Update';
4519
const deleteDataText = 'Delete';
@@ -50,50 +24,41 @@ describe('CrudToolbar Component', function () {
5024
function renderCrudToolbar(
5125
props?: Partial<React.ComponentProps<typeof CrudToolbar>>
5226
) {
53-
const queryBarProps = {};
54-
55-
return render(
56-
<PreferencesProvider value={preferences}>
57-
<MockQueryBarPlugin {...(queryBarProps as any)}>
58-
<CrudToolbar
59-
activeDocumentView="List"
60-
count={55}
61-
end={20}
62-
getPage={noop}
63-
insertDataHandler={noop}
64-
loadingCount={false}
65-
isFetching={false}
66-
docsPerPage={25}
67-
isWritable
68-
instanceDescription=""
69-
onApplyClicked={noop}
70-
onResetClicked={noop}
71-
onUpdateButtonClicked={noop}
72-
onDeleteButtonClicked={noop}
73-
onExpandAllClicked={noop}
74-
onCollapseAllClicked={noop}
75-
openExportFileDialog={noop}
76-
outdated={false}
77-
page={0}
78-
readonly={false}
79-
refreshDocuments={noop}
80-
resultId="123"
81-
start={0}
82-
viewSwitchHandler={noop}
83-
updateMaxDocumentsPerPage={noop}
84-
queryLimit={0}
85-
querySkip={0}
86-
{...props}
87-
/>
88-
</MockQueryBarPlugin>
89-
</PreferencesProvider>
27+
return renderWithQueryBar(
28+
<CrudToolbar
29+
activeDocumentView="List"
30+
count={55}
31+
end={20}
32+
getPage={noop}
33+
insertDataHandler={noop}
34+
loadingCount={false}
35+
isFetching={false}
36+
docsPerPage={25}
37+
isWritable
38+
instanceDescription=""
39+
onApplyClicked={noop}
40+
onResetClicked={noop}
41+
onUpdateButtonClicked={noop}
42+
onDeleteButtonClicked={noop}
43+
onExpandAllClicked={noop}
44+
onCollapseAllClicked={noop}
45+
openExportFileDialog={noop}
46+
outdated={false}
47+
page={0}
48+
readonly={false}
49+
refreshDocuments={noop}
50+
resultId="123"
51+
start={0}
52+
viewSwitchHandler={noop}
53+
updateMaxDocumentsPerPage={noop}
54+
queryLimit={0}
55+
querySkip={0}
56+
{...props}
57+
/>,
58+
{ preferences }
9059
);
9160
}
9261

93-
afterEach(function () {
94-
cleanup();
95-
});
96-
9762
beforeEach(async function () {
9863
preferences = await createSandboxFromDefaultPreferences();
9964
});
@@ -732,7 +697,6 @@ describe('CrudToolbar Component', function () {
732697

733698
const contextMenu = screen.getByTestId('context-menu');
734699
expect(within(contextMenu).queryByText('Bulk update')).to.not.exist;
735-
expect(within(contextMenu).queryByText('Bulk delete')).to.not.exist;
736700
});
737701

738702
it('should not show bulk operations when query has skip', function () {

packages/compass-crud/src/components/document-list-view-item.spec.tsx

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11
import React from 'react';
2-
import { render, screen, userEvent } from '@mongodb-js/testing-library-compass';
2+
import { screen, userEvent } from '@mongodb-js/testing-library-compass';
33
import { expect } from 'chai';
44
import sinon from 'sinon';
55
import HadronDocument from 'hadron-document';
66
import type { PreferencesAccess } from 'compass-preferences-model';
77
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
8-
import { PreferencesProvider } from 'compass-preferences-model/provider';
9-
import QueryBarPlugin from '@mongodb-js/compass-query-bar';
10-
import {
11-
compassFavoriteQueryStorageAccess,
12-
compassRecentQueryStorageAccess,
13-
} from '@mongodb-js/my-queries-storage';
8+
import { renderWithQueryBar } from '../../test/render-with-query-bar';
149
import { DocumentListViewItem } from './document-list-view-item';
1510

16-
const MockQueryBarPlugin = QueryBarPlugin.withMockServices({
17-
dataService: {
18-
sample() {
19-
return Promise.resolve([]);
20-
},
21-
getConnectionString() {
22-
return { hosts: [] } as any;
23-
},
24-
},
25-
instance: { on() {}, removeListener() {} } as any,
26-
favoriteQueryStorageAccess: compassFavoriteQueryStorageAccess,
27-
recentQueryStorageAccess: compassRecentQueryStorageAccess,
28-
atlasAiService: {} as any,
29-
});
30-
3111
describe('DocumentListViewItem', function () {
3212
let doc: HadronDocument;
3313
let copyToClipboardStub: sinon.SinonStub;
@@ -37,22 +17,19 @@ describe('DocumentListViewItem', function () {
3717
function renderDocumentListViewItem(
3818
props?: Partial<React.ComponentProps<typeof DocumentListViewItem>>
3919
) {
40-
const queryBarProps = {};
41-
42-
return render(
43-
<PreferencesProvider value={preferences}>
44-
<MockQueryBarPlugin {...(queryBarProps as any)}>
45-
<DocumentListViewItem
46-
doc={doc}
47-
docRef={null}
48-
docIndex={0}
49-
isEditable={true}
50-
copyToClipboard={copyToClipboardStub}
51-
openInsertDocumentDialog={openInsertDocumentDialogStub}
52-
{...props}
53-
/>
54-
</MockQueryBarPlugin>
55-
</PreferencesProvider>
20+
return renderWithQueryBar(
21+
<DocumentListViewItem
22+
doc={doc}
23+
docRef={null}
24+
docIndex={0}
25+
isEditable={true}
26+
copyToClipboard={copyToClipboardStub}
27+
openInsertDocumentDialog={openInsertDocumentDialogStub}
28+
{...props}
29+
/>,
30+
{
31+
preferences,
32+
}
5633
);
5734
}
5835

packages/compass-crud/src/components/virtualized-document-list-view.spec.tsx

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import React from 'react';
22
import { expect } from 'chai';
33
import HadronDocument from 'hadron-document';
44
import {
5-
render,
65
screen,
7-
cleanup,
86
within,
97
act,
108
userEvent,
119
} from '@mongodb-js/testing-library-compass';
1210
import { type VirtualListRef } from '@mongodb-js/compass-components';
11+
import type { PreferencesAccess } from 'compass-preferences-model';
12+
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
13+
import { renderWithQueryBar } from '../../test/render-with-query-bar';
1314

1415
import VirtualizedDocumentListView from './virtualized-document-list-view';
1516

@@ -36,12 +37,17 @@ const getDocs = () => [
3637
];
3738

3839
describe('VirtualizedDocumentListView', function () {
39-
afterEach(function () {
40-
cleanup();
40+
let preferences: PreferencesAccess;
41+
42+
beforeEach(async function () {
43+
preferences = await createSandboxFromDefaultPreferences();
4144
});
4245

4346
it('renders the list of provided BSON objects', function () {
44-
render(<VirtualizedDocumentListView docs={getDocs()} isEditable={false} />);
47+
renderWithQueryBar(
48+
<VirtualizedDocumentListView docs={getDocs()} isEditable={false} />,
49+
{ preferences }
50+
);
4551

4652
expect(screen.getByTitle('1')).to.be.visible;
4753
expect(screen.getByTitle('Doc1')).to.be.visible;
@@ -50,11 +56,12 @@ describe('VirtualizedDocumentListView', function () {
5056
});
5157

5258
it('renders the list of provided HadronDocuments', function () {
53-
render(
59+
renderWithQueryBar(
5460
<VirtualizedDocumentListView
5561
docs={getDocs().map((doc) => new HadronDocument(doc))}
5662
isEditable={false}
57-
/>
63+
/>,
64+
{ preferences }
5865
);
5966

6067
expect(screen.getByTitle('1')).to.be.visible;
@@ -64,22 +71,24 @@ describe('VirtualizedDocumentListView', function () {
6471
});
6572

6673
it('renders a readonly list when isEditable is false', function () {
67-
render(
74+
renderWithQueryBar(
6875
<VirtualizedDocumentListView
6976
docs={getDocs().map((doc) => new HadronDocument(doc))}
7077
isEditable={false}
71-
/>
78+
/>,
79+
{ preferences }
7280
);
7381

7482
expect(screen.getAllByTestId('readonly-document')).to.have.lengthOf(2);
7583
});
7684

7785
it('renders an editable list when isEditable is true', function () {
78-
render(
86+
renderWithQueryBar(
7987
<VirtualizedDocumentListView
8088
docs={getDocs().map((doc) => new HadronDocument(doc))}
8189
isEditable={true}
82-
/>
90+
/>,
91+
{ preferences }
8392
);
8493

8594
expect(screen.getAllByTestId('editable-document')).to.have.lengthOf(2);
@@ -91,14 +100,16 @@ describe('VirtualizedDocumentListView', function () {
91100
(_, idx) => new HadronDocument(createBigDocument(idx))
92101
);
93102
const listRef: VirtualListRef = React.createRef();
94-
render(
103+
104+
renderWithQueryBar(
95105
<VirtualizedDocumentListView
96106
docs={bigDocuments}
97107
isEditable={true}
98108
listRef={listRef}
99109
__TEST_OVERSCAN_COUNT={0}
100110
__TEST_LIST_HEIGHT={178}
101-
/>
111+
/>,
112+
{ preferences }
102113
);
103114

104115
const firstDocument = bigDocuments[0];
@@ -152,12 +163,13 @@ describe('VirtualizedDocumentListView', function () {
152163
});
153164

154165
it('discards the state of document when the underlying document changes', function () {
155-
const { rerender } = render(
166+
const { rerender } = renderWithQueryBar(
156167
<VirtualizedDocumentListView
157168
docs={[new HadronDocument(createBigDocument(1))]}
158169
isEditable={true}
159170
__TEST_LIST_HEIGHT={178}
160-
/>
171+
/>,
172+
{ preferences }
161173
);
162174

163175
let [documentElement] = screen.getAllByTestId('editable-document');
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { PropsWithChildren } from 'react';
2+
import { render } from '@mongodb-js/testing-library-compass';
3+
import type { PreferencesAccess } from 'compass-preferences-model';
4+
import { PreferencesProvider } from 'compass-preferences-model/provider';
5+
import QueryBarPlugin from '@mongodb-js/compass-query-bar';
6+
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
7+
import {
8+
compassFavoriteQueryStorageAccess,
9+
compassRecentQueryStorageAccess,
10+
} from '@mongodb-js/my-queries-storage';
11+
12+
export const MockQueryBarPlugin: typeof QueryBarPlugin =
13+
QueryBarPlugin.withMockServices({
14+
dataService: {
15+
sample() {
16+
return Promise.resolve([]);
17+
},
18+
getConnectionString() {
19+
return { hosts: [] } as any;
20+
},
21+
},
22+
instance: { on() {}, removeListener() {} } as any,
23+
favoriteQueryStorageAccess: compassFavoriteQueryStorageAccess,
24+
recentQueryStorageAccess: compassRecentQueryStorageAccess,
25+
atlasAiService: {} as any,
26+
});
27+
28+
export const renderWithQueryBar = (
29+
component: React.ReactElement,
30+
{ preferences }: { preferences: PreferencesAccess }
31+
) => {
32+
const queryBarProps = {};
33+
34+
return render(component, {
35+
wrapper: ({ children }: PropsWithChildren<unknown>) => (
36+
<PreferencesProvider value={preferences}>
37+
<MockQueryBarPlugin {...(queryBarProps as any)}>
38+
{children}
39+
</MockQueryBarPlugin>
40+
</PreferencesProvider>
41+
),
42+
});
43+
};

0 commit comments

Comments
 (0)