Skip to content

Commit

Permalink
Updated jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron Hulcher committed May 9, 2022
1 parent 6cbc279 commit 2ebac49
Show file tree
Hide file tree
Showing 22 changed files with 419 additions and 486 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
SOURCES_PATH,
PRIVATE_SOURCES_PATH,
SOURCE_DETAILS_PATH,
getAddPath,
getEditPath,
} from './routes';

const TestComponent = ({ id, isOrg }: { id: string; isOrg?: boolean }) => {
Expand Down Expand Up @@ -86,3 +88,32 @@ describe('getReindexJobRoute', () => {
);
});
});

describe('getAddPath', () => {
it('should handle a service type', () => {
expect(getAddPath('share_point')).toEqual('/sources/add/share_point');
});

it('should should handle an external service type with no base service type', () => {
expect(getAddPath('external')).toEqual('/sources/add/external');
});

it('should should handle an external service type with a base service type', () => {
expect(getAddPath('external', 'share_point')).toEqual('/sources/add/share_point/external');
});
it('should should handle a custom service type with no base service type', () => {
expect(getAddPath('external')).toEqual('/sources/add/external');
});

it('should should handle a custom service type with a base service type', () => {
expect(getAddPath('custom', 'share_point_server')).toEqual(
'/sources/add/share_point_server/custom'
);
});
});

describe('getEditPath', () => {
it('should handle a service type', () => {
expect(getEditPath('share_point')).toEqual('/settings/connectors/share_point/edit');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import '../../../../../../__mocks__/shallow_useeffect.mock';
import { setMockValues } from '../../../../../../__mocks__/kea_logic';
import { mockUseParams } from '../../../../../../__mocks__/react_router';
import { sourceConfigData } from '../../../../../__mocks__/content_sources.mock';

import React from 'react';
Expand All @@ -17,52 +18,55 @@ import {
WorkplaceSearchPageTemplate,
PersonalDashboardLayout,
} from '../../../../../components/layout';
import { staticSourceData } from '../../../source_data';

import { AddCustomSource } from './add_custom_source';
import { AddCustomSourceSteps } from './add_custom_source_logic';
import { ConfigureCustom } from './configure_custom';
import { SaveCustom } from './save_custom';

describe('AddCustomSource', () => {
const props = {
sourceData: staticSourceData[0],
initialValues: undefined,
};

const values = {
sourceConfigData,
isOrganization: true,
};

beforeEach(() => {
setMockValues({ ...values });
mockUseParams.mockReturnValue({ baseServiceType: 'share_point_server' });
});

it('renders', () => {
const wrapper = shallow(<AddCustomSource {...props} />);
const wrapper = shallow(<AddCustomSource />);

expect(wrapper.find(WorkplaceSearchPageTemplate)).toHaveLength(1);
});

it('returns null if there is no matching source data for the service type', () => {
mockUseParams.mockReturnValue({ baseServiceType: 'doesnt_exist' });

const wrapper = shallow(<AddCustomSource />);

expect(wrapper.isEmptyRender()).toBe(true);
});

it('should show correct layout for personal dashboard', () => {
setMockValues({ isOrganization: false });
const wrapper = shallow(<AddCustomSource {...props} />);
const wrapper = shallow(<AddCustomSource />);

expect(wrapper.find(WorkplaceSearchPageTemplate)).toHaveLength(0);
expect(wrapper.find(PersonalDashboardLayout)).toHaveLength(1);
});

it('should show Configure Custom for custom configuration step', () => {
setMockValues({ currentStep: AddCustomSourceSteps.ConfigureCustomStep });
const wrapper = shallow(<AddCustomSource {...props} />);
const wrapper = shallow(<AddCustomSource />);

expect(wrapper.find(ConfigureCustom)).toHaveLength(1);
});

it('should show Save Custom for save custom step', () => {
setMockValues({ currentStep: AddCustomSourceSteps.SaveCustomStep });
const wrapper = shallow(<AddCustomSource {...props} />);
const wrapper = shallow(<AddCustomSource />);

expect(wrapper.find(SaveCustom)).toHaveLength(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const AddCustomSource: React.FC = () => {
const Layout = isOrganization ? WorkplaceSearchPageTemplate : PersonalDashboardLayout;

return (
<Layout pageChrome={[NAV.SOURCES, NAV.ADD_SOURCE, sourceData.name || '...']}>
<Layout pageChrome={[NAV.SOURCES, NAV.ADD_SOURCE, sourceData.name]}>
{currentStep === AddCustomSourceSteps.ConfigureCustomStep && <ConfigureCustom />}
{currentStep === AddCustomSourceSteps.SaveCustomStep && <SaveCustom />}
</Layout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,25 @@ import { sourceConfigData } from '../../../../../__mocks__/content_sources.mock'

import { nextTick } from '@kbn/test-jest-helpers';

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

jest.mock('../../../../../app_logic', () => ({
AppLogic: { values: { isOrganization: true } },
}));
import { AppLogic } from '../../../../../app_logic';

import { SOURCE_NAMES } from '../../../../../constants';
import { CustomSource, SourceDataItem } from '../../../../../types';
import { CustomSource } from '../../../../../types';

import { AddCustomSourceLogic, AddCustomSourceSteps } from './add_custom_source_logic';

const CUSTOM_SOURCE_DATA_ITEM: SourceDataItem = {
name: SOURCE_NAMES.CUSTOM,
serviceType: 'custom',
configuration: {
isPublicKey: false,
hasOauthRedirect: false,
needsBaseUrl: false,
documentationUrl: docLinks.workplaceSearchCustomSources,
applicationPortalUrl: '',
},
accountContextOnly: false,
};

const DEFAULT_VALUES = {
currentStep: AddCustomSourceSteps.ConfigureCustomStep,
buttonLoading: false,
customSourceNameValue: '',
newCustomSource: {} as CustomSource,
sourceData: CUSTOM_SOURCE_DATA_ITEM,
};

const MOCK_PROPS = { initialValue: '', sourceData: CUSTOM_SOURCE_DATA_ITEM };
const MOCK_PROPS = {};

const MOCK_NAME = 'name';

Expand Down Expand Up @@ -150,11 +134,7 @@ describe('AddCustomSourceLogic', () => {
customSourceNameValue: MOCK_NAME,
},
{
...MOCK_PROPS,
sourceData: {
...CUSTOM_SOURCE_DATA_ITEM,
serviceType: 'sharepoint-server',
},
baseServiceType: 'share_point_server',
}
);

Expand All @@ -164,7 +144,7 @@ describe('AddCustomSourceLogic', () => {
body: JSON.stringify({
service_type: 'custom',
name: MOCK_NAME,
base_service_type: 'sharepoint-server',
base_service_type: 'share_point_server',
}),
});
});
Expand Down Expand Up @@ -199,10 +179,7 @@ describe('AddCustomSourceLogic', () => {
},
{
...MOCK_PROPS,
sourceData: {
...CUSTOM_SOURCE_DATA_ITEM,
serviceType: 'sharepoint-server',
},
baseServiceType: 'share_point_server',
}
);

Expand All @@ -214,7 +191,7 @@ describe('AddCustomSourceLogic', () => {
body: JSON.stringify({
service_type: 'custom',
name: MOCK_NAME,
base_service_type: 'sharepoint-server',
base_service_type: 'share_point_server',
}),
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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

import React from 'react';
Expand All @@ -19,24 +20,15 @@ import {
WorkplaceSearchPageTemplate,
PersonalDashboardLayout,
} from '../../../../../components/layout';
import { staticSourceData } from '../../../source_data';

import { ExternalConnectorConfig } from './external_connector_config';
import { ExternalConnectorFormFields } from './external_connector_form_fields';

describe('ExternalConnectorConfig', () => {
const goBack = jest.fn();
const onDeleteConfig = jest.fn();
const setExternalConnectorApiKey = jest.fn();
const setExternalConnectorUrl = jest.fn();
const saveExternalConnectorConfig = jest.fn();

const props = {
sourceData: staticSourceData[0],
goBack,
onDeleteConfig,
};

const values = {
sourceConfigData,
buttonLoading: false,
Expand All @@ -48,37 +40,47 @@ describe('ExternalConnectorConfig', () => {
};

beforeEach(() => {
jest.clearAllMocks();
setMockActions({
setExternalConnectorApiKey,
setExternalConnectorUrl,
saveExternalConnectorConfig,
});
setMockValues({ ...values });
mockUseParams.mockReturnValue({});
});

it('returns null if there is no matching source data for the service type', () => {
mockUseParams.mockReturnValue({ baseServiceType: 'doesnt_exist' });

const wrapper = shallow(<ExternalConnectorConfig />);

expect(wrapper.isEmptyRender()).toBe(true);
});

it('renders', () => {
const wrapper = shallow(<ExternalConnectorConfig {...props} />);
const wrapper = shallow(<ExternalConnectorConfig />);

expect(wrapper.find(EuiSteps)).toHaveLength(1);
expect(wrapper.find(EuiSteps).dive().find(ExternalConnectorFormFields)).toHaveLength(1);
});

it('renders organizstion layout', () => {
const wrapper = shallow(<ExternalConnectorConfig {...props} />);
const wrapper = shallow(<ExternalConnectorConfig />);

expect(wrapper.find(WorkplaceSearchPageTemplate)).toHaveLength(1);
});

it('should show correct layout for personal dashboard', () => {
setMockValues({ ...values, isOrganization: false });
const wrapper = shallow(<ExternalConnectorConfig {...props} />);
const wrapper = shallow(<ExternalConnectorConfig />);

expect(wrapper.find(WorkplaceSearchPageTemplate)).toHaveLength(0);
expect(wrapper.find(PersonalDashboardLayout)).toHaveLength(1);
});

it('handles form submission', () => {
const wrapper = shallow(<ExternalConnectorConfig {...props} />);
const wrapper = shallow(<ExternalConnectorConfig />);

const preventDefault = jest.fn();
wrapper.find('form').simulate('submit', { preventDefault });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const ExternalConnectorConfig: React.FC = () => {
const Layout = isOrganization ? WorkplaceSearchPageTemplate : PersonalDashboardLayout;

return (
<Layout pageChrome={[NAV.SOURCES, NAV.ADD_SOURCE, name || '...']} isLoading={false}>
<Layout pageChrome={[NAV.SOURCES, NAV.ADD_SOURCE, name]} isLoading={false}>
{header}
<EuiSpacer size="l" />
<ExternalConnectorDocumentation name={name} documentationUrl={documentationUrl} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ describe('ExternalConnectorLogic', () => {
formDisabled: true,
externalConnectorUrl: '',
externalConnectorApiKey: '',
sourceConfigData: {
name: '',
categories: [],
},
urlValid: true,
showInsecureUrlCallout: false,
insecureUrl: true,
Expand All @@ -52,7 +48,6 @@ describe('ExternalConnectorLogic', () => {
formDisabled: false,
insecureUrl: false,
dataLoading: false,
sourceConfigData,
};

beforeEach(() => {
Expand Down Expand Up @@ -87,7 +82,6 @@ describe('ExternalConnectorLogic', () => {
it('saves the source config', () => {
expect(ExternalConnectorLogic.values).toEqual({
...DEFAULT_VALUES_SUCCESS,
sourceConfigData,
});
});

Expand All @@ -104,7 +98,6 @@ describe('ExternalConnectorLogic', () => {
...DEFAULT_VALUES_SUCCESS,
externalConnectorUrl: '',
insecureUrl: true,
sourceConfigData: newSourceConfigData,
});
});
it('sets undefined api key to empty string', () => {
Expand All @@ -119,7 +112,6 @@ describe('ExternalConnectorLogic', () => {
expect(ExternalConnectorLogic.values).toEqual({
...DEFAULT_VALUES_SUCCESS,
externalConnectorApiKey: '',
sourceConfigData: newSourceConfigData,
});
});
});
Expand Down
Loading

0 comments on commit 2ebac49

Please sign in to comment.