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

Added tests for dot notation processors. #107340

Merged

Conversation

cuff-links
Copy link
Contributor

Summary

This PR is to add test coverage for the Ingest Pipelines Date Processor.

Test cases being covered

Prevents submission 0/1 required fields given.
Prevents submission when field does not contain . for the dot notation.
Saves processor with default options
Saves processor with setting optional options.

@cuff-links cuff-links added v8.0.0 Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more release_note:skip Skip the PR/issue when compiling release notes Feature:Ingest Node Pipelines Ingest node pipelines management v7.14.0 auto-backport Deprecated - use backport:version if exact versions are needed labels Jul 31, 2021
@cuff-links cuff-links requested a review from a team as a code owner July 31, 2021 00:39
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-stack-management (Team:Stack Management)

@cuff-links
Copy link
Contributor Author

@sebelga For the second test in this test case, when I fill out the field but omit the . for the dot notation, the error does not show up in the errors object in the assertion when I save the processor. It does when I test manually though. I am not sure if this is an issue with the test, forms lib, bug, or what have you. Can you try this out? I tried loggin component.debug() but the output was so large that I got lost in it.

} = testBed;

// Add "field" value (required)
form.setInputValue('fieldNameField.input', 'missing');
Copy link
Contributor

Choose a reason for hiding this comment

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

As you are testing a validation to run and then display a message you need to wait for the validation to have ran. The validation run after a 500ms debounce time after the form input value has change.

As you correctly use the fake timer from jest the 500ms never pass and the validation does not run.

This is what you need to do

// Wrap the `setInputValue` in an `act()` call as it modifies the internal value state of the field and mark it as "changed".
// This trigger the debounce which now waits... 
await act(async () => {
  form.setInputValue('fieldNameField.input', 'missing');
});
// Move ahead the debounce time which will then execute any validations
await act(async () => {
  jest.runAllTimers();
});

Copy link
Contributor

Choose a reason for hiding this comment

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

@sebelga interestingly enough - we are not wrapping other instances of form.setInputValue() in act() (although I guess we probably should be?) or calling runAllTimers() in these processor tests. Is there something different about this specific validation?

On another note - should the act() be part of the actual setInputValue method in the testbed utils? I see it does call component.update() after the simulate change - related code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Depending on what you are testing and if there is code reacts to the field value change. You might not need to wrap with an act when changing the form input in certain cases. Here he is testing the result of the validation so he both needs the act() and wait for the debounce.

If for example you are testing that the correct payload is sent, then you don't need to wait for useEffect (that triggers the validate() on field). You can set the value of all fields synchronously and simulate the click of the "Send" button. That will call the form.getData() and all the values are there synchronously.

As you can see it in my latest test for preview, my helpers to modify field values all wrap with an act call (https://github.com/elastic/kibana/pull/106410/files#diff-8733b59b04f21f21eb6bf66bd48aba1334abad4ec93d8bde99ac1386e000f131R32)

should the act() be part of the actual setInputValue method in the testbed utils?

It probably would have be good to do it there. Now it would require a lot of work to add it there and then remove it everywhere we have wrapped setInputValue with an act as it would be wrapped twice 😊

Note that this is all due to hooks and the way state updates and react in useEffect. The await act( ... ) makes sure all the lifecycle has ran.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation @sebelga!

I was thinking there were other test cases where we were verifying the input validation, but I see now most of the processor tests are only testing empty fields on submit, e.g.,

 test('prevents form submission if required fields are not provided', async () => {
    const {
      actions: { saveNewProcessor },
      form,
    } = testBed;

    // Click submit button with only the type defined
    await saveNewProcessor();

    // Expect form error as "field" and "target_field" are required parameters
    expect(form.getErrorsMessages()).toEqual(['A field value is required.']);
  });

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes and saveNewProcessor is wrapped

async saveNewProcessor() {
  await act(async () => {
    find('addProcessorForm.submitButton').simulate('click');
  });
  component.update();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I get it. I am not sure if this is spelled out explicitly in our CIT docs but I think it would be useful to do so. @sebelga @alisonelizabeth

Copy link
Contributor

@alisonelizabeth alisonelizabeth left a comment

Choose a reason for hiding this comment

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

Nice work @cuff-links! Left a few nits - looks like a some of the comments in the tests might have been copy-pasted and need to be updated.

} = testBed;

// Add "field" value (required)
form.setInputValue('fieldNameField.input', 'missing');
Copy link
Contributor

Choose a reason for hiding this comment

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

@sebelga interestingly enough - we are not wrapping other instances of form.setInputValue() in act() (although I guess we probably should be?) or calling runAllTimers() in these processor tests. Is there something different about this specific validation?

On another note - should the act() be part of the actual setInputValue method in the testbed utils? I see it does call component.update() after the simulate change - related code.

@cjcenizal
Copy link
Contributor

@cuff-links We're in the 7.15 dev cycle now, so could you please update the version labels?

Copy link
Contributor

@alisonelizabeth alisonelizabeth left a comment

Choose a reason for hiding this comment

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

Latest LGTM. Ran tests locally. Thanks @cuff-links!

Looks like there are some linting issues that may need to be addressed before merging. The test failure reported looks unrelated to these changes.

Also, I noticed there is a new parameter for the dot expander processor. Opened #108895 as a follow-up issue to address.

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
ingestPipelines 709.7KB 709.8KB +29.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@cuff-links cuff-links merged commit c373e48 into elastic:master Aug 17, 2021
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Aug 17, 2021
* Added tests for dot notation processors.

* Fixed nits in PR.

* Fixed linting issues.
@kibanamachine
Copy link
Contributor

💚 Backport successful

Status Branch Result
7.x

This backport PR will be merged automatically after passing CI.

kibanamachine added a commit that referenced this pull request Aug 18, 2021
* Added tests for dot notation processors.

* Fixed nits in PR.

* Fixed linting issues.

Co-authored-by: John Dorlus <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Deprecated - use backport:version if exact versions are needed Feature:Ingest Node Pipelines Ingest node pipelines management release_note:skip Skip the PR/issue when compiling release notes Team:Kibana Management Dev Tools, Index Management, Upgrade Assistant, ILM, Ingest Node Pipelines, and more v7.15.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants