Skip to content

Commit c23b7d7

Browse files
committed
Consolidate helper functions in test file
1 parent eda194e commit c23b7d7

File tree

1 file changed

+47
-74
lines changed

1 file changed

+47
-74
lines changed

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

Lines changed: 47 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import Sinon from 'sinon';
2828
function renderCollectionHeader(
2929
props: Partial<ComponentProps<typeof CollectionHeader>> = {},
3030
workspaceService: Partial<WorkspacesService> = {},
31-
stateOverrides: any = {}
31+
stateOverrides: Record<string, unknown> = {},
32+
connectionInfo?: ConnectionInfo
3233
) {
3334
const defaultState = {
3435
mockDataGenerator: {
@@ -40,7 +41,11 @@ function renderCollectionHeader(
4041

4142
const mockStore = createStore(() => defaultState);
4243

43-
return renderWithConnections(
44+
const renderMethod = connectionInfo
45+
? renderWithActiveConnection
46+
: renderWithConnections;
47+
48+
return renderMethod(
4449
<Provider store={mockStore}>
4550
<WorkspacesServiceProvider value={workspaceService as WorkspacesService}>
4651
<CollectionHeader
@@ -53,16 +58,17 @@ function renderCollectionHeader(
5358
{...props}
5459
/>
5560
</WorkspacesServiceProvider>
56-
</Provider>
61+
</Provider>,
62+
connectionInfo
5763
);
5864
}
5965

6066
describe('CollectionHeader [Component]', function () {
6167
afterEach(cleanup);
6268

6369
context('when the collection is not readonly', function () {
64-
beforeEach(function () {
65-
renderCollectionHeader();
70+
beforeEach(async function () {
71+
await renderCollectionHeader();
6672
});
6773

6874
it('renders the correct root classname', function () {
@@ -91,8 +97,11 @@ describe('CollectionHeader [Component]', function () {
9197
});
9298

9399
context('when the collection is readonly', function () {
94-
beforeEach(function () {
95-
renderCollectionHeader({ isReadonly: true, sourceName: 'orig.coll' });
100+
beforeEach(async function () {
101+
await renderCollectionHeader({
102+
isReadonly: true,
103+
sourceName: 'orig.coll',
104+
});
96105
});
97106

98107
afterEach(cleanup);
@@ -115,8 +124,8 @@ describe('CollectionHeader [Component]', function () {
115124
});
116125

117126
context('when the collection is readonly but not a view', function () {
118-
beforeEach(function () {
119-
renderCollectionHeader({ isReadonly: true, sourceName: undefined });
127+
beforeEach(async function () {
128+
await renderCollectionHeader({ isReadonly: true, sourceName: undefined });
120129
});
121130

122131
it('renders the readonly badge', function () {
@@ -129,8 +138,8 @@ describe('CollectionHeader [Component]', function () {
129138
});
130139

131140
context('when the collection is a time-series collection', function () {
132-
beforeEach(function () {
133-
renderCollectionHeader({ isTimeSeries: true });
141+
beforeEach(async function () {
142+
await renderCollectionHeader({ isTimeSeries: true });
134143
});
135144

136145
it('does not render the readonly badge', function () {
@@ -143,8 +152,8 @@ describe('CollectionHeader [Component]', function () {
143152
});
144153

145154
context('when the collection is a clustered collection', function () {
146-
beforeEach(function () {
147-
renderCollectionHeader({ isClustered: true });
155+
beforeEach(async function () {
156+
await renderCollectionHeader({ isClustered: true });
148157
});
149158

150159
it('does not render the readonly badge', function () {
@@ -161,8 +170,8 @@ describe('CollectionHeader [Component]', function () {
161170
});
162171

163172
context('when the collection is a fle collection', function () {
164-
beforeEach(function () {
165-
renderCollectionHeader({ isFLE: true });
173+
beforeEach(async function () {
174+
await renderCollectionHeader({ isFLE: true });
166175
});
167176

168177
it('renders the fle badge', function () {
@@ -171,8 +180,8 @@ describe('CollectionHeader [Component]', function () {
171180
});
172181

173182
describe('insights', function () {
174-
it('should show an insight when $text is used in the pipeline source', function () {
175-
renderCollectionHeader({
183+
it('should show an insight when $text is used in the pipeline source', async function () {
184+
await renderCollectionHeader({
176185
sourcePipeline: [{ $match: { $text: {} } }],
177186
});
178187
expect(screen.getByTestId('insight-badge-button')).to.exist;
@@ -181,8 +190,8 @@ describe('CollectionHeader [Component]', function () {
181190
.exist;
182191
});
183192

184-
it('should show an insight when $regex is used in the pipeline source', function () {
185-
renderCollectionHeader({
193+
it('should show an insight when $regex is used in the pipeline source', async function () {
194+
await renderCollectionHeader({
186195
sourcePipeline: [{ $match: { $regex: {} } }],
187196
});
188197
expect(screen.getByTestId('insight-badge-button')).to.exist;
@@ -191,8 +200,8 @@ describe('CollectionHeader [Component]', function () {
191200
.exist;
192201
});
193202

194-
it('should show an insight when $lookup is used in the pipeline source', function () {
195-
renderCollectionHeader({
203+
it('should show an insight when $lookup is used in the pipeline source', async function () {
204+
await renderCollectionHeader({
196205
sourcePipeline: [{ $lookup: {} }],
197206
});
198207
expect(screen.getByTestId('insight-badge-button')).to.exist;
@@ -211,7 +220,7 @@ describe('CollectionHeader [Component]', function () {
211220
});
212221

213222
function assertBreadcrumbText(items: string[]) {
214-
const crumbs: any[] = [];
223+
const crumbs: (string | null)[] = [];
215224
screen.getByTestId('breadcrumbs').childNodes.forEach((item) => {
216225
crumbs.push(item.textContent);
217226
});
@@ -232,22 +241,22 @@ describe('CollectionHeader [Component]', function () {
232241
}
233242

234243
context('renders correclty', function () {
235-
it('for a collection', function () {
236-
renderCollectionHeader({ namespace: 'db.coll1' });
244+
it('for a collection', async function () {
245+
await renderCollectionHeader({ namespace: 'db.coll1' });
237246
assertBreadcrumbText(['db', 'coll1']);
238247
});
239248

240-
it('for a view', function () {
241-
renderCollectionHeader({
249+
it('for a view', async function () {
250+
await renderCollectionHeader({
242251
namespace: 'db.coll1',
243252
sourceName: 'db.coll2',
244253
});
245254
// For view: connection-db-sourceCollectionName-viewName
246255
assertBreadcrumbText(['db', 'coll2', 'coll1']);
247256
});
248257

249-
it('for a view when its being edited', function () {
250-
renderCollectionHeader({
258+
it('for a view when its being edited', async function () {
259+
await renderCollectionHeader({
251260
namespace: 'db.coll3',
252261
editViewName: 'db.coll1',
253262
});
@@ -257,10 +266,10 @@ describe('CollectionHeader [Component]', function () {
257266
});
258267

259268
context('calls onClick correclty', function () {
260-
it('for a collection', function () {
269+
it('for a collection', async function () {
261270
const openCollectionsWorkspaceStub = sandbox.stub();
262271
const openCollectionWorkspaceStub = sandbox.stub();
263-
renderCollectionHeader(
272+
await renderCollectionHeader(
264273
{ namespace: 'db.coll1' },
265274
{
266275
openCollectionsWorkspace: openCollectionsWorkspaceStub,
@@ -281,9 +290,9 @@ describe('CollectionHeader [Component]', function () {
281290
]);
282291
});
283292

284-
it('for a view, opens source collection', function () {
293+
it('for a view, opens source collection', async function () {
285294
const openCollectionWorkspaceStub = sandbox.stub();
286-
renderCollectionHeader(
295+
await renderCollectionHeader(
287296
{ namespace: 'db.coll1', sourceName: 'db.coll2' },
288297
{
289298
openCollectionWorkspace: openCollectionWorkspaceStub,
@@ -360,44 +369,8 @@ describe('CollectionHeader [Component]', function () {
360369
},
361370
};
362371

363-
function renderCollectionHeaderWithSchemaAnalysis(
364-
props: Partial<ComponentProps<typeof CollectionHeader>> = {},
365-
workspaceService: Partial<WorkspacesService> = {},
366-
stateOverrides: any = {},
367-
connectionInfo?: ConnectionInfo
368-
) {
369-
const defaultState = {
370-
mockDataGenerator: {
371-
isModalOpen: false,
372-
currentStep: MockDataGeneratorStep.SCHEMA_CONFIRMATION,
373-
},
374-
...stateOverrides,
375-
};
376-
377-
const mockStore = createStore(() => defaultState);
378-
379-
return renderWithActiveConnection(
380-
<Provider store={mockStore}>
381-
<WorkspacesServiceProvider
382-
value={workspaceService as WorkspacesService}
383-
>
384-
<CollectionHeader
385-
isAtlas={false}
386-
isReadonly={false}
387-
isTimeSeries={false}
388-
isClustered={false}
389-
isFLE={false}
390-
namespace="test.test"
391-
{...props}
392-
/>
393-
</WorkspacesServiceProvider>
394-
</Provider>,
395-
connectionInfo
396-
);
397-
}
398-
399372
it('should show Mock Data Generator button when all conditions are met', async function () {
400-
await renderCollectionHeaderWithSchemaAnalysis(
373+
await renderCollectionHeader(
401374
{
402375
isAtlas: true, // Atlas environment
403376
isReadonly: false, // Not readonly
@@ -426,7 +399,7 @@ describe('CollectionHeader [Component]', function () {
426399
});
427400

428401
it('should disable Mock Data Generator button when collection has no schema analysis data', async function () {
429-
await renderCollectionHeaderWithSchemaAnalysis(
402+
await renderCollectionHeader(
430403
{
431404
isAtlas: true,
432405
isReadonly: false,
@@ -453,7 +426,7 @@ describe('CollectionHeader [Component]', function () {
453426
});
454427

455428
it('should disable Mock Data Generator button for collections with excessive nesting depth', async function () {
456-
await renderCollectionHeaderWithSchemaAnalysis(
429+
await renderCollectionHeader(
457430
{
458431
isAtlas: true,
459432
isReadonly: false,
@@ -482,7 +455,7 @@ describe('CollectionHeader [Component]', function () {
482455
});
483456

484457
it('should not show Mock Data Generator button for readonly collections (views)', async function () {
485-
await renderCollectionHeaderWithSchemaAnalysis(
458+
await renderCollectionHeader(
486459
{
487460
isAtlas: true,
488461
isReadonly: true, // Readonly (view)
@@ -509,7 +482,7 @@ describe('CollectionHeader [Component]', function () {
509482
});
510483

511484
it('should not show Mock Data Generator button in non-Atlas environments', async function () {
512-
await renderCollectionHeaderWithSchemaAnalysis(
485+
await renderCollectionHeader(
513486
{
514487
isAtlas: false, // Not Atlas
515488
isReadonly: false,
@@ -536,7 +509,7 @@ describe('CollectionHeader [Component]', function () {
536509
});
537510

538511
it('should not show Mock Data Generator button when schema analysis has not run', async function () {
539-
await renderCollectionHeaderWithSchemaAnalysis(
512+
await renderCollectionHeader(
540513
{
541514
isAtlas: true,
542515
isReadonly: false,

0 commit comments

Comments
 (0)