Skip to content

Commit

Permalink
chore(indexes): convert create index plugin to the new plugin interfa…
Browse files Browse the repository at this point in the history
…ce (mongodb-js#5277)

chore(indexes): convert create index plugin to new plugin interface
  • Loading branch information
gribnoysup authored Dec 28, 2023
1 parent beec3cb commit 351c35f
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 270 deletions.
7 changes: 6 additions & 1 deletion packages/compass-home/src/components/workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { CompassSchemaPlugin } from '@mongodb-js/compass-schema';
import {
CompassIndexesPlugin,
DropIndexPlugin as DropIndexCollectionTabModal,
CreateIndexPlugin as CreateIndexCollectionTabModal,
} from '@mongodb-js/compass-indexes';
import { CompassSchemaValidationPlugin } from '@mongodb-js/compass-schema-validation';
import { CreateViewPlugin } from '@mongodb-js/compass-aggregations';
Expand Down Expand Up @@ -76,7 +77,11 @@ export default function Workspace({
CompassIndexesPlugin,
CompassSchemaValidationPlugin,
]}
modals={[ExplainPlanCollectionTabModal, DropIndexCollectionTabModal]}
modals={[
ExplainPlanCollectionTabModal,
DropIndexCollectionTabModal,
CreateIndexCollectionTabModal,
]}
>
<WorkspacesPlugin
initialWorkspaceTabs={[{ type: 'My Queries' }]}
Expand Down
1 change: 0 additions & 1 deletion packages/compass-indexes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
"mongodb-query-parser": "^4.0.0",
"numeral": "^2.0.6",
"nyc": "^15.1.0",
"prop-types": "^15.7.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^8.1.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ import { createIndex, closeCreateIndexModal } from '../../modules/create-index';
import { CreateIndexForm } from '../create-index-form/create-index-form';
import CreateIndexActions from '../create-index-actions';
import type { RootState } from '../../modules/create-index';
import type { CollectionTabPluginMetadata } from '@mongodb-js/compass-collection';

type CreateIndexModalProps = React.ComponentProps<typeof CreateIndexForm> & {
isVisible: boolean;
namespace: string;
error: string | null;
clearError: () => void;
inProgress: boolean;
createIndex: () => void;
closeCreateIndexModal: () => void;
};

function CreateIndexModal({
isVisible,
Expand All @@ -30,15 +41,7 @@ function CreateIndexModal({
createIndex,
closeCreateIndexModal,
...props
}: React.ComponentProps<typeof CreateIndexForm> & {
isVisible: boolean;
namespace: string;
error: string | null;
clearError: () => void;
inProgress: boolean;
createIndex: () => void;
closeCreateIndexModal: () => void;
}) {
}: CreateIndexModalProps) {
const onSetOpen = useCallback(
(open) => {
if (!open) {
Expand Down Expand Up @@ -87,15 +90,21 @@ function CreateIndexModal({
);
}

const mapState = ({
fields,
inProgress,
schemaFields,
error,
isVisible,
namespace,
serverVersion,
}: RootState) => ({
const mapState = (
{
fields,
inProgress,
schemaFields,
error,
isVisible,
namespace,
serverVersion,
}: RootState,
// To make sure the derived type is correctly including plugin metadata passed
// by CollectionTab
// eslint-disable-next-line @typescript-eslint/no-unused-vars
ownProps: Pick<CollectionTabPluginMetadata, 'namespace' | 'serverVersion'>
) => ({
fields,
inProgress,
schemaFields,
Expand All @@ -115,4 +124,5 @@ const mapDispatch = {
updateFieldName,
updateFieldType,
};

export default connect(mapState, mapDispatch)(CreateIndexModal);
26 changes: 0 additions & 26 deletions packages/compass-indexes/src/create-index-plugin.jsx

This file was deleted.

40 changes: 16 additions & 24 deletions packages/compass-indexes/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type AppRegistry from 'hadron-app-registry';
import CreateIndexPlugin from './create-index-plugin';
import configureCreateIndexStore from './stores/create-index';
import CreateIndexModal from './components/create-index-modal';
import { activatePlugin as activateCreateIndexPlugin } from './stores/create-index';
import {
activatePlugin as activateDropIndexPlugin,
DropIndexComponent,
Expand Down Expand Up @@ -46,16 +45,17 @@ export const CompassIndexesPlugin = {
component: CompassIndexesHadronPlugin,
};

const CREATE_INDEX_ROLE = {
name: 'Create Index',
component: CreateIndexPlugin,
configureStore: configureCreateIndexStore,
configureActions: () => {
/* noop */
export const CreateIndexPlugin = registerHadronPlugin(
{
name: 'CreateIndex',
activate: activateCreateIndexPlugin,
component: CreateIndexModal,
},
storeName: 'Indexes.CreateIndexStore',
actionName: 'Indexes.CreateIndexActions',
};
{
dataService: dataServiceLocator as DataServiceLocator<'createIndex'>,
logger: createLoggerAndTelemetryLocator('COMPASS-INDEXES-UI'),
}
);

export const DropIndexPlugin = registerHadronPlugin(
{
Expand All @@ -69,20 +69,12 @@ export const DropIndexPlugin = registerHadronPlugin(
}
);

/**
* Activate all the components in the Indexes package.
* @param {Object} appRegistry - The Hadron appRegistry to activate this plugin with.
**/
function activate(appRegistry: AppRegistry): void {
appRegistry.registerRole('Collection.ScopedModal', CREATE_INDEX_ROLE);
function activate(): void {
// noop
}

/**
* Deactivate all the components in the Indexes package.
* @param {Object} appRegistry - The Hadron appRegistry to deactivate this plugin with.
**/
function deactivate(appRegistry: AppRegistry): void {
appRegistry.deregisterRole('Collection.ScopedModal', CREATE_INDEX_ROLE);
function deactivate(): void {
// noop
}

export { activate, deactivate };
Expand Down
119 changes: 66 additions & 53 deletions packages/compass-indexes/src/modules/create-index/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ function createOptions(options) {
};
}

const thunkArgs = {
localAppRegistry: { emit() {} },
dataService: { createIndex() {} },
logger: { track() {} },
};

describe('create index module', function () {
let errorSpy;
let inProgressSpy;
Expand Down Expand Up @@ -54,7 +60,7 @@ describe('create index module', function () {
const state = () => ({
fields: [{ name: '', type: '' }],
});
await createIndex()(dispatch, state);
await createIndex()(dispatch, state, thunkArgs);
expect(errorSpy.calledOnce).to.equal(true);
});
it('errors if TTL is not number', async function () {
Expand All @@ -69,7 +75,7 @@ describe('create index module', function () {
fields: [{ name: 'abc', type: 'def' }],
options: createOptions({ expireAfterSeconds: 'abc' }),
});
await createIndex()(dispatch, state);
await createIndex()(dispatch, state, thunkArgs);
expect(errorSpy.calledOnce).to.equal(true);
});
it('errors if PFE is not JSON', async function () {
Expand All @@ -84,7 +90,7 @@ describe('create index module', function () {
fields: [{ name: 'abc', type: 'def' }],
options: createOptions({ partialFilterExpression: 'abc' }),
});
await createIndex()(dispatch, state);
await createIndex()(dispatch, state, thunkArgs);
expect(errorSpy.calledOnce).to.equal(true);
});
it('calls createIndex with correct options', async function () {
Expand Down Expand Up @@ -121,29 +127,33 @@ describe('create index module', function () {
collation: "{locale: 'en'}",
expireAfterSeconds: 100,
}),
appRegistry: {
emit: emitSpy,
},
namespace: 'db.coll',
dataService: {
createIndex: (ns, spec, options) => {
expect(ns).to.equal('db.coll');
expect(spec).to.deep.equal({ abc: 1 });
expect(options).to.deep.equal({
collation: {
locale: 'en',
},
expireAfterSeconds: 100,
name: 'test name',
partialFilterExpression: { a: 1 },
unique: true,
sparse: true,
});
return Promise.resolve();
},
});
const localAppRegistry = {
emit: emitSpy,
};
const dataService = {
createIndex: (ns, spec, options) => {
expect(ns).to.equal('db.coll');
expect(spec).to.deep.equal({ abc: 1 });
expect(options).to.deep.equal({
collation: {
locale: 'en',
},
expireAfterSeconds: 100,
name: 'test name',
partialFilterExpression: { a: 1 },
unique: true,
sparse: true,
});
return Promise.resolve();
},
};
await createIndex()(dispatch, state, {
...thunkArgs,
localAppRegistry,
dataService,
});
await createIndex()(dispatch, state);
expect(resetFormSpy.calledOnce).to.equal(true, 'reset not called');
expect(clearErrorSpy.calledOnce).to.equal(true, 'clearError not called');
expect(inProgressSpy.calledTwice).to.equal(
Expand Down Expand Up @@ -189,26 +199,30 @@ describe('create index module', function () {
expireAfterSeconds: 100,
}),
namespace: 'db.coll',
appRegistry: {
emit: emitSpy,
},
dataService: {
createIndex: (ns, spec, options) => {
expect(ns).to.equal('db.coll');
expect(spec).to.deep.equal({ abc: 1 });
expect(options).to.deep.equal({
collation: {
locale: 'en',
},
expireAfterSeconds: 100,
partialFilterExpression: { a: 1 },
unique: true,
});
return Promise.resolve();
},
});
const localAppRegistry = {
emit: emitSpy,
};
const dataService = {
createIndex: (ns, spec, options) => {
expect(ns).to.equal('db.coll');
expect(spec).to.deep.equal({ abc: 1 });
expect(options).to.deep.equal({
collation: {
locale: 'en',
},
expireAfterSeconds: 100,
partialFilterExpression: { a: 1 },
unique: true,
});
return Promise.resolve();
},
};
await createIndex()(dispatch, state, {
...thunkArgs,
localAppRegistry,
dataService,
});
await createIndex()(dispatch, state);
expect(resetFormSpy.calledOnce).to.equal(true, 'reset not called');
expect(clearErrorSpy.calledOnce).to.equal(true, 'clearError not called');
expect(inProgressSpy.calledTwice).to.equal(
Expand Down Expand Up @@ -254,19 +268,18 @@ describe('create index module', function () {
sparse: false,
}),
namespace: 'db.coll',
appRegistry: {},
dataService: {
createIndex: (ns, spec, options) => {
expect(ns).to.equal('db.coll');
expect(spec).to.deep.equal({ abc: 1 });
expect(options).to.deep.equal({
name: 'test name',
});
return Promise.reject({ message: 'test err' });
},
},
});
await createIndex()(dispatch, state);
const dataService = {
createIndex: (ns, spec, options) => {
expect(ns).to.equal('db.coll');
expect(spec).to.deep.equal({ abc: 1 });
expect(options).to.deep.equal({
name: 'test name',
});
return Promise.reject({ message: 'test err' });
},
};
await createIndex()(dispatch, state, { ...thunkArgs, dataService });
expect(inProgressSpy.calledTwice).to.equal(
true,
'toggleInProgress not called'
Expand Down
Loading

0 comments on commit 351c35f

Please sign in to comment.