Skip to content
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

fix(compass-aggregations): fix preferences check for Atlas-only out stages preview #5078

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,103 +1,155 @@
import React from 'react';
import type { ComponentProps } from 'react';
import { render, screen } from '@testing-library/react';
import { cleanup, render, screen } from '@testing-library/react';
import { expect } from 'chai';
import { Provider } from 'react-redux';

import configureStore from '../../../test/configure-store';
import preferencesAccess from 'compass-preferences-model';
import { OutputStage } from './output-stage-preview';

const renderOutputStage = (
props: Partial<ComponentProps<typeof OutputStage>> = {}
) => {
render(
<Provider
store={configureStore({
pipeline: [{ $match: { _id: 1 } }, { $limit: 10 }],
})}
>
<OutputStage
operator={null}
isLoading={false}
isFinishedPersistingDocuments={false}
destinationNamespace=""
hasServerError={false}
onGoToOutputResults={() => {}}
onRunOutputStage={() => {}}
{...props}
/>
</Provider>
<OutputStage
operator={null}
isLoading={false}
isFinishedPersistingDocuments={false}
destinationNamespace="foo.bar"
hasServerError={false}
onGoToOutputResults={() => {}}
onRunOutputStage={() => {}}
{...props}
/>
);
};

describe('OutputStagePreview', function () {
(['$out', '$merge'] as const).forEach(function (operator) {
let enableAggregationBuilderRunPipeline: boolean;

afterEach(cleanup);

it('renders nothing for a non-out stage', function () {
renderOutputStage({ operator: '$match' });
expect(screen.queryAllByText(/./)).to.have.lengthOf(0);
});

for (const operator of ['$out', '$merge']) {
describe(`${operator} stage`, function () {
context('renders loader when stage is loading', function () {
it('renders loader with namespace', function () {
renderOutputStage({
operator,
isLoading: true,
destinationNamespace: 'test.out',
describe('with enableAggregationBuilderRunPipeline set to `true`', function () {
before(async function () {
enableAggregationBuilderRunPipeline =
preferencesAccess.getPreferences()
.enableAggregationBuilderRunPipeline;
await preferencesAccess.savePreferences({
enableAggregationBuilderRunPipeline: true,
});
});

after(async function () {
await preferencesAccess.savePreferences({
enableAggregationBuilderRunPipeline,
});
});

it('shows stage description in default state', function () {
renderOutputStage({ operator });
expect(
screen.getByText(/Persisting documents to test.out/i)
screen.getByText(
new RegExp(`The \\${operator} operator will cause the pipeline`)
)
).to.exist;
});

it('renders loader with generic text', function () {
it('does not show the "run" button', function () {
renderOutputStage({ operator });
expect(
screen.queryByRole('button', {
name:
operator === '$merge' ? 'Merge Documents' : 'Save Documents',
})
).to.eq(null);
});

it('shows stage description in error state', function () {
renderOutputStage({ operator, hasServerError: true });
expect(
screen.getByText(
new RegExp(`The \\${operator} operator will cause the pipeline`)
)
).to.exist;
});

it('shows stage description in loading state', function () {
renderOutputStage({ operator, isLoading: true });
expect(screen.getByText(/Persisting documents .../i)).to.exist;
expect(
screen.getByText(
new RegExp(`The \\${operator} operator will cause the pipeline`)
)
).to.exist;
});
});

it('renders nothing on server error', function () {
renderOutputStage({ operator, hasServerError: true });
expect(() => {
screen.getByTestId('output-stage-text');
}).to.throw;
it('shows stage description in finished state', function () {
renderOutputStage({ operator, isFinishedPersistingDocuments: true });
expect(
screen.getByText(
new RegExp(`The \\${operator} operator will cause the pipeline`)
)
).to.exist;
});
});

context('when documents have been persisted', function () {
context('renders the out stage preview', function () {
it('with namespace', function () {
renderOutputStage({
operator,
isFinishedPersistingDocuments: true,
destinationNamespace: 'test.out',
});
expect(
screen.getByText(/Documents persisted to collection: test.out/i)
).to.exist;
describe('with enableAggregationBuilderRunPipeline set to `false`', function () {
before(async function () {
enableAggregationBuilderRunPipeline =
preferencesAccess.getPreferences()
.enableAggregationBuilderRunPipeline;
await preferencesAccess.savePreferences({
enableAggregationBuilderRunPipeline: false,
});
});

it('without namespace', function () {
renderOutputStage({
operator,
isFinishedPersistingDocuments: true,
});
expect(
screen.getByText(/Documents persisted to specified collection/i)
).to.exist;
after(async function () {
await preferencesAccess.savePreferences({
enableAggregationBuilderRunPipeline,
});
});

it('renders go to collection button', function () {
renderOutputStage({ operator, isFinishedPersistingDocuments: true });
expect(screen.getByTestId('goto-output-collection')).to.exist;
it('shows stage description in default state', function () {
renderOutputStage({ operator });
expect(
screen.getByText(
new RegExp(`The \\${operator} operator will cause the pipeline`)
)
).to.exist;
});
});

context('default stage of component', function () {
it('renders the out stage preview', function () {
it('shows the "run" button', function () {
renderOutputStage({ operator });
expect(screen.getByTestId('output-stage-text')).to.exist;
expect(
screen.getByRole('button', {
name:
operator === '$merge' ? 'Merge Documents' : 'Save Documents',
})
).to.exist;
});
it('renders save documents button on atlas', function () {
renderOutputStage({ operator });
expect(screen.getByTestId('save-output-documents')).to.exist;

it('shows nothing in error state', function () {
renderOutputStage({ operator, hasServerError: true });
expect(screen.queryAllByText(/./)).to.have.lengthOf(0);
});

it('shows loader in loading state', function () {
renderOutputStage({ operator, isLoading: true });
expect(screen.getByText(/Persisting Documents to foo.bar/)).to.exist;
});

it('shows "Documents persisted ..." in finished state', function () {
renderOutputStage({ operator, isFinishedPersistingDocuments: true });
expect(screen.getByText(/Documents persisted to collection: foo.bar/))
.to.exist;
expect(screen.getByRole('button', { name: 'Go to collection.' })).to
.exist;
});
});
});
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export const OutputStage = ({
onRunOutputStage,
onGoToOutputResults,
}: OutputStageProps) => {
const enableAggregationBuilderRunPipeline = usePreference(
// When explicit pipeline run is not enabled, we allow to run output stage
// from the preview
const showOutputActions = !usePreference(
'enableAggregationBuilderRunPipeline',
React
);
Expand All @@ -98,31 +100,35 @@ export const OutputStage = ({
return null;
}

if (isLoading) {
return <Loader destinationNamespace={destinationNamespace} />;
}

// Stage editor show the error message.
if (hasServerError) {
return null;
}

if (isFinishedPersistingDocuments) {
return (
<div className={stagePreviewStyles}>
<Body className={stagePreviewTextStyles}>
{documentsPersistedText(destinationNamespace)}
</Body>
<Link
data-testid="goto-output-collection"
as="button"
className={stagePreviewLinkStyles}
onClick={onGoToOutputResults}
>
Go to collection.
</Link>
</div>
);
// Following states are only allowed when running out stage from the preview
// card is enabled
if (showOutputActions) {
// Stage editor show the error message.
if (hasServerError) {
return null;
}

if (isLoading) {
return <Loader destinationNamespace={destinationNamespace} />;
}

if (isFinishedPersistingDocuments) {
return (
<div className={stagePreviewStyles}>
<Body className={stagePreviewTextStyles}>
{documentsPersistedText(destinationNamespace)}
</Body>
<Link
data-testid="goto-output-collection"
as="button"
className={stagePreviewLinkStyles}
onClick={onGoToOutputResults}
>
Go to collection.
</Link>
</div>
);
}
}

return (
Expand All @@ -132,7 +138,7 @@ export const OutputStage = ({
? MERGE_STAGE_PREVIEW_TEXT
: OUT_STAGE_PREVIEW_TEXT}
</div>
{enableAggregationBuilderRunPipeline && (
{showOutputActions && (
<Button
variant="primary"
data-testid="save-output-documents"
Expand Down
Loading