Skip to content

feat: Add covered and optimal query examples CLOUDP-311782 #6883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
polished up and added tests
  • Loading branch information
rubydong committed Apr 28, 2025
commit 175ebaa124aedbe8691828907e84ccaf143555d1
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,165 @@ import React from 'react';
import { render, screen } from '@mongodb-js/testing-library-compass';
import IndexFlowSection from './index-flow-section';
import { expect } from 'chai';
import type { Field } from '../../modules/create-index';

describe('IndexFlowSection', () => {
const renderComponent = (createIndexFieldsComponent?: JSX.Element) => {
const renderComponent = ({
createIndexFieldsComponent,
fields,
}: {
createIndexFieldsComponent?: JSX.Element;
fields?: Field[];
}) => {
render(
<IndexFlowSection
createIndexFieldsComponent={createIndexFieldsComponent ?? null}
fields={[]}
fields={fields || []}
dbName={'fakeDBName'}
collectionName={'fakeCollectionName'}
/>
);
};
it('renders the Input Index header', () => {
renderComponent();
expect(screen.getByText('Input Index')).to.be.visible;
});

it('renders the Code Equivalent toggle', () => {
renderComponent();
expect(screen.getByLabelText('Toggle Code Equivalent')).to.be.visible;
});
describe('when the fields are not filled in', () => {
it('renders the Input Index header', () => {
renderComponent({});
expect(screen.getByText('Input Index')).to.be.visible;
});

it('renders the Show covered queries button', () => {
renderComponent();
expect(screen.getByText('Show covered queries')).to.be.visible;
});
it('does not render the Covered Queries header', () => {
renderComponent({});
expect(screen.queryByText('Covered Queries')).to.be.null;
});

it('renders the Covered Queries header', () => {
renderComponent();
expect(screen.getByText('Covered Queries')).to.be.visible;
});
it('renders the Code Equivalent toggle', () => {
renderComponent({});
expect(screen.getByLabelText('Toggle Code Equivalent')).to.be.visible;
});

it('renders the provided createIndexFieldsComponent', () => {
const mockComponent = (
<div data-testid="mock-component">Mock Component</div>
);
renderComponent(mockComponent);
expect(screen.getByTestId('mock-component')).to.be.visible;
it('renders the Show covered queries button and it\\s disabled', () => {
renderComponent({});
const coveredQueriesButton = screen.getByTestId(
'index-flow-section-covered-queries-button'
);

expect(coveredQueriesButton).to.be.visible;
});

it('does not render the covered queries examples', () => {
renderComponent({});
expect(
screen.queryByTestId('index-flow-section-covered-queries-examples')
).not.to.exist;
});

it('does not render the optimal query examples', () => {
renderComponent({});
expect(
screen.queryByTestId('index-flow-section-optimal-queries-examples')
).not.to.exist;
});

it('renders the provided createIndexFieldsComponent', () => {
const mockComponent = (
<div data-testid="mock-component">Mock Component</div>
);
renderComponent({ createIndexFieldsComponent: mockComponent });
expect(screen.getByTestId('mock-component')).to.be.visible;
});
});

it('renders the covered queries examples', () => {
renderComponent();
expect(screen.getByTestId('index-flow-section-covered-queries-examples')).to
.exist;
describe('when 3 index fields are filled in and user clicks on covered queries button', () => {
const fields: Field[] = [
{ name: 'field1', type: '1 (asc)' },
{ name: 'field2', type: '-1 (desc)' },
{ name: 'field3', type: '1 (asc)' },
];

beforeEach(() => {
renderComponent({ fields });
screen.getByTestId('index-flow-section-covered-queries-button').click();
});

it('renders the covered queries examples', () => {
const coveredQueriesExamples = screen.getByTestId(
'index-flow-section-covered-queries-examples'
);
expect(coveredQueriesExamples).to.exist;
expect(coveredQueriesExamples).to.contain.text(
JSON.stringify({
field1: 1,
field2: 2,
field3: 3,
})
);
});

it('renders the optimal query examples', () => {
const optimalQueriesExamples = screen.getByTestId(
'index-flow-section-optimal-queries-examples'
);
expect(optimalQueriesExamples).to.exist;
expect(optimalQueriesExamples).to.contain.text(
`{"field1":1,"field2":{"$gt":2}}.sort(field3: 1})`
);
});

it('renders the Learn More link', () => {
const link = screen.getByText('Learn More');
expect(link).to.be.visible;
});
});

it('renders the optimal query examples', () => {
renderComponent();
expect(screen.getByTestId('index-flow-section-optimal-query-examples')).to
.exist;
describe('when 2 index fields are filled in and user clicks on covered queries button', () => {
const fields: Field[] = [
{ name: 'field1', type: '1 (asc)' },
{ name: 'field2', type: '1 (asc)' },
];

beforeEach(() => {
renderComponent({ fields });
screen.getByTestId('index-flow-section-covered-queries-button').click();
});

it('renders the covered queries examples', () => {
const coveredQueriesExamples = screen.getByTestId(
'index-flow-section-covered-queries-examples'
);
expect(coveredQueriesExamples).to.exist;
});

it('renders the optimal query examples', () => {
const optimalQueriesExamples = screen.getByTestId(
'index-flow-section-optimal-queries-examples'
);
expect(optimalQueriesExamples).to.exist;
expect(optimalQueriesExamples).to.contain.text(
`{"field1":1,"field2":{"$gt":2}}}`
);
expect(optimalQueriesExamples).to.contain.text(
`{"field1":1}.sort({"field2":2})`
);
});
});

it('renders the Learn More link', () => {
renderComponent();
const link = screen.getByText('Learn More');
expect(link).to.be.visible;
describe('when 1 index field is filled in and user clicks on covered queries button', () => {
const fields: Field[] = [{ name: 'field1', type: '1 (asc)' }];

beforeEach(() => {
renderComponent({ fields });
screen.getByTestId('index-flow-section-covered-queries-button').click();
});

it('renders the covered queries examples', () => {
expect(screen.getByTestId('index-flow-section-covered-queries-examples'))
.to.exist;
});

it('does not render the optimal query examples', () => {
expect(
screen.queryByTestId('index-flow-section-optimal-queries-examples')
).not.to.exist;
});
});
});
Loading
Loading