Skip to content

Commit

Permalink
Add tests to elasticsearch guide
Browse files Browse the repository at this point in the history
Co-authored-by: Joseph McElroy <joseph.mcelroy@elastic.co>
  • Loading branch information
Vadim Yakhin and joemcelroy committed Mar 29, 2022
1 parent c423cc5 commit 267e098
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 { setMockValues } from '../../../__mocks__/kea_logic';

import React from 'react';

import { shallow, ShallowWrapper } from 'enzyme';

import * as Languages from './languages';

import { ElasticsearchClientInstructions } from '.';

describe('Elasticsearch Client Instructions', () => {
let wrapper: ShallowWrapper;

afterEach(() => {
jest.clearAllMocks();
});

const setup = (language: string) => {
setMockValues({
cloud: {
cloudId: 'example-cloud-id',
},
});
wrapper = shallow(<ElasticsearchClientInstructions language={language} />);
};

describe('Displaying the right language options', () => {
it.concurrent.each([
['dotnet', Languages.ElasticsearchDotnet, false],
['go', Languages.ElasticsearchGo, true],
['java', Languages.ElasticsearchJava, false],
['javascript', Languages.ElasticsearchJavascript, true],
['php', Languages.ElasticsearchPhp, true],
['python', Languages.ElasticsearchPython, true],
['ruby', Languages.ElasticsearchRuby, true],
['rust', Languages.ElasticsearchRust, false],
])('%s', (language, Component, hasCloudIdProp) => {
setup(language);
expect(wrapper.find(Component)).toHaveLength(1);
expect(wrapper.find(Component).prop('cloudId')).toEqual(
hasCloudIdProp ? 'example-cloud-id' : undefined
);
});
});

it('does not display language for unrecognised language', () => {
setup('coffeescript');
expect(wrapper.isEmptyRender()).toEqual(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import { useValues } from 'kea';

import { KibanaLogic } from '../../../shared/kibana';

import { ElasticsearchDotnet } from './elasticsearch_dotnet';
import { ElasticsearchGo } from './elasticsearch_go';
import { ElasticsearchJava } from './elasticsearch_java';
import { ElasticsearchJavascript } from './elasticsearch_javascript';
import { ElasticsearchPhp } from './elasticsearch_php';
import { ElasticsearchPython } from './elasticsearch_python';
import { ElasticsearchRuby } from './elasticsearch_ruby';
import { ElasticsearchRust } from './elasticsearch_rust';
import {
ElasticsearchDotnet,
ElasticsearchGo,
ElasticsearchJava,
ElasticsearchJavascript,
ElasticsearchPhp,
ElasticsearchPython,
ElasticsearchRuby,
ElasticsearchRust,
} from './languages';

const useCloudId = (): string | undefined => {
const { cloud } = useValues(KibanaLogic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dedent from 'dedent';

import { EuiCodeBlock, EuiLink, EuiText, EuiSpacer } from '@elastic/eui';

import { docLinks } from '../../../shared/doc_links';
import { docLinks } from '../../../../shared/doc_links';

export const ElasticsearchDotnet: React.FC<{ cloudId?: string }> = ({ cloudId }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dedent from 'dedent';

import { EuiCodeBlock, EuiLink, EuiText, EuiSpacer } from '@elastic/eui';

import { docLinks } from '../../../shared/doc_links';
import { docLinks } from '../../../../shared/doc_links';

export const ElasticsearchGo: React.FC<{ cloudId?: string }> = ({ cloudId }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dedent from 'dedent';

import { EuiCodeBlock, EuiLink, EuiText, EuiSpacer } from '@elastic/eui';

import { docLinks } from '../../../shared/doc_links';
import { docLinks } from '../../../../shared/doc_links';

export const ElasticsearchJava: React.FC = () => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dedent from 'dedent';

import { EuiCodeBlock, EuiLink, EuiText, EuiSpacer } from '@elastic/eui';

import { docLinks } from '../../../shared/doc_links';
import { docLinks } from '../../../../shared/doc_links';

export const ElasticsearchJavascript: React.FC<{ cloudId?: string }> = ({ cloudId }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dedent from 'dedent';

import { EuiCodeBlock, EuiLink, EuiText, EuiSpacer } from '@elastic/eui';

import { docLinks } from '../../../shared/doc_links';
import { docLinks } from '../../../../shared/doc_links';

export const ElasticsearchPhp: React.FC<{ cloudId?: string }> = ({ cloudId }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dedent from 'dedent';

import { EuiCodeBlock, EuiLink, EuiText, EuiSpacer } from '@elastic/eui';

import { docLinks } from '../../../shared/doc_links';
import { docLinks } from '../../../../shared/doc_links';

export const ElasticsearchPython: React.FC<{ cloudId?: string }> = ({ cloudId }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dedent from 'dedent';

import { EuiCodeBlock, EuiLink, EuiText, EuiSpacer } from '@elastic/eui';

import { docLinks } from '../../../shared/doc_links';
import { docLinks } from '../../../../shared/doc_links';

export const ElasticsearchRuby: React.FC<{ cloudId?: string }> = ({ cloudId }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import dedent from 'dedent';

import { EuiCodeBlock, EuiLink, EuiText, EuiSpacer } from '@elastic/eui';

import { docLinks } from '../../../shared/doc_links';
import { docLinks } from '../../../../shared/doc_links';

export const ElasticsearchRust: React.FC = () => {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 { ElasticsearchDotnet } from './elasticsearch_dotnet';
import { ElasticsearchGo } from './elasticsearch_go';
import { ElasticsearchJava } from './elasticsearch_java';
import { ElasticsearchJavascript } from './elasticsearch_javascript';
import { ElasticsearchPhp } from './elasticsearch_php';
import { ElasticsearchPython } from './elasticsearch_python';
import { ElasticsearchRuby } from './elasticsearch_ruby';
import { ElasticsearchRust } from './elasticsearch_rust';

export {
ElasticsearchDotnet,
ElasticsearchGo,
ElasticsearchJava,
ElasticsearchJavascript,
ElasticsearchPhp,
ElasticsearchPython,
ElasticsearchRuby,
ElasticsearchRust,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 React from 'react';

import { shallow, ShallowWrapper } from 'enzyme';

import { EuiSteps, EuiSelect } from '@elastic/eui';

import { ElasticsearchClientInstructions } from '../elasticsearch_client_instructions';

import { ElasticsearchGuide } from './';

describe('Elasticsearch Guide Component', () => {
let wrapper: ShallowWrapper;

const setup = (param: string) => {
Object.defineProperty(window, 'location', {
value: { search: param },
writable: true,
});
wrapper = shallow(<ElasticsearchGuide />);
};

describe('Rendering Language option ', () => {
it('should use default language (java)', () => {
setup('');
const clientInstructionsElement = wrapper
.find(EuiSteps)
.dive()
.find(ElasticsearchClientInstructions);
expect(clientInstructionsElement.prop('language')).toBe('java');
});

it('should use language from param (ruby)', () => {
setup('?client=ruby');
const clientInstructionsElement = wrapper
.find(EuiSteps)
.dive()
.find(ElasticsearchClientInstructions);
expect(clientInstructionsElement.prop('language')).toBe('ruby');
});

it('should fallback to java if client not recognised', () => {
setup('?client=coffeescript');
const clientInstructionsElement = wrapper
.find(EuiSteps)
.dive()
.find(ElasticsearchClientInstructions);
expect(clientInstructionsElement.prop('language')).toBe('java');
});
});

describe('Changing Language option', () => {
it('should change the client instructions language prop when choosing another option', () => {
setup('');
const languageSelectElement = wrapper.find(EuiSteps).dive().find(EuiSelect);
languageSelectElement.simulate('change', { target: { value: 'ruby' } });

wrapper.update();

const clientInstructionsElement = wrapper
.find(EuiSteps)
.dive()
.find(ElasticsearchClientInstructions);
expect(clientInstructionsElement.prop('language')).toBe('ruby');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ export const ElasticsearchGuide: React.FC = () => {
{ value: 'rust', text: 'Rust' },
];

const client = queryString.parse(window.location.search).client;
const client = queryString.parse(window.location.search).client as string;
const languageExists = languages.some((language) => language.value === client);
const [selectedLanguage, setSelectedLanguage] = useState(
languageExists ? (client as string) : 'java'
);
const [selectedLanguage, setSelectedLanguage] = useState(languageExists ? client : 'java');

const basicSelectId = useGeneratedHtmlId({ prefix: 'languageSelect' });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { EuiEmptyPrompt } from '@elastic/eui';

import { WORKPLACE_SEARCH_PLUGIN } from '../../../../../common/constants';

import { ElasticsearchCard } from '../elasticsearch_card';
import { LicenseCallout } from '../license_callout';
import { ProductCard } from '../product_card';
import { SetupGuideCta } from '../setup_guide';
Expand All @@ -35,6 +36,7 @@ describe('ProductSelector', () => {
expect(wrapper.find(ProductCard)).toHaveLength(2);
expect(wrapper.find(SetupGuideCta)).toHaveLength(1);
expect(wrapper.find(LicenseCallout)).toHaveLength(0);
expect(wrapper.find(ElasticsearchCard)).toHaveLength(1);
});

it('renders the trial callout', () => {
Expand Down Expand Up @@ -97,6 +99,7 @@ describe('ProductSelector', () => {
expect(wrapper.find(EuiEmptyPrompt)).toHaveLength(1);
expect(wrapper.find(ProductCard)).toHaveLength(0);
expect(wrapper.find(LicenseCallout)).toHaveLength(0);
expect(wrapper.find(ElasticsearchCard)).toHaveLength(0);
});
});
});

0 comments on commit 267e098

Please sign in to comment.