Skip to content

Commit

Permalink
Review implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jun 26, 2020
1 parent 47ee05f commit 6d593b5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,36 @@ describe('Callout', () => {
expect(wrapper.find(`[data-test-subj="callout-messages-md5-hex"]`).exists()).toBeFalsy();
});

it('transform the button color correctly', () => {
let wrapper = mount(<CallOut {...defaultProps} />);
expect(wrapper.find(`[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('color')).toBe(
'primary'
);
it('transform the button color correctly - primary', () => {
const wrapper = mount(<CallOut {...defaultProps} />);
const className =
wrapper.find(`button[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('className') ??
'';
expect(className.includes('euiButton--primary')).toBeTruthy();
});

wrapper = mount(<CallOut {...defaultProps} type={'success'} />);
expect(wrapper.find(`[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('color')).toBe(
'secondary'
);
it('transform the button color correctly - success', () => {
const wrapper = mount(<CallOut {...defaultProps} type={'success'} />);
const className =
wrapper.find(`button[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('className') ??
'';
expect(className.includes('euiButton--secondary')).toBeTruthy();
});

it('transform the button color correctly - warning', () => {
const wrapper = mount(<CallOut {...defaultProps} type={'warning'} />);
const className =
wrapper.find(`button[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('className') ??
'';
expect(className.includes('euiButton--warning')).toBeTruthy();
});

wrapper = mount(<CallOut {...defaultProps} type={'danger'} />);
expect(wrapper.find(`[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('color')).toBe(
'danger'
);
it('transform the button color correctly - danger', () => {
const wrapper = mount(<CallOut {...defaultProps} type={'danger'} />);
const className =
wrapper.find(`button[data-test-subj="callout-dismiss-md5-hex"]`).first().prop('className') ??
'';
expect(className.includes('euiButton--danger')).toBeTruthy();
});

it('dismiss the callout correctly', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('CaseCallOut ', () => {
id: 'message-one',
title: 'title one',
description: <p>{'we have two messages'}</p>,
errorType: 'danger' as 'primary' | 'success' | 'warning' | 'danger',
errorType: 'danger',
},
],
};
Expand All @@ -172,7 +172,7 @@ describe('CaseCallOut ', () => {
id: 'message-one',
title: 'title one',
description: <p>{'we have two messages'}</p>,
errorType: 'warning' as 'primary' | 'success' | 'warning' | 'danger',
errorType: 'warning',
},
],
};
Expand All @@ -197,7 +197,7 @@ describe('CaseCallOut ', () => {
id: 'message-one',
title: 'title one',
description: <p>{'we have two messages'}</p>,
errorType: 'success' as 'primary' | 'success' | 'warning' | 'danger',
errorType: 'success',
},
],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { EuiSpacer } from '@elastic/eui';
import React, { memo, useCallback, useState } from 'react';
import React, { memo, useCallback, useState, useMemo } from 'react';

import { useMessagesStorage } from '../../../common/containers/local_storage/use_messages_storage';
import { CallOut } from './callout';
Expand All @@ -32,12 +32,18 @@ interface CalloutVisibility {

const CaseCallOutComponent = ({ title, messages = [] }: CaseCallOutProps) => {
const { getMessages, addMessage } = useMessagesStorage();
const dismissedCallouts = getMessages('case').reduce<CalloutVisibility>(
(acc, id) => ({
...acc,
[id]: false,
}),
{}

const caseMessages = useMemo(() => getMessages('case'), [getMessages]);
const dismissedCallouts = useMemo(
() =>
caseMessages.reduce<CalloutVisibility>(
(acc, id) => ({
...acc,
[id]: false,
}),
{}
),
[caseMessages]
);

const [calloutVisibility, setCalloutVisibility] = useState(dismissedCallouts);
Expand All @@ -51,18 +57,22 @@ const CaseCallOutComponent = ({ title, messages = [] }: CaseCallOutProps) => {
[setCalloutVisibility, addMessage]
);

const groupedByTypeErrorMessages = messages.reduce<GroupByTypeMessages>(
(acc: GroupByTypeMessages, currentMessage: ErrorMessage) => {
const type = currentMessage.errorType == null ? 'primary' : currentMessage.errorType;
return {
...acc,
[type]: {
messagesId: [...(acc[type]?.messagesId ?? []), currentMessage.id],
messages: [...(acc[type]?.messages ?? []), currentMessage],
const groupedByTypeErrorMessages = useMemo(
() =>
messages.reduce<GroupByTypeMessages>(
(acc: GroupByTypeMessages, currentMessage: ErrorMessage) => {
const type = currentMessage.errorType == null ? 'primary' : currentMessage.errorType;
return {
...acc,
[type]: {
messagesId: [...(acc[type]?.messagesId ?? []), currentMessage.id],
messages: [...(acc[type]?.messages ?? []), currentMessage],
},
};
},
};
},
{} as GroupByTypeMessages
{} as GroupByTypeMessages
),
[messages]
);

return (
Expand Down

0 comments on commit 6d593b5

Please sign in to comment.