Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jun 24, 2020
1 parent 5cf81d3 commit 02d45f6
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import React from 'react';
import { mount } from 'enzyme';

import { CallOut } from './callout';
import { ErrorMessage } from './types';
import { CallOut, CallOutProps } from './callout';

describe('Callout', () => {
const defaultProps = {
const defaultProps: CallOutProps = {
id: 'md5-hex',
type: 'primary' as NonNullable<ErrorMessage['errorType']>,
type: 'primary',
title: 'a tittle',
messages: [
{
Expand All @@ -26,6 +25,10 @@ describe('Callout', () => {
handleDismissCallout: jest.fn(),
};

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

it('It renders the callout', () => {
const wrapper = mount(<CallOut {...defaultProps} />);
expect(wrapper.find(`[data-test-subj="case-callout-md5-hex"]`).exists()).toBeTruthy();
Expand Down Expand Up @@ -60,11 +63,12 @@ describe('Callout', () => {
);
});

it('dismiss the button correctly', () => {
it('dismiss the callout correctly', () => {
const wrapper = mount(<CallOut {...defaultProps} messages={[]} />);
expect(wrapper.find(`[data-test-subj="callout-dismiss-md5-hex"]`).exists()).toBeTruthy();
wrapper.find(`button[data-test-subj="callout-dismiss-md5-hex"]`).simulate('click');
wrapper.update();
expect(defaultProps.handleDismissCallout).toHaveBeenCalledWith('md5-hex');

expect(defaultProps.handleDismissCallout).toHaveBeenCalledWith('md5-hex', 'primary');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import React, { memo, useCallback } from 'react';
import { ErrorMessage } from './types';
import * as i18n from './translations';

interface CallOutProps {
export interface CallOutProps {
id: string;
type: NonNullable<ErrorMessage['errorType']>;
title: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,25 @@ import { mount } from 'enzyme';
import { useMessagesStorage } from '../../../common/containers/local_storage/use_messages_storage';
import { TestProviders } from '../../../common/mock';
import { createCalloutId } from './helpers';
import { CaseCallOut } from '.';
import { CaseCallOut, CaseCallOutProps } from '.';

jest.mock('../../../common/containers/use_messages_storage');
jest.mock('../../../common/containers/local_storage/use_messages_storage');

const useSecurityLocalStorageMock = useMessagesStorage as jest.Mock;
const securityLocalStorageMock = {
getMessages: jest.fn(() => []),
addMessage: jest.fn(),
};

const defaultProps = {
title: 'hey title',
};

describe('CaseCallOut ', () => {
beforeEach(() => {
jest.clearAllMocks();
useSecurityLocalStorageMock.mockImplementation(() => securityLocalStorageMock);
});

it('Renders single message callout', () => {
const props = {
...defaultProps,
message: 'we have one message',
};

const wrapper = mount(
<TestProviders>
<CaseCallOut {...props} />
</TestProviders>
);

expect(wrapper.find(`[data-test-subj="callout-message-primary"]`).last().exists()).toBeTruthy();
});

it('Renders multi message callout', () => {
const props = {
...defaultProps,
message: 'we have one message',
it('renders a callout correctly', () => {
const props: CaseCallOutProps = {
title: 'hey title',
messages: [
{ id: 'message-one', title: 'title', description: <p>{'we have two messages'}</p> },
{ id: 'message-two', title: 'title', description: <p>{'for real'}</p> },
Expand All @@ -60,20 +40,19 @@ describe('CaseCallOut ', () => {
</TestProviders>
);

const id = createCalloutId(['message-one', 'message-two', 'generic-message-error']);

const id = createCalloutId(['message-one', 'message-two']);
expect(wrapper.find(`[data-test-subj="callout-messages-${id}"]`).last().exists()).toBeTruthy();
});

it('shows the correct type of callouts', () => {
const props = {
...defaultProps,
it('groups the messages correctly', () => {
const props: CaseCallOutProps = {
title: 'hey title',
messages: [
{
id: 'message-one',
title: 'title one',
description: <p>{'we have two messages'}</p>,
errorType: 'danger' as 'primary' | 'success' | 'warning' | 'danger',
errorType: 'danger',
},
{ id: 'message-two', title: 'title two', description: <p>{'for real'}</p> },
],
Expand All @@ -96,28 +75,32 @@ describe('CaseCallOut ', () => {
).toBeTruthy();
});

it('Dismisses callout', () => {
const props = {
...defaultProps,
message: 'we have one message',
it('dismisses the callout correctly', () => {
const props: CaseCallOutProps = {
title: 'hey title',
messages: [
{ id: 'message-one', title: 'title', description: <p>{'we have two messages'}</p> },
],
};
const wrapper = mount(
<TestProviders>
<CaseCallOut {...props} />
</TestProviders>
);

const id = createCalloutId(['generic-message-error']);
const id = createCalloutId(['message-one']);

expect(wrapper.find(`[data-test-subj="case-callout-${id}"]`).last().exists()).toBeTruthy();
wrapper.find(`[data-test-subj="callout-dismiss-${id}"]`).last().simulate('click');
expect(wrapper.find(`[data-test-subj="case-callout-${id}"]`).exists()).toBeFalsy();
});

it('persist the callout when dismissed', () => {
const props = {
...defaultProps,
message: 'we have one message',
it('persist the callout of type primary when dismissed', () => {
const props: CaseCallOutProps = {
title: 'hey title',
messages: [
{ id: 'message-one', title: 'title', description: <p>{'we have two messages'}</p> },
],
};

const wrapper = mount(
Expand All @@ -126,19 +109,21 @@ describe('CaseCallOut ', () => {
</TestProviders>
);

const id = createCalloutId(['generic-message-error']);
const id = createCalloutId(['message-one']);
expect(securityLocalStorageMock.getMessages).toHaveBeenCalledWith('case');
wrapper.find(`[data-test-subj="callout-dismiss-${id}"]`).last().simulate('click');
expect(securityLocalStorageMock.addMessage).toHaveBeenCalledWith('case', id);
});

it('do not show the callout if is in the localStorage', () => {
const props = {
...defaultProps,
message: 'we have one message',
const props: CaseCallOutProps = {
title: 'hey title',
messages: [
{ id: 'message-one', title: 'title', description: <p>{'we have two messages'}</p> },
],
};

const id = createCalloutId(['generic-message-error']);
const id = createCalloutId(['message-one']);

useSecurityLocalStorageMock.mockImplementation(() => ({
...securityLocalStorageMock,
Expand All @@ -155,8 +140,8 @@ describe('CaseCallOut ', () => {
});

it('do not persist a callout of type danger', () => {
const props = {
...defaultProps,
const props: CaseCallOutProps = {
title: 'hey title',
messages: [
{
id: 'message-one',
Expand All @@ -167,18 +152,21 @@ describe('CaseCallOut ', () => {
],
};

mount(
const wrapper = mount(
<TestProviders>
<CaseCallOut {...props} />
</TestProviders>
);

const id = createCalloutId(['message-one']);
wrapper.find(`button[data-test-subj="callout-dismiss-${id}"]`).simulate('click');
wrapper.update();
expect(securityLocalStorageMock.addMessage).not.toHaveBeenCalled();
});

it('do not persist a callout of type warning', () => {
const props = {
...defaultProps,
const props: CaseCallOutProps = {
title: 'hey title',
messages: [
{
id: 'message-one',
Expand All @@ -189,18 +177,21 @@ describe('CaseCallOut ', () => {
],
};

mount(
const wrapper = mount(
<TestProviders>
<CaseCallOut {...props} />
</TestProviders>
);

const id = createCalloutId(['message-one']);
wrapper.find(`button[data-test-subj="callout-dismiss-${id}"]`).simulate('click');
wrapper.update();
expect(securityLocalStorageMock.addMessage).not.toHaveBeenCalled();
});

it('do not persist a callout of type success', () => {
const props = {
...defaultProps,
const props: CaseCallOutProps = {
title: 'hey title',
messages: [
{
id: 'message-one',
Expand All @@ -211,12 +202,15 @@ describe('CaseCallOut ', () => {
],
};

mount(
const wrapper = mount(
<TestProviders>
<CaseCallOut {...props} />
</TestProviders>
);

const id = createCalloutId(['message-one']);
wrapper.find(`button[data-test-subj="callout-dismiss-${id}"]`).simulate('click');
wrapper.update();
expect(securityLocalStorageMock.addMessage).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createCalloutId } from './helpers';

export * from './helpers';

interface CaseCallOutProps {
export interface CaseCallOutProps {
title: string;
messages?: ErrorMessage[];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('usePushToService', () => {
await waitForNextUpdate();
const errorsMsg = result.current.pushCallouts?.props.messages;
expect(errorsMsg).toHaveLength(1);
expect(errorsMsg[0].title).toEqual(getLicenseError().title);
expect(errorsMsg[0].id).toEqual('license-error');
});
});

Expand All @@ -132,7 +132,7 @@ describe('usePushToService', () => {
await waitForNextUpdate();
const errorsMsg = result.current.pushCallouts?.props.messages;
expect(errorsMsg).toHaveLength(1);
expect(errorsMsg[0].title).toEqual(getKibanaConfigError().title);
expect(errorsMsg[0].id).toEqual('kibana-config-error');
});
});

Expand All @@ -152,7 +152,7 @@ describe('usePushToService', () => {
await waitForNextUpdate();
const errorsMsg = result.current.pushCallouts?.props.messages;
expect(errorsMsg).toHaveLength(1);
expect(errorsMsg[0].title).toEqual(i18n.PUSH_DISABLE_BY_NO_CONFIG_TITLE);
expect(errorsMsg[0].id).toEqual('connector-missing-error');
});
});

Expand All @@ -171,7 +171,7 @@ describe('usePushToService', () => {
await waitForNextUpdate();
const errorsMsg = result.current.pushCallouts?.props.messages;
expect(errorsMsg).toHaveLength(1);
expect(errorsMsg[0].title).toEqual(i18n.PUSH_DISABLE_BY_NO_CASE_CONFIG_TITLE);
expect(errorsMsg[0].id).toEqual('connector-not-selected-error');
});
});

Expand All @@ -191,7 +191,7 @@ describe('usePushToService', () => {
await waitForNextUpdate();
const errorsMsg = result.current.pushCallouts?.props.messages;
expect(errorsMsg).toHaveLength(1);
expect(errorsMsg[0].title).toEqual(i18n.PUSH_DISABLE_BY_NO_CASE_CONFIG_TITLE);
expect(errorsMsg[0].id).toEqual('connector-deleted-error');
});
});

Expand All @@ -212,7 +212,7 @@ describe('usePushToService', () => {
await waitForNextUpdate();
const errorsMsg = result.current.pushCallouts?.props.messages;
expect(errorsMsg).toHaveLength(1);
expect(errorsMsg[0].title).toEqual(i18n.PUSH_DISABLE_BY_NO_CASE_CONFIG_TITLE);
expect(errorsMsg[0].id).toEqual('connector-deleted-error');
});
});

Expand All @@ -231,7 +231,7 @@ describe('usePushToService', () => {
await waitForNextUpdate();
const errorsMsg = result.current.pushCallouts?.props.messages;
expect(errorsMsg).toHaveLength(1);
expect(errorsMsg[0].title).toEqual(i18n.PUSH_DISABLE_BECAUSE_CASE_CLOSED_TITLE);
expect(errorsMsg[0].id).toEqual('closed-case-push-error');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,6 @@ export const usePushToService = ({
/>
),
},
{
id: 'connector-deleted-error',
title: i18n.PUSH_DISABLE_BY_NO_CASE_CONFIG_TITLE,
description: (
<FormattedMessage
defaultMessage="The connector used to send updates to external service has been deleted. To update cases in external systems, select a different connector or create a new one."
id="xpack.securitySolution.case.caseView.pushToServiceDisableByInvalidConnector"
/>
),
errorType: 'danger',
},
];
} else if (caseConnectorId === 'none' && !loadingLicense) {
errors = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useKibana } from '../../lib/kibana';
import { createUseKibanaMock } from '../../mock/kibana_react';
import { useMessagesStorage, UseMessagesStorage } from './use_messages_storage';

jest.mock('../lib/kibana');
jest.mock('../../lib/kibana');
const useKibanaMock = useKibana as jest.Mock;

describe('useLocalStorage', () => {
Expand Down

0 comments on commit 02d45f6

Please sign in to comment.