Skip to content

Commit 90c1998

Browse files
authored
fix(compass-validation): unified validation preview CTA COMPASS-8862 (#6769)
1 parent d34486f commit 90c1998

File tree

9 files changed

+353
-281
lines changed

9 files changed

+353
-281
lines changed

packages/compass-e2e-tests/helpers/selectors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ export const UpdateValidationButton =
11531153
'[data-testid="update-validation-button"]';
11541154
export const ValidationMatchingDocumentsPreview =
11551155
'[data-testid="validation-content"] [data-testid="matching-documents"] [data-testid="document-preview"]';
1156-
export const ValidationLoadMatchingDocumentsBtn = `${ValidationMatchingDocumentsPreview} [data-testid="load-sample-document"]`;
1156+
export const ValidationLoadSampleDocumentsBtn = `[data-testid="load-sample-documents"]`;
11571157
export const ValidationNotMatchingDocumentsPreview =
11581158
'[data-testid="validation-content"] [data-testid="notmatching-documents"] [data-testid="document-preview"]';
11591159
export const ValidationLoadNotMatchingDocumentsBtn = `${ValidationNotMatchingDocumentsPreview} [data-testid="load-sample-document"]`;

packages/compass-e2e-tests/tests/collection-validation-tab.test.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import * as Selectors from '../helpers/selectors';
1010
import { createNumbersCollection } from '../helpers/insert-data';
1111

1212
const NO_PREVIEW_DOCUMENTS = 'No Preview Documents';
13-
const LOAD_SAMPLE_DOCUMENT = 'Load document';
1413
const PASSING_VALIDATOR = '{ $jsonSchema: {} }';
1514
const FAILING_VALIDATOR =
1615
'{ $jsonSchema: { bsonType: "object", required: [ "phone" ] } }';
@@ -54,9 +53,11 @@ describe('Collection validation tab', function () {
5453
}
5554

5655
context('when the schema validation is set or modified', function () {
57-
it('provides users with a button to load sample documents', async function () {
56+
it('provides users with a single button to load sample documents', async function () {
5857
await addValidation(PASSING_VALIDATOR);
5958

59+
await browser.clickVisible(Selectors.ValidationLoadSampleDocumentsBtn);
60+
6061
await browser.waitUntil(async () => {
6162
const matchTextElement = browser.$(
6263
Selectors.ValidationMatchingDocumentsPreview
@@ -67,18 +68,15 @@ describe('Collection validation tab', function () {
6768
);
6869
const notMatchingText = await notMatchingTextElement.getText();
6970
return (
70-
matchText === LOAD_SAMPLE_DOCUMENT &&
71-
notMatchingText === LOAD_SAMPLE_DOCUMENT
71+
matchText.includes('ObjectId(') &&
72+
notMatchingText === NO_PREVIEW_DOCUMENTS
7273
);
7374
});
7475
});
7576

7677
it('supports rules in JSON schema', async function () {
7778
await addValidation(FAILING_VALIDATOR);
78-
await browser.clickVisible(Selectors.ValidationLoadMatchingDocumentsBtn);
79-
await browser.clickVisible(
80-
Selectors.ValidationLoadNotMatchingDocumentsBtn
81-
);
79+
await browser.clickVisible(Selectors.ValidationLoadSampleDocumentsBtn);
8280

8381
// nothing passed, everything failed
8482
await browser.waitUntil(async () => {
@@ -107,10 +105,7 @@ describe('Collection validation tab', function () {
107105

108106
// the automatic indentation and brackets makes multi-line values very fiddly here
109107
await browser.setValidation(PASSING_VALIDATOR);
110-
await browser.clickVisible(Selectors.ValidationLoadMatchingDocumentsBtn);
111-
await browser.clickVisible(
112-
Selectors.ValidationLoadNotMatchingDocumentsBtn
113-
);
108+
await browser.clickVisible(Selectors.ValidationLoadSampleDocumentsBtn);
114109

115110
// nothing failed, everything passed
116111
await browser.waitUntil(async () => {

packages/compass-schema-validation/src/components/document-preview.spec.tsx

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,14 @@ import { expect } from 'chai';
55
import { DocumentPreview } from './document-preview';
66

77
describe('DocumentPreview [Component]', function () {
8-
context('when document loading state is initial', function () {
9-
it('renders a button to load a sample document', function () {
10-
const component = mount(<DocumentPreview loadingState="initial" />);
11-
expect(component.find('[data-testid="load-sample-document"]')).to.exist;
12-
});
13-
});
14-
15-
context('when document loading state is loading', function () {
16-
it('renders a spinner', function () {
17-
const component = mount(<DocumentPreview loadingState="loading" />);
18-
expect(component.find('[data-testid="load-sample-spinner"]')).to.exist;
19-
});
20-
});
21-
228
context('when document loading state is success', function () {
239
it('renders a document if there is one present', function () {
24-
const component = mount(
25-
<DocumentPreview loadingState="success" document={{}} />
26-
);
10+
const component = mount(<DocumentPreview document={{}} />);
2711
expect(component.find('HadronDocument')).to.exist;
2812
});
2913

3014
it('renders a no preview text when there is no document', function () {
31-
const component = mount(<DocumentPreview loadingState="success" />);
32-
expect(component).to.have.text('No Preview Documents');
33-
});
34-
});
35-
36-
context('when document loading state is error', function () {
37-
it('renders a no preview text', function () {
38-
const component = mount(<DocumentPreview loadingState="error" />);
15+
const component = mount(<DocumentPreview />);
3916
expect(component).to.have.text('No Preview Documents');
4017
});
4118
});
Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
import React from 'react';
2-
import { connect } from 'react-redux';
32
import {
43
Body,
54
KeylineCard,
65
css,
76
cx,
87
spacing,
9-
Button,
108
} from '@mongodb-js/compass-components';
119
import { Document } from '@mongodb-js/compass-crud';
1210

13-
import { LoadingOverlay } from './loading-overlay';
14-
import type { DOCUMENT_LOADING_STATES } from '../modules/sample-documents';
15-
import {
16-
fetchValidDocument,
17-
fetchInvalidDocument,
18-
} from '../modules/sample-documents';
19-
import type { RootState } from '../modules';
20-
2111
const previewStyles = css({
2212
display: 'flex',
2313
height: spacing[6] * 3,
@@ -30,45 +20,20 @@ const noPreviewStyles = css({
3020
alignItems: 'center',
3121
});
3222

33-
const loadSampleStyles = css({
34-
width: '100%',
35-
textAlign: 'center',
36-
});
37-
3823
const noPreviewTextStyles = css({
3924
padding: spacing[3],
4025
textAlign: 'center',
4126
fontStyle: 'italic',
4227
width: '100%',
4328
});
4429

45-
function DocumentPreview({
46-
document,
47-
loadingState,
48-
onLoadSampleClick,
49-
}: {
50-
document?: Record<string, unknown>;
51-
loadingState: DOCUMENT_LOADING_STATES;
52-
onLoadSampleClick?: () => void;
53-
}) {
30+
function DocumentPreview({ document }: { document?: Record<string, unknown> }) {
5431
return (
5532
<KeylineCard
5633
className={cx(previewStyles, document ? undefined : noPreviewStyles)}
5734
data-testid="document-preview"
5835
>
59-
{loadingState === 'initial' ? (
60-
<Body as="div" className={loadSampleStyles}>
61-
<Button
62-
data-testid="load-sample-document"
63-
size="small"
64-
onClick={onLoadSampleClick}
65-
>
66-
Load document
67-
</Button>
68-
</Body>
69-
) : loadingState === 'loading' ? (
70-
<LoadingOverlay />
71-
) : document ? (
36+
{document ? (
7237
<Document doc={document} editable={false} />
7338
) : (
7439
<Body
@@ -82,24 +47,4 @@ function DocumentPreview({
8247
);
8348
}
8449

85-
const ValidDocumentPreview = connect(
86-
(state: RootState) => ({
87-
document: state.sampleDocuments.validDocument,
88-
loadingState: state.sampleDocuments.validDocumentState,
89-
}),
90-
{
91-
onLoadSampleClick: fetchValidDocument,
92-
}
93-
)(DocumentPreview);
94-
95-
const InvalidDocumentPreview = connect(
96-
(state: RootState) => ({
97-
document: state.sampleDocuments.invalidDocument,
98-
loadingState: state.sampleDocuments.invalidDocumentState,
99-
}),
100-
{
101-
onLoadSampleClick: fetchInvalidDocument,
102-
}
103-
)(DocumentPreview);
104-
105-
export { DocumentPreview, ValidDocumentPreview, InvalidDocumentPreview };
50+
export { DocumentPreview };
Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
11
import React from 'react';
2-
import { mount } from 'enzyme';
2+
import { render, screen } from '@mongodb-js/testing-library-compass';
33
import { expect } from 'chai';
44
import { SampleDocuments } from './sample-documents';
55
import { Provider } from 'react-redux';
66
import { configureStore } from '../stores/store';
7+
import { INITIAL_STATE } from '../modules';
78

89
describe('SampleDocuments [Component]', function () {
9-
it('renders a valid and invalid document preview', function () {
10+
it('initial state : renders the CTA', function () {
1011
const store = configureStore({}, {} as any);
11-
const component = mount(
12+
render(
1213
<Provider store={store}>
1314
<SampleDocuments />
1415
</Provider>
1516
);
1617

17-
expect(component.find('[data-testid="matching-documents"]')).to.exist;
18-
expect(component.find('[data-testid="notmatching-documents"]')).to.exist;
18+
expect(screen.getByRole('button', { name: 'Preview documents' })).to.be
19+
.visible;
20+
});
21+
22+
it('non initial state : renders a valid and invalid document preview', function () {
23+
const store = configureStore(
24+
{
25+
...INITIAL_STATE,
26+
sampleDocuments: {
27+
validDocumentState: 'loading',
28+
invalidDocumentState: 'loading',
29+
validDocument: undefined,
30+
invalidDocument: undefined,
31+
},
32+
},
33+
{} as any
34+
);
35+
render(
36+
<Provider store={store}>
37+
<SampleDocuments />
38+
</Provider>
39+
);
40+
41+
expect(screen.queryByRole('button', { name: 'Preview documents' })).not.to
42+
.exist;
43+
expect(screen.getByTestId('matching-documents')).to.be.visible;
44+
expect(screen.getByTestId('notmatching-documents')).to.be.visible;
1945
});
2046
});

0 commit comments

Comments
 (0)