Skip to content

Commit

Permalink
[App Search] Allow for query parameter to indicate ingestion mechanis…
Browse files Browse the repository at this point in the history
…m for new engines (#115188)
  • Loading branch information
orhantoy authored Oct 18, 2021
1 parent 84df569 commit 672b592
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
* 2.0.
*/

import '../../../__mocks__/react_router';
import '../../../__mocks__/shallow_useeffect.mock';
import { setMockActions } from '../../../__mocks__/kea_logic';
import '../../__mocks__/engine_logic.mock';

import React from 'react';
import { useLocation } from 'react-router-dom';

import { shallow } from 'enzyme';

Expand Down Expand Up @@ -60,4 +63,20 @@ describe('DocumentCreationButtons', () => {

expect(wrapper.find(EuiCardTo).prop('to')).toEqual('/engines/some-engine/crawler');
});

it('calls openDocumentCreation("file") if ?method=json', () => {
const search = '?method=json';
(useLocation as jest.Mock).mockImplementationOnce(() => ({ search }));

shallow(<DocumentCreationButtons />);
expect(actions.openDocumentCreation).toHaveBeenCalledWith('file');
});

it('calls openDocumentCreation("api") if ?method=api', () => {
const search = '?method=api';
(useLocation as jest.Mock).mockImplementationOnce(() => ({ search }));

shallow(<DocumentCreationButtons />);
expect(actions.openDocumentCreation).toHaveBeenCalledWith('api');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
* 2.0.
*/

import React from 'react';
import React, { useEffect } from 'react';

import { useLocation } from 'react-router-dom';

import { Location } from 'history';
import { useActions } from 'kea';

import {
Expand All @@ -22,6 +25,7 @@ import {
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';

import { parseQueryParams } from '../../../shared/query_params';
import { EuiCardTo } from '../../../shared/react_router_helpers';
import { DOCS_PREFIX, ENGINE_CRAWLER_PATH } from '../../routes';
import { generateEnginePath } from '../engine';
Expand All @@ -35,6 +39,20 @@ interface Props {
export const DocumentCreationButtons: React.FC<Props> = ({ disabled = false }) => {
const { openDocumentCreation } = useActions(DocumentCreationLogic);

const { search } = useLocation() as Location;
const { method } = parseQueryParams(search);

useEffect(() => {
switch (method) {
case 'json':
openDocumentCreation('file');
break;
case 'api':
openDocumentCreation('api');
break;
}
}, []);

const crawlerLink = generateEnginePath(ENGINE_CRAWLER_PATH);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@
* 2.0.
*/

import '../../../__mocks__/react_router';
import '../../../__mocks__/shallow_useeffect.mock';
import { setMockActions, setMockValues } from '../../../__mocks__/kea_logic';

import React from 'react';
import { useLocation } from 'react-router-dom';

import { shallow } from 'enzyme';

import { EngineCreation } from './';

describe('EngineCreation', () => {
const DEFAULT_VALUES = {
ingestionMethod: '',
isLoading: false,
name: '',
rawName: '',
language: 'Universal',
};

const MOCK_ACTIONS = {
setIngestionMethod: jest.fn(),
setRawName: jest.fn(),
setLanguage: jest.fn(),
submitEngine: jest.fn(),
Expand All @@ -38,6 +43,14 @@ describe('EngineCreation', () => {
expect(wrapper.find('[data-test-subj="EngineCreation"]')).toHaveLength(1);
});

it('EngineCreationLanguageInput calls setIngestionMethod on mount', () => {
const search = '?method=crawler';
(useLocation as jest.Mock).mockImplementationOnce(() => ({ search }));

shallow(<EngineCreation />);
expect(MOCK_ACTIONS.setIngestionMethod).toHaveBeenCalledWith('crawler');
});

it('EngineCreationForm calls submitEngine on form submit', () => {
const wrapper = shallow(<EngineCreation />);
const simulatedEvent = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
* 2.0.
*/

import React from 'react';
import React, { useEffect } from 'react';

import { useLocation } from 'react-router-dom';

import { Location } from 'history';
import { useActions, useValues } from 'kea';

import {
Expand All @@ -22,6 +25,7 @@ import {
EuiButton,
} from '@elastic/eui';

import { parseQueryParams } from '../../../shared/query_params';
import { ENGINES_TITLE } from '../engines';
import { AppSearchPageTemplate } from '../layout';

Expand All @@ -39,8 +43,18 @@ import {
import { EngineCreationLogic } from './engine_creation_logic';

export const EngineCreation: React.FC = () => {
const { search } = useLocation() as Location;
const { method } = parseQueryParams(search);

const { name, rawName, language, isLoading } = useValues(EngineCreationLogic);
const { setLanguage, setRawName, submitEngine } = useActions(EngineCreationLogic);
const { setIngestionMethod, setLanguage, setRawName, submitEngine } =
useActions(EngineCreationLogic);

useEffect(() => {
if (typeof method === 'string') {
setIngestionMethod(method);
}
}, []);

return (
<AppSearchPageTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('EngineCreationLogic', () => {
const { flashSuccessToast, flashAPIErrors } = mockFlashMessageHelpers;

const DEFAULT_VALUES = {
ingestionMethod: '',
isLoading: false,
name: '',
rawName: '',
Expand All @@ -35,6 +36,17 @@ describe('EngineCreationLogic', () => {
});

describe('actions', () => {
describe('setIngestionMethod', () => {
it('sets ingestion method to the provided value', () => {
mount();
EngineCreationLogic.actions.setIngestionMethod('crawler');
expect(EngineCreationLogic.values).toEqual({
...DEFAULT_VALUES,
ingestionMethod: 'crawler',
});
});
});

describe('setLanguage', () => {
it('sets language to the provided value', () => {
mount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@
* 2.0.
*/

import { generatePath } from 'react-router-dom';

import { kea, MakeLogicType } from 'kea';

import { flashAPIErrors, flashSuccessToast } from '../../../shared/flash_messages';
import { HttpLogic } from '../../../shared/http';
import { KibanaLogic } from '../../../shared/kibana';
import { ENGINE_PATH } from '../../routes';
import { formatApiName } from '../../utils/format_api_name';

import { DEFAULT_LANGUAGE, ENGINE_CREATION_SUCCESS_MESSAGE } from './constants';
import { getRedirectToAfterEngineCreation } from './utils';

interface EngineCreationActions {
onEngineCreationSuccess(): void;
setIngestionMethod(method: string): { method: string };
setLanguage(language: string): { language: string };
setRawName(rawName: string): { rawName: string };
submitEngine(): void;
onSubmitError(): void;
}

interface EngineCreationValues {
ingestionMethod: string;
isLoading: boolean;
language: string;
name: string;
Expand All @@ -36,12 +36,19 @@ export const EngineCreationLogic = kea<MakeLogicType<EngineCreationValues, Engin
path: ['enterprise_search', 'app_search', 'engine_creation_logic'],
actions: {
onEngineCreationSuccess: true,
setIngestionMethod: (method) => ({ method }),
setLanguage: (language) => ({ language }),
setRawName: (rawName) => ({ rawName }),
submitEngine: true,
onSubmitError: true,
},
reducers: {
ingestionMethod: [
'',
{
setIngestionMethod: (_, { method }) => method,
},
],
isLoading: [
false,
{
Expand Down Expand Up @@ -81,12 +88,12 @@ export const EngineCreationLogic = kea<MakeLogicType<EngineCreationValues, Engin
}
},
onEngineCreationSuccess: () => {
const { name } = values;
const { ingestionMethod, name } = values;
const { navigateToUrl } = KibanaLogic.values;
const enginePath = generatePath(ENGINE_PATH, { engineName: name });
const toUrl = getRedirectToAfterEngineCreation({ ingestionMethod, engineName: name });

flashSuccessToast(ENGINE_CREATION_SUCCESS_MESSAGE(name));
navigateToUrl(enginePath);
navigateToUrl(toUrl);
},
}),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { getRedirectToAfterEngineCreation } from './utils';

describe('getRedirectToAfterEngineCreation', () => {
it('returns crawler path when ingestionMethod is crawler', () => {
const engineName = 'elastic';
const redirectTo = getRedirectToAfterEngineCreation({ ingestionMethod: 'crawler', engineName });
expect(redirectTo).toEqual('/engines/elastic/crawler');
});

it('returns engine overview path when there is no ingestionMethod', () => {
const engineName = 'elastic';
const redirectTo = getRedirectToAfterEngineCreation({ ingestionMethod: '', engineName });
expect(redirectTo).toEqual('/engines/elastic');
});

it('returns engine overview path with query param when there is ingestionMethod', () => {
const engineName = 'elastic';
const redirectTo = getRedirectToAfterEngineCreation({ ingestionMethod: 'api', engineName });
expect(redirectTo).toEqual('/engines/elastic?method=api');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { generatePath } from 'react-router-dom';

import { ENGINE_CRAWLER_PATH, ENGINE_PATH } from '../../routes';

export const getRedirectToAfterEngineCreation = ({
ingestionMethod,
engineName,
}: {
ingestionMethod?: string;
engineName: string;
}): string => {
if (ingestionMethod === 'crawler') {
return generatePath(ENGINE_CRAWLER_PATH, { engineName });
}

let enginePath = generatePath(ENGINE_PATH, { engineName });
if (ingestionMethod) {
enginePath += `?method=${encodeURIComponent(ingestionMethod)}`;
}

return enginePath;
};

0 comments on commit 672b592

Please sign in to comment.