Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
64ae5fd
feat(aggregation-count): count docs
mabaasit Apr 21, 2022
685d704
Merge branch 'main' of github.com:mongodb-js/compass into COMPASS-575…
mabaasit Apr 21, 2022
5170829
Merge branch 'main' of github.com:mongodb-js/compass into COMPASS-575…
mabaasit Apr 21, 2022
4c93906
feat(aggregation-count): count fix
mabaasit Apr 21, 2022
1b55829
feat(aggregation-count): count tests
mabaasit Apr 21, 2022
e12f862
Merge branch 'main' of github.com:mongodb-js/compass into COMPASS-575…
mabaasit Apr 21, 2022
cc391a7
feat(aggregation-count): use aggregation count on export
mabaasit Apr 21, 2022
03a3ef4
feat(aggregation-count): cancel count
mabaasit Apr 22, 2022
35e37ac
feat(aggregation-count): cancel when user switches to builder view
mabaasit Apr 22, 2022
38b5bc5
Merge branch 'main' into COMPASS-5756-aggregation-count
mabaasit Apr 22, 2022
0a3fc24
Merge branch 'main' into COMPASS-5756-aggregation-count
mabaasit Apr 23, 2022
502371a
Merge branch 'main' of github.com:mongodb-js/compass into COMPASS-575…
mabaasit Apr 25, 2022
916586b
feat(aggregation-count): cancel aggregation
mabaasit Apr 25, 2022
95aaa29
feat(aggregation-count): handle run race condition
mabaasit Apr 27, 2022
6c6e404
Merge branch 'COMPASS-5756-aggregation-count' of github.com:mongodb-j…
mabaasit Apr 27, 2022
5ea676a
Update packages/compass-import-export/src/modules/export.js
mabaasit Apr 27, 2022
e8155f6
Merge branch 'main' into COMPASS-5756-aggregation-count
mabaasit Apr 27, 2022
aaff451
Merge branch 'main' into COMPASS-5756-aggregation-count
mabaasit Apr 27, 2022
e4b21d5
feat(aggregation-count): clean up action
mabaasit Apr 27, 2022
0d6f6e6
Merge branch 'main' into COMPASS-5756-aggregation-count
mabaasit Apr 27, 2022
2cec9a1
Merge branch 'main' into COMPASS-5756-aggregation-count
mabaasit Apr 27, 2022
9e1c3a3
feat(aggregation-count): remove cancelled
mabaasit Apr 27, 2022
6e7022c
Merge branch 'main' into COMPASS-5756-aggregation-count
mabaasit Apr 27, 2022
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
@@ -0,0 +1,48 @@
import React from 'react';
import { render, screen, within } from '@testing-library/react';
import { expect } from 'chai';
import { spy } from 'sinon';
import userEvent from '@testing-library/user-event';

import { PipelinePaginationCount } from './pipeline-pagination-count';

describe('PipelinePagination', function () {
it('renders count button by default', function () {
render(
<PipelinePaginationCount
loading={false}
count={undefined}
onCount={() => {}}
/>
);

const container = screen.getByTestId('pipeline-pagination-count');
expect(within(container).getByTestId('pipeline-pagination-count-action')).to
.exist;
});
it('calls onCount when clicked', function () {
const onCountSpy = spy();
render(
<PipelinePaginationCount
loading={false}
count={undefined}
onCount={onCountSpy}
/>
);

expect(onCountSpy.calledOnce).to.be.false;
const container = screen.getByTestId('pipeline-pagination-count');
userEvent.click(
within(container).getByTestId('pipeline-pagination-count-action')
);
expect(onCountSpy.calledOnce).to.be.true;
});
it('renders count of results', function () {
render(
<PipelinePaginationCount loading={false} count={20} onCount={() => {}} />
);

const container = screen.getByTestId('pipeline-pagination-count');
expect(within(container).getByText('of 20')).to.exist;
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React from 'react';
import { connect } from 'react-redux';
import {
css,
spacing,
Body,
Link,
uiColors,
Tooltip,
SpinLoader,
} from '@mongodb-js/compass-components';

import type { RootState } from '../../modules';
import { countDocuments } from '../../modules/count-documents';

type PipelinePaginationCountProps = {
loading: boolean;
count?: number;
onCount: () => void;
};

const countButtonStyles = css({
backgroundColor: 'transparent',
border: 'none',
display: 'inline',
height: spacing[4] + spacing[1],
':focus': {
outline: `${spacing[1]}px auto ${uiColors.focus}`,
},
});

export const PipelinePaginationCount: React.FunctionComponent<PipelinePaginationCountProps> =
({ loading, count, onCount }) => {
const countDefinition = `
In order to have the final count of documents we need to run the
aggregation again. This will be the equivalent of adding a
$count as the last stage of the pipeline.
`;

const testId = 'pipeline-pagination-count';

if (loading) {
return (
<div data-testid={testId}>
<SpinLoader />
</div>
);
}

if (count !== undefined) {
return (
<div data-testid={testId}>
<Body>of {count}</Body>
</div>
);
}

return (
<div data-testid={testId}>
<Tooltip
trigger={({ children, ...props }) => (
<Link
{...props}
aria-label={'count results'}
as="button"
data-testid="pipeline-pagination-count-action"
hideExternalIcon={true}
className={countButtonStyles}
onClick={() => onCount()}
>
{children}
count results
</Link>
)}
>
<Body>{countDefinition}</Body>
</Tooltip>
</div>
);
};

const mapState = ({ countDocuments: { loading, count } }: RootState) => ({
loading,
count,
});

const mapDispatch = {
onCount: countDocuments,
};

export default connect(mapState, mapDispatch)(PipelinePaginationCount);
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { render, screen, within } from '@testing-library/react';
import { expect } from 'chai';
import { spy } from 'sinon';
import userEvent from '@testing-library/user-event';
import { Provider } from 'react-redux';

import configureStore from '../../stores/store';

import {
PipelinePagination,
Expand All @@ -12,16 +15,18 @@ import {

const renderPipelinePagination = (props: Record<string, unknown> = {}) => {
render(
<PipelinePagination
showingFrom={1}
showingTo={20}
isCountDisabled={false}
isPrevDisabled={false}
isNextDisabled={false}
onPrev={() => {}}
onNext={() => {}}
{...props}
/>
<Provider store={configureStore()}>
<PipelinePagination
showingFrom={1}
showingTo={20}
isCountDisabled={false}
isPrevDisabled={false}
isNextDisabled={false}
onPrev={() => {}}
onNext={() => {}}
{...props}
/>
</Provider>
);
};

Expand All @@ -33,6 +38,8 @@ describe('PipelinePagination', function () {
expect(
within(container).getByTestId('pipeline-pagination-desc').textContent
).to.equal('Showing 1 – 20');
expect(within(container).getByTestId('pipeline-pagination-count')).to
.exist;
expect(within(container).getByTestId('pipeline-pagination-prev-action'))
.to.exist;
expect(within(container).getByTestId('pipeline-pagination-next-action'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import type { RootState } from '../../modules';
import { fetchNextPage, fetchPrevPage } from '../../modules/aggregation';

import PipelinePaginationCount from './pipeline-pagination-count';

type PipelinePaginationProps = {
showingFrom: number;
showingTo: number;
Expand All @@ -27,6 +29,12 @@ const containerStyles = css({
gap: spacing[2],
});

const paginationStyles = css({
display: 'flex',
gap: spacing[1],
alignItems: 'center',
});

export const PipelinePagination: React.FunctionComponent<PipelinePaginationProps> =
({
showingFrom,
Expand All @@ -40,9 +48,12 @@ export const PipelinePagination: React.FunctionComponent<PipelinePaginationProps
return (
<div className={containerStyles} data-testid="pipeline-pagination">
{!isCountDisabled && (
<Body data-testid="pipeline-pagination-desc">
Showing {showingFrom} – {showingTo}
</Body>
<div className={paginationStyles}>
<Body data-testid="pipeline-pagination-desc">
Showing {showingFrom} – {showingTo}
</Body>
<PipelinePaginationCount />
</div>
)}
<div>
<IconButton
Expand Down Expand Up @@ -85,17 +96,22 @@ export const calculateShowingTo = ({

const mapState = ({
aggregation: { documents, isLast, page, limit, loading, error },
}: RootState) => ({
showingFrom: calculateShowingFrom({ limit, page }),
showingTo: calculateShowingTo({
countDocuments: { count },
}: RootState) => {
const showingFrom = calculateShowingFrom({ limit, page });
const showingTo = calculateShowingTo({
limit,
page,
documentCount: documents.length,
}),
isCountDisabled: Boolean(error),
isPrevDisabled: page <= 1 || loading || Boolean(error),
isNextDisabled: isLast || loading || Boolean(error),
});
});
return {
showingFrom,
showingTo,
isCountDisabled: Boolean(error),
isPrevDisabled: page <= 1 || loading || Boolean(error),
isNextDisabled: isLast || loading || Boolean(error) || count === showingTo,
};
};

const mapDispatch = {
onPrev: fetchPrevPage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe('aggregation module', function () {
loading: false,
error: undefined,
abortController: undefined,
previousPageData: undefined,
});
});

Expand Down Expand Up @@ -152,6 +153,7 @@ describe('aggregation module', function () {
loading: false,
error: undefined,
abortController: undefined,
previousPageData: undefined,
});
});

Expand Down Expand Up @@ -205,6 +207,7 @@ describe('aggregation module', function () {
loading: false,
error: undefined,
abortController: undefined,
previousPageData: undefined,
});
});
it('nextPage -> on last page', async function () {
Expand Down Expand Up @@ -277,6 +280,7 @@ describe('aggregation module', function () {
loading: false,
error: undefined,
abortController: undefined,
previousPageData: undefined,
});
});
it('prevPage -> on first page', async function () {
Expand Down
Loading