Skip to content

Commit ec89d93

Browse files
authored
feat: Add covered and optimal query examples CLOUDP-311782 (#6883)
* added covered queries and optimal queries * polished up and added tests * updated state to be an object and changed to useCallback for button click
1 parent d661a15 commit ec89d93

File tree

2 files changed

+305
-96
lines changed

2 files changed

+305
-96
lines changed

packages/compass-indexes/src/components/create-index-form/index-flow-section.spec.tsx

Lines changed: 140 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,165 @@ import React from 'react';
22
import { render, screen } from '@mongodb-js/testing-library-compass';
33
import IndexFlowSection from './index-flow-section';
44
import { expect } from 'chai';
5+
import type { Field } from '../../modules/create-index';
56

67
describe('IndexFlowSection', () => {
7-
const renderComponent = (createIndexFieldsComponent?: JSX.Element) => {
8+
const renderComponent = ({
9+
createIndexFieldsComponent,
10+
fields,
11+
}: {
12+
createIndexFieldsComponent?: JSX.Element;
13+
fields?: Field[];
14+
}) => {
815
render(
916
<IndexFlowSection
1017
createIndexFieldsComponent={createIndexFieldsComponent ?? null}
11-
fields={[]}
18+
fields={fields || []}
1219
dbName={'fakeDBName'}
1320
collectionName={'fakeCollectionName'}
1421
/>
1522
);
1623
};
17-
it('renders the Input Index header', () => {
18-
renderComponent();
19-
expect(screen.getByText('Input Index')).to.be.visible;
20-
});
2124

22-
it('renders the Code Equivalent toggle', () => {
23-
renderComponent();
24-
expect(screen.getByLabelText('Toggle Code Equivalent')).to.be.visible;
25-
});
25+
describe('when the fields are not filled in', () => {
26+
it('renders the Input Index header', () => {
27+
renderComponent({});
28+
expect(screen.getByText('Input Index')).to.be.visible;
29+
});
2630

27-
it('renders the Show covered queries button', () => {
28-
renderComponent();
29-
expect(screen.getByText('Show covered queries')).to.be.visible;
30-
});
31+
it('does not render the Covered Queries header', () => {
32+
renderComponent({});
33+
expect(screen.queryByText('Covered Queries')).to.be.null;
34+
});
3135

32-
it('renders the Covered Queries header', () => {
33-
renderComponent();
34-
expect(screen.getByText('Covered Queries')).to.be.visible;
35-
});
36+
it('renders the Code Equivalent toggle', () => {
37+
renderComponent({});
38+
expect(screen.getByLabelText('Toggle Code Equivalent')).to.be.visible;
39+
});
3640

37-
it('renders the provided createIndexFieldsComponent', () => {
38-
const mockComponent = (
39-
<div data-testid="mock-component">Mock Component</div>
40-
);
41-
renderComponent(mockComponent);
42-
expect(screen.getByTestId('mock-component')).to.be.visible;
41+
it('renders the Show covered queries button and it\\s disabled', () => {
42+
renderComponent({});
43+
const coveredQueriesButton = screen.getByTestId(
44+
'index-flow-section-covered-queries-button'
45+
);
46+
47+
expect(coveredQueriesButton).to.be.visible;
48+
});
49+
50+
it('does not render the covered queries examples', () => {
51+
renderComponent({});
52+
expect(
53+
screen.queryByTestId('index-flow-section-covered-queries-examples')
54+
).not.to.exist;
55+
});
56+
57+
it('does not render the optimal query examples', () => {
58+
renderComponent({});
59+
expect(
60+
screen.queryByTestId('index-flow-section-optimal-queries-examples')
61+
).not.to.exist;
62+
});
63+
64+
it('renders the provided createIndexFieldsComponent', () => {
65+
const mockComponent = (
66+
<div data-testid="mock-component">Mock Component</div>
67+
);
68+
renderComponent({ createIndexFieldsComponent: mockComponent });
69+
expect(screen.getByTestId('mock-component')).to.be.visible;
70+
});
4371
});
4472

45-
it('renders the covered queries examples', () => {
46-
renderComponent();
47-
expect(screen.getByTestId('index-flow-section-covered-queries-examples')).to
48-
.exist;
73+
describe('when 3 index fields are filled in and user clicks on covered queries button', () => {
74+
const fields: Field[] = [
75+
{ name: 'field1', type: '1 (asc)' },
76+
{ name: 'field2', type: '-1 (desc)' },
77+
{ name: 'field3', type: '1 (asc)' },
78+
];
79+
80+
beforeEach(() => {
81+
renderComponent({ fields });
82+
screen.getByTestId('index-flow-section-covered-queries-button').click();
83+
});
84+
85+
it('renders the covered queries examples', () => {
86+
const coveredQueriesExamples = screen.getByTestId(
87+
'index-flow-section-covered-queries-examples'
88+
);
89+
expect(coveredQueriesExamples).to.exist;
90+
expect(coveredQueriesExamples).to.contain.text(
91+
JSON.stringify({
92+
field1: 1,
93+
field2: 2,
94+
field3: 3,
95+
})
96+
);
97+
});
98+
99+
it('renders the optimal query examples', () => {
100+
const optimalQueriesExamples = screen.getByTestId(
101+
'index-flow-section-optimal-queries-examples'
102+
);
103+
expect(optimalQueriesExamples).to.exist;
104+
expect(optimalQueriesExamples).to.contain.text(
105+
`{"field1":1,"field2":{"$gt":2}}.sort(field3: 1})`
106+
);
107+
});
108+
109+
it('renders the Learn More link', () => {
110+
const link = screen.getByText('Learn More');
111+
expect(link).to.be.visible;
112+
});
49113
});
50114

51-
it('renders the optimal query examples', () => {
52-
renderComponent();
53-
expect(screen.getByTestId('index-flow-section-optimal-query-examples')).to
54-
.exist;
115+
describe('when 2 index fields are filled in and user clicks on covered queries button', () => {
116+
const fields: Field[] = [
117+
{ name: 'field1', type: '1 (asc)' },
118+
{ name: 'field2', type: '1 (asc)' },
119+
];
120+
121+
beforeEach(() => {
122+
renderComponent({ fields });
123+
screen.getByTestId('index-flow-section-covered-queries-button').click();
124+
});
125+
126+
it('renders the covered queries examples', () => {
127+
const coveredQueriesExamples = screen.getByTestId(
128+
'index-flow-section-covered-queries-examples'
129+
);
130+
expect(coveredQueriesExamples).to.exist;
131+
});
132+
133+
it('renders the optimal query examples', () => {
134+
const optimalQueriesExamples = screen.getByTestId(
135+
'index-flow-section-optimal-queries-examples'
136+
);
137+
expect(optimalQueriesExamples).to.exist;
138+
expect(optimalQueriesExamples).to.contain.text(
139+
`{"field1":1,"field2":{"$gt":2}}}`
140+
);
141+
expect(optimalQueriesExamples).to.contain.text(
142+
`{"field1":1}.sort({"field2":2})`
143+
);
144+
});
55145
});
56146

57-
it('renders the Learn More link', () => {
58-
renderComponent();
59-
const link = screen.getByText('Learn More');
60-
expect(link).to.be.visible;
147+
describe('when 1 index field is filled in and user clicks on covered queries button', () => {
148+
const fields: Field[] = [{ name: 'field1', type: '1 (asc)' }];
149+
150+
beforeEach(() => {
151+
renderComponent({ fields });
152+
screen.getByTestId('index-flow-section-covered-queries-button').click();
153+
});
154+
155+
it('renders the covered queries examples', () => {
156+
expect(screen.getByTestId('index-flow-section-covered-queries-examples'))
157+
.to.exist;
158+
});
159+
160+
it('does not render the optimal query examples', () => {
161+
expect(
162+
screen.queryByTestId('index-flow-section-optimal-queries-examples')
163+
).not.to.exist;
164+
});
61165
});
62166
});

0 commit comments

Comments
 (0)