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

Refactor ValidatedForm tests #465

Merged
merged 4 commits into from
Sep 10, 2018

Conversation

BudgieInWA
Copy link
Contributor

While looking to test my upcoming changes to #441, I got a bit carried away trying to make sense of the ValidatedForm tests. I have refactored them in a way that highlights the patterns and makes it clear to me what my new tests should look like. Hopefully that holds true in general.

There was one of the tests that I couldn't get to the bottom of, as described initially in fa4dc6b. What do you think?

The real history is available here for the curious.

@BudgieInWA BudgieInWA requested a review from radekmie as a code owner September 8, 2018 02:06
@radekmie radekmie added the Type: Feature New features and feature requests label Sep 8, 2018
Copy link
Contributor

@radekmie radekmie left a comment

Choose a reason for hiding this comment

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

Great work! Current tests were built up just to cover each branch and not all of them were simplified later.

I'll try to check the failing tests in the meantime.


expect(validator).not.toBeCalled();
describe('on validation', () => {
let wrapper, form;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use two lets.

@@ -9,11 +9,7 @@ jest.mock('meteor/check');
describe('ValidatedForm', () => {
const onChange = jest.fn();
const onSubmit = jest.fn();
const onValidate = jest.fn((model, error, next) => {
if (error) return;
Copy link
Contributor

@radekmie radekmie Sep 8, 2018

Choose a reason for hiding this comment

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

I think, that this might be the issue - this test was not failing, because it never returned.

EDIT: That's not the case, but it's a good catch.

wrapper.setProps({model});

expect(validator).not.toBeCalled();
beforeEach(async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't have to be async.

});
onValidate.mockImplementationOnce((m, e, next) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's keep using full names everywhere.

next(null);
});
form.validate();
await new Promise(resolve => process.nextTick(resolve));
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pretty sure that this one is no longer needed, as you don't use wrapper nor form below.

<ValidatedForm model={model} schema={schema} onValidate={onValidate} validate="onChange" />
);
describe('on change', () => {
describe('in "onChange" mode', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no convention, but I'd keep ` instead of ".

wrapper.find('form').simulate('submit');
expect(validator).toHaveBeenCalled();
expect(onValidate).toHaveBeenCalled();
expect(onSubmit).toHaveBeenCalled();
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not called, because:

  • .validate() is not submitting the form (it should be .submit())
  • onSubmit is defined above, but not passed to the form (line 36)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! That makes perfect sense. I have now refactored it now (a9d7566). I have also addressed the stylistic points.

The test 'lets `onValidate` suppress `validator` errors' is really about testing `onValidate`, not the submit flow, so now the test is 'leaves error state alone when `onValidate` suppress `validator` errors' (like 'updates error state with async errors from `onValidate`').
@codecov
Copy link

codecov bot commented Sep 10, 2018

Codecov Report

Merging #465 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #465   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files         157    157           
  Lines        1404   1404           
=====================================
  Hits         1404   1404

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b1ad1e1...18fc7ac. Read the comment docs.

@radekmie radekmie merged commit 5171be3 into vazco:master Sep 10, 2018
@radekmie
Copy link
Contributor

Thank you again @BudgieInWA!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New features and feature requests
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants