Skip to content

Commit 8b1df5a

Browse files
committed
better tests
1 parent 5c42e6b commit 8b1df5a

File tree

2 files changed

+130
-48
lines changed

2 files changed

+130
-48
lines changed
Lines changed: 116 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,140 @@
11
import React from 'react';
22
import { expect } from 'chai';
3-
import { render, screen } from '@mongodb-js/testing-library-compass';
3+
import { screen, waitFor } from '@mongodb-js/testing-library-compass';
4+
import AnalysisProgressStatus from './analysis-progress-status';
5+
import { renderWithStore, testConnections } from '../../test/setup-store';
46
import {
5-
AnalysisProgressStatus,
6-
type AnalysisProgressStatusProps,
7-
} from './analysis-progress-status';
8-
import Sinon from 'sinon';
7+
AnalysisProcessActionTypes,
8+
startAnalysis,
9+
} from '../store/analysis-process';
10+
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
911

1012
describe('AnalysisProgressStatus', () => {
11-
const renderAnalysisProgressStatus = (
12-
options: Partial<AnalysisProgressStatusProps>
13-
) => {
14-
const props: AnalysisProgressStatusProps = {
15-
step: 'IDLE',
16-
sampledCollections: 0,
17-
analyzedCollections: 0,
18-
collectionRelationsInferred: 0,
19-
totalCollections: 0,
20-
onCancelClick: () => {},
21-
...options,
22-
};
23-
return render(<AnalysisProgressStatus {...props} />);
24-
};
25-
26-
it('Allows cancellation', () => {
27-
const onCancel = Sinon.spy();
28-
renderAnalysisProgressStatus({
29-
step: 'SAMPLING',
30-
sampledCollections: 2,
31-
totalCollections: 5,
32-
onCancelClick: onCancel,
13+
async function renderAnalysisProgressStatus({
14+
automaticallyInferRelations = false,
15+
} = {}) {
16+
const preferences = await createSandboxFromDefaultPreferences();
17+
const { store } = renderWithStore(<AnalysisProgressStatus />, {
18+
services: {
19+
preferences,
20+
},
3321
});
22+
store.dispatch(
23+
startAnalysis(
24+
'My Diagram',
25+
testConnections[0].id,
26+
'testDB',
27+
['coll1', 'coll2', 'coll3'],
28+
{ automaticallyInferRelations }
29+
)
30+
);
31+
return store;
32+
}
33+
34+
it('Allows cancellation', async () => {
35+
const store = await renderAnalysisProgressStatus();
36+
expect(screen.getByText('Sampling collections..')).to.be.visible;
3437
expect(screen.getByText('Cancel')).to.be.visible;
3538
screen.getByText('Cancel').click();
36-
expect(onCancel).to.have.been.calledOnce;
39+
await waitFor(() => {
40+
expect(store.getState().analysisProgress.step).to.equal('IDLE');
41+
expect(store.getState().diagram).to.be.null;
42+
});
3743
});
3844

3945
describe('Keeps showing progress along the way', () => {
40-
it('Sampling', () => {
41-
renderAnalysisProgressStatus({
42-
step: 'SAMPLING',
43-
sampledCollections: 2,
44-
totalCollections: 5,
46+
it('Without relationship inferring', async () => {
47+
const store = await renderAnalysisProgressStatus({
48+
automaticallyInferRelations: false,
4549
});
4650
expect(screen.getByText('Sampling collections..')).to.be.visible;
47-
expect(screen.getByText('2/5')).to.be.visible;
48-
});
51+
expect(screen.getByText('0/3')).to.be.visible;
52+
53+
// 2 out of 3 samples fetched, 1 analyzed
54+
store.dispatch({
55+
type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED,
56+
});
57+
store.dispatch({
58+
type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED,
59+
});
60+
store.dispatch({
61+
type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED,
62+
});
63+
64+
expect(screen.getByText('Sampling collections..')).to.be.visible;
65+
expect(screen.getByText('2/3')).to.be.visible;
4966

50-
it('Analyzing', () => {
51-
renderAnalysisProgressStatus({
52-
step: 'ANALYZING_SCHEMA',
53-
analyzedCollections: 1,
54-
totalCollections: 5,
67+
// Last sample fetched
68+
store.dispatch({
69+
type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED,
5570
});
71+
5672
expect(screen.getByText('Analyzing collection schemas..')).to.be.visible;
57-
expect(screen.getByText('1/5')).to.be.visible;
73+
expect(screen.getByText('1/3')).to.be.visible;
74+
75+
// Finish analyzing
76+
store.dispatch({
77+
type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED,
78+
});
79+
store.dispatch({
80+
type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED,
81+
});
82+
83+
expect(
84+
screen.queryByText('Inferring relationships between collections..')
85+
).not.to.exist;
86+
expect(screen.getByText('Preparing diagram..')).to.be.visible;
5887
});
5988

60-
it('Relations Inferring', () => {
61-
renderAnalysisProgressStatus({
62-
step: 'INFERRING_RELATIONSHIPS',
63-
collectionRelationsInferred: 3,
64-
totalCollections: 5,
89+
it('With relationship inferring', async () => {
90+
const store = await renderAnalysisProgressStatus({
91+
automaticallyInferRelations: true,
92+
});
93+
expect(screen.getByText('Sampling collections..')).to.be.visible;
94+
expect(screen.getByText('0/3')).to.be.visible;
95+
96+
// Fetch and analyze all samples
97+
store.dispatch({
98+
type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED,
99+
});
100+
store.dispatch({
101+
type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED,
102+
});
103+
store.dispatch({
104+
type: AnalysisProcessActionTypes.NAMESPACE_SAMPLE_FETCHED,
105+
});
106+
store.dispatch({
107+
type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED,
108+
});
109+
store.dispatch({
110+
type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED,
111+
});
112+
store.dispatch({
113+
type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED,
114+
});
115+
116+
expect(screen.getByText('Inferring relationships between collections..'))
117+
.to.be.visible;
118+
expect(screen.queryByText('0/3')).not.to.exist;
119+
120+
// Infer some relationships
121+
store.dispatch({
122+
type: AnalysisProcessActionTypes.NAMESPACE_RELATIONS_INFERRED,
123+
});
124+
store.dispatch({
125+
type: AnalysisProcessActionTypes.NAMESPACE_RELATIONS_INFERRED,
65126
});
127+
66128
expect(screen.getByText('Inferring relationships between collections..'))
67129
.to.be.visible;
68-
expect(screen.queryByText('3/5')).not.exist;
130+
expect(screen.queryByText('2/3')).not.to.exist;
131+
132+
// Finish inferring
133+
store.dispatch({
134+
type: AnalysisProcessActionTypes.NAMESPACE_RELATIONS_INFERRED,
135+
});
136+
137+
expect(screen.getByText('Preparing diagram..')).to.be.visible;
69138
});
70139
});
71140
});

packages/compass-data-modeling/test/setup-store.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type ConnectionInfoWithMockData = ConnectionInfo & {
2828
};
2929

3030
export type DataModelingStore = Awaited<ReturnType<typeof setupStore>>;
31-
const testConnections = [
31+
export const testConnections = [
3232
{
3333
id: 'one',
3434
savedConnectionType: 'favorite' as const,
@@ -174,6 +174,19 @@ export const setupStore = (
174174
conn.databases.find((x) => x._id === database)?.collections
175175
);
176176
},
177+
sample: () => {
178+
return new Promise((resolve) =>
179+
setTimeout(
180+
() =>
181+
resolve([
182+
{ _id: 'sample1' },
183+
{ _id: 'sample2' },
184+
{ _id: 'sample3' },
185+
]),
186+
1
187+
)
188+
);
189+
},
177190
};
178191
},
179192
} as any,

0 commit comments

Comments
 (0)