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

test for traffic splitting and refactor component #4700

Merged

Conversation

nemesis09
Copy link
Contributor

@nemesis09 nemesis09 commented Mar 9, 2020

Fixes
issue https://issues.redhat.com/browse/ODC-3281

Analysis / Root cause:
Increase test coverage for components in the knative package

Solution Description:
adds unit test to TrafficSplittingModal component

Screens:
Screenshot from 2020-03-24 05-58-55
traffic-test-coverage

Browser Conformance:

  • Firefox
  • Chrome
  • Safari
  • Edge

@openshift-ci-robot openshift-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. component/knative Related to knative-plugin labels Mar 9, 2020
@nemesis09
Copy link
Contributor Author

/retest

@nemesis09
Copy link
Contributor Author

/test frontend

mockServiceData,
} from '../../../utils/__mocks__/traffic-splitting-utils-mock';

describe('TrafficSplitting', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit:

Suggested change
describe('TrafficSplitting', () => {
describe(TrafficSplitting.displayName, () => {

Copy link
Contributor

Choose a reason for hiding this comment

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

@sahil143 unless we explicitly assign a display name, this won't work.

Copy link
Contributor

@sahil143 sahil143 Mar 11, 2020

Choose a reason for hiding this comment

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

@christianvogt This works with the functional component as well without defining it explicitly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Never Mind, This doesn't work, just tested this.

Comment on lines 10 to 14
it('should match the previous traffic splitting snapshot', () => {
const tree = Renderer.create(
<TrafficSplitting service={mockServiceData} revisions={mockRevisions} />,
).toJSON();
expect(tree).toMatchSnapshot();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you using snapshot testing here? I don't think snapshot testing is the right way here because If there are some changes made in the TrafficSplittingModal component this test will fail even though there are no changes made to the TrafficSplitting component.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I agree with Sahil. Snapshot testing isn't very useful in this case. I usually don't prefer snapshot testing. You should be testing some rendering logic of the component or even the validation logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Snapshot testing this is not useful at all.
We should be using shallow to mount and then assert specific changes in in the rendered output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed snapshot testing and updated code with shallow testing

@andrewballantyne
Copy link
Contributor

/kind cleanup

@openshift-ci-robot openshift-ci-robot added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Mar 10, 2020
};
};

const Controller: React.FC<ControllerProps> = (props) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

What was the need to move the Controller component into a separate file? You are not using the component anywhere else. And now you have two components Controller and TrafficSplitingController which seems ambiguous to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed. In this case the only reason we have this other component is because of the poor implementation of Firehose which injects props into the child component. We will have (already do?) hooks to perform firehose like behavior and this component will just go away then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The intention was to follow "one file on component", but I can see now that this component just exists for implementing Firehose and has no other use. Have reverted to the initial implementation.

Comment on lines 10 to 14
it('should match the previous traffic splitting snapshot', () => {
const tree = Renderer.create(
<TrafficSplitting service={mockServiceData} revisions={mockRevisions} />,
).toJSON();
expect(tree).toMatchSnapshot();
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I agree with Sahil. Snapshot testing isn't very useful in this case. I usually don't prefer snapshot testing. You should be testing some rendering logic of the component or even the validation logic.

@nemesis09 nemesis09 force-pushed the test-traffic-splitting branch 4 times, most recently from 0543025 to a4bb1a5 Compare March 24, 2020 00:19
);
});

it('should disable add row for one value', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this say disable delete row button for one value? The prop you're checking is disableDeleteRow and we do not disable add button ever.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed.

).toBe(true);
});

it('should not disable add row for more than one values', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

).toBe(false);
});

it('should should render modal footer with proper values', () => {
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
it('should should render modal footer with proper values', () => {
it('should render modal footer with proper values', () => {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed typo

@debsmita1
Copy link
Contributor

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Mar 30, 2020
};

describe('TrafficSplittingModal', () => {
let wrapper;
Copy link
Member

Choose a reason for hiding this comment

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

@nemesis09 can we have type for wrapper

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

mockRevisionItems,
} from '../../../utils/__mocks__/traffic-splitting-utils-mock';

const formProps = {
Copy link
Member

Choose a reason for hiding this comment

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

can we have type for formProps and move it to beforeEach as there is just one describe block

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines +91 to +59
wrapper.find('form').simulate('submit', {
preventDefault: () => {},
});
Copy link
Member

Choose a reason for hiding this comment

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

@nemesis09 can we check on simulate submit , handleSubmit has been called

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To check changes made by handleSubmit we can check the status. That needs an async testing. I tried to test it asynchronously but I couldn't work it out as the call to Formik is made in TrafficSplitting component and handleSubmit is passed from there. To test this we need to pass a mock handleSubmit and make some changes to the form in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

placed a test in place to check if handle submit has been called on submit.

Comment on lines 100 to 79
expect(
wrapper
.find(ModalSubmitFooter)
.first()
.props().errorMessage,
).toEqual('error');
});
Copy link
Member

Choose a reason for hiding this comment

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

why will there be error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is a synchronised testing, ideally in the async case,error is set in status inside handleSubmit from TrafficSplitting component. Here I'm just checking if the prop is passed. any async call to handleSubmit is consumed.

@openshift-ci-robot openshift-ci-robot removed the lgtm Indicates that a PR is ready to be merged. label Apr 2, 2020
Comment on lines 17 to 44
formProps = {
errors: {},
touched: {},
isSubmitting: true,
isValidating: true,
status: { error: 'error' },
submitCount: 0,
dirty: false,
handleReset: jest.fn(),
handleSubmit: jest.fn(),
getFieldProps: jest.fn(),
handleBlur: jest.fn(),
handleChange: jest.fn(),
initialValues: [{ percent: 100, revisionName: 'overlayimage-fdqsf' }],
initialErrors: {},
initialStatus: {},
initialTouched: {},
isValid: true,
resetForm: jest.fn(),
setErrors: jest.fn(),
setFieldError: jest.fn(),
setFieldTouched: jest.fn(),
setFieldValue: jest.fn(),
setFormikState: jest.fn(),
setStatus: jest.fn(),
setSubmitting: jest.fn(),
setTouched: jest.fn(),
setValues: jest.fn(),
Copy link
Member

Choose a reason for hiding this comment

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

can import props from here frontend/packages/console-shared/src/test-utils/formik-props-utils.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@invincibleJai
Copy link
Member

/lgtm
/approve

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dboytherealest1000, debsmita1, invincibleJai, nemesis09

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

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 7, 2020
@openshift-merge-robot openshift-merge-robot merged commit 5799723 into openshift:master Apr 7, 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/knative Related to knative-plugin kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants