Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit test for Breakdown Card Component #4852

Merged
merged 1 commit into from Apr 1, 2020

Conversation

bipuladh
Copy link
Contributor

  • Unit tests for BreakDown Card Body and BreakdownCard Chart

@openshift-ci-robot openshift-ci-robot added component/ceph Related to ceph-storage-plugin approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Mar 30, 2020
@openshift openshift deleted a comment from bipuladh Mar 30, 2020
Copy link
Contributor

@vojtechszocs vojtechszocs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React / Enzyme component unit testing tips:

For example, when testing Redux connected components:

let wrapper: ReactWrapper<YourComponentProps>;

beforeEach(() => {
  wrapper = mount(
    <YourComponent />,
    {
      wrappingComponent: ({ children }) => <Provider store={store}>{children}</Provider>,
    },
  );
});

Another example, using official React test renderer:

import * as TestRenderer from 'react-test-renderer';

it('should render correctly', () => {
  expect(TestRenderer.create(<YourComponent />).toJSON()).toMatchSnapshot();
});

Besides React components, you can snapshot-test any serializable objects which are worth checking for potential regressions (unwanted changes).

@@ -0,0 +1,48 @@
import { humanizeBinaryBytes } from '@console/internal/components/utils';

export const mockData = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest naming it breakdownData so that when we import it in other module, we don't end up with a rather generic mockData variable or import { mockData as breakdownData } alias.

const top5MetricsStats = getStackChartStats(mockData.top5, mockData.humanize);

describe('<BreakdownCardBody>', () => {
const wrapper: ShallowWrapper<BreakdownBodyProps> = shallow(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest re-creating Enzyme wrapper for each test case, rather than sharing the same instance.

let wrapper: ShallowWrapper<BreakdownBodyProps>;

beforeEach(() => {
  wrapper = shallow( /* your component */ );
});

const breakdownChart = wrapper.find(BreakdownChart);
expect(breakdownChart.exists()).toBe(true);
expect(breakdownChart.props().data.length).toBe(7);
// Last is poopped if capacityTotal is available(7 - 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Last is poopped if capacityTotal is available(7 - 1)
// Last is popped if capacityTotal is available(7 - 1)

});

it('Hides available capacity text, legend, stack', () => {
wrapper.setProps({ capacityTotal: null });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mutates the wrapper.

If all test cases share the same instance, this makes them dependent in terms of order of execution, which is generally a bad thing.

Test cases should not depend on each other. That's why it's best to re-create wrapper instance via beforeEach.

const legends = getLegends(chartData);

describe('<BreakdownChart>', () => {
const wrapper: ShallowWrapper<BreakdownChartProps> = shallow(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest re-creating wrapper instance via beforeEach as discussed above.

});

describe('<LinkableLegend>', () => {
const wrapper: ShallowWrapper<LinkableLegendProps> = shallow(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest re-creating wrapper instance via beforeEach as discussed above.

/>
</Tooltip>
);
if (datum.name === OTHER || datum.name === CLUSTERWIDE) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This differs from original, is that intentional?

if (datum.name[0] === OTHER || datum.name[0] === CLUSTERWIDE)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The original is erroneous.


export const BreakdownChart: React.FC<BreakdownChartProps> = ({
data,
legends,
metricModel,
ocsVersion,
}) => {
const chartData = data.map((d: StackDataPoint, index) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change here is inlining chartData variable into JSX markup, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.

@vojtechszocs
Copy link
Contributor

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Apr 1, 2020
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bipuladh, vojtechszocs

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@bipuladh
Copy link
Contributor Author

bipuladh commented Apr 1, 2020

/retest

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@bipuladh
Copy link
Contributor Author

bipuladh commented Apr 1, 2020

/retest

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@bipuladh
Copy link
Contributor Author

bipuladh commented Apr 1, 2020

/retest

@openshift-merge-robot openshift-merge-robot merged commit 89c186c into openshift:master Apr 1, 2020
@spadgett spadgett added this to the v4.5 milestone Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. component/ceph Related to ceph-storage-plugin lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants