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

feat(search-indexes): add a drop down to choose a search index template COMPASS-7173 #4892

Merged
merged 20 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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
71 changes: 67 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/compass-e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"ps-list": "^8.1.0",
"puppeteer": "^15.4.0",
"resolve-mongodb-srv": "^1.1.2",
"semver": "^7.5.0",
"semver": "^7.5.4",
"ts-node": "^10.9.1",
"webdriverio": "^7.16.13",
"xvfb-maybe": "^0.2.1"
Expand Down
6 changes: 4 additions & 2 deletions packages/compass-indexes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@mongodb-js/compass-editor": "^0.13.0",
"@mongodb-js/compass-logging": "^1.2.0",
"@mongodb-js/mongodb-redux-common": "^2.0.11",
"@mongodb-js/mongodb-constants": "^0.8.3",
"bson": "^6.0.0",
"compass-preferences-model": "^2.14.0",
"react": "^17.0.2"
Expand All @@ -74,6 +75,7 @@
"@testing-library/user-event": "^13.5.0",
"chai": "^4.2.0",
"depcheck": "^1.4.1",
"ejson-shell-parser": "^1.2.4",
"electron": "^26.2.2",
"enzyme": "^3.11.0",
"eslint": "^7.25.0",
Expand All @@ -93,14 +95,14 @@
"redux-thunk": "^2.4.1",
"semver": "^5.4.1",
"sinon": "^9.2.3",
"xvfb-maybe": "^0.2.1",
"ejson-shell-parser": "^1.2.4"
"xvfb-maybe": "^0.2.1"
},
"dependencies": {
"@mongodb-js/compass-components": "^1.14.0",
"@mongodb-js/compass-editor": "^0.13.0",
"@mongodb-js/compass-logging": "^1.2.0",
"@mongodb-js/mongodb-redux-common": "^2.0.11",
"@mongodb-js/mongodb-constants": "^0.8.3",
"bson": "^6.0.0",
"compass-preferences-model": "^2.14.0"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ATLAS_SEARCH_TEMPLATES } from '@mongodb-js/mongodb-constants';
import { expect } from 'chai';
import { SearchIndexTemplateDropdown } from './';
import sinon from 'sinon';
import type { SinonSpy } from 'sinon';

import { render, screen, cleanup } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import React from 'react';

function templateNamed(name: string) {
return ATLAS_SEARCH_TEMPLATES.find((t) => t.name === name);
}

describe('Search Index Template Dropdown', function () {
let onTemplateSpy: SinonSpy;

beforeEach(function () {
onTemplateSpy = sinon.spy();

render(
<SearchIndexTemplateDropdown
tooltip="Tooltip"
onTemplate={onTemplateSpy}
/>
);
});

afterEach(cleanup);

it('notifies upwards with onTemplate when a new template is choosen', async function () {
const dropDown = screen
.getByText('Dynamic field mappings')
.closest('button')!;

userEvent.click(dropDown);

const staticFieldMappingOption = await screen.findByText(
'Static field mappings'
);
userEvent.click(staticFieldMappingOption);

expect(onTemplateSpy).to.have.been.calledWith(
templateNamed('Static field mappings')
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { ATLAS_SEARCH_TEMPLATES } from '@mongodb-js/mongodb-constants';
import type { SearchTemplate } from '@mongodb-js/mongodb-constants';
import React, { useState, useCallback } from 'react';
import {
Select,
Option,
Icon,
css,
Tooltip,
} from '@mongodb-js/compass-components';

const dropdownLabelStyles = css({
display: 'flex',
pointerEvents: 'auto', // leafy green specifies none in the label, which is wrong
});

const fillParentStyles = css({
flexGrow: 1,
});

type SearchIndexTemplateDropdownLabelProps = {
label: string;
tooltip: string;
};

const SearchIndexTemplateDropdownLabel: React.FunctionComponent<
SearchIndexTemplateDropdownLabelProps
> = ({ label, tooltip }) => (
<div className={dropdownLabelStyles}>
<span className={fillParentStyles}>{label}</span>
<Tooltip
align="right"
triggerEvent="hover"
trigger={({ children, ...props }) => (
<div {...props}>
<Icon
data-testid="search-template-info-icon"
glyph="InfoWithCircle"
/>
{children}
</div>
)}
>
{tooltip}
</Tooltip>
</div>
);

type SearchIndexTemplateDropdownProps = {
tooltip: string;
onTemplate: (template: SearchTemplate) => void;
};

export const SearchIndexTemplateDropdown: React.FunctionComponent<
SearchIndexTemplateDropdownProps
> = ({ tooltip, onTemplate }) => {
const [templateValue, setTemplateValue] = useState('0');

const onChooseTemplate = useCallback(
(value: string) => {
setTemplateValue(value);
onTemplate(ATLAS_SEARCH_TEMPLATES[+value]);
},
[onTemplate]
);

return (
<Select
value={templateValue}
allowDeselect={false}
onChange={onChooseTemplate}
label={
(
<SearchIndexTemplateDropdownLabel
label="Template"
tooltip={tooltip}
/>
) as unknown as string
kmruiz marked this conversation as resolved.
Show resolved Hide resolved
kmruiz marked this conversation as resolved.
Show resolved Hide resolved
}
>
{ATLAS_SEARCH_TEMPLATES.map((template, idx) => (
<Option key={idx} value={`${idx}`}>
{template.name}
</Option>
))}
</Select>
);
};
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { ATLAS_SEARCH_TEMPLATES } from '@mongodb-js/mongodb-constants';
import { expect } from 'chai';
import { BaseSearchIndexModal } from './base-search-index-modal';
import sinon from 'sinon';
import type { SinonSpy } from 'sinon';

import { render, screen, cleanup } from '@testing-library/react';
import { render, screen, cleanup, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import React from 'react';
import { getCodemirrorEditorValue } from '@mongodb-js/compass-editor';

function normalizedTemplateNamed(name: string) {
const snippet =
ATLAS_SEARCH_TEMPLATES.find((t) => t.name === name)?.snippet || '';
// code mirror 'changes' the template placeholders, so let's do the same
// this regexp removes `tab` markers to their default value, for example:
// ${1:default} => default
return snippet.replace(/\${\d:([<>a-z ]+)}/gm, '$1');
kmruiz marked this conversation as resolved.
Show resolved Hide resolved
}

describe('Create Search Index Modal', function () {
let onSubmitSpy: SinonSpy;
let onCloseSpy: SinonSpy;
Expand Down Expand Up @@ -88,4 +98,27 @@ describe('Create Search Index Modal', function () {
expect(onSubmitSpy).to.have.been.calledOnceWithExactly('default', {});
});
});

describe('templates', function () {
it('replaces the contents of the index editor when a template is selected', async function () {
const dropDown = screen
.getByText('Dynamic field mappings')
.closest('button')!;

userEvent.click(dropDown);

const staticFieldMappingOption = await screen.findByText(
'Static field mappings'
);
userEvent.click(staticFieldMappingOption);

await waitFor(() => {
const indexDef = getCodemirrorEditorValue('definition-of-search-index');

expect(indexDef).to.equal(
normalizedTemplateNamed('Static field mappings')
);
});
});
});
});
Loading
Loading