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

[htmlIdGenerator] testenv mock #4212

Merged
merged 5 commits into from
Nov 11, 2020
Merged

Conversation

thompsongl
Copy link
Contributor

@thompsongl thompsongl commented Nov 2, 2020

Summary

Closes #1381 by providing a testenv mock for use in Jest environments (makeId was previously removed and not in use).
Added internal mocking to EUI and removed one-off component mocks from individual test files.

we could hash the stack trace - maybe removing the line numbers - to generate a consistent, yet unique, ID.

Explored this idea, but without line numbers the trace is not very helpful, and with numbers we run the risk of excessive snapshot churn. We can discuss here if something other than generated-id is better.

Checklist

- [ ] Check against all themes for compatibility in both light and dark modes
- [ ] Checked in mobile
- [ ] Checked in Chrome, Safari, Edge, and Firefox
- [ ] Props have proper autodocs
- [ ] Added documentation
- [ ] Checked Code Sandbox works for the any docs examples

- [ ] Checked for breaking changes and labeled appropriately
- [ ] Checked for accessibility including keyboard-only and screenreader modes

  • A changelog entry exists and is marked appropriately

@myasonik
Copy link
Contributor

myasonik commented Nov 2, 2020

Just to make sure, I noticed that in the a11y test where we need the unique IDs, you call requireActual to get it all to work. We'll be able to do the same in Kibana, right?

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_4212/

@thompsongl
Copy link
Contributor Author

thompsongl commented Nov 2, 2020

you call requireActual to get it all to work. We'll be able to do the same in Kibana, right?

Thanks for bringing this up. Testing in Kibana highlights that moduleNameMapper (the method by which EUI is mocked) will also be used when jest.requireActual is called, meaning that we cannot get the original, unmocked function like we do in EUI test suite. We can, however, still overwrite the mock with a new mock.
Given that there does appear to be one non-shapshot Jest test in Kibana that relies on htmlIdGenerator returning unique values, we should address requireActual.

I have a solution in mind: add a second parameter on the testenv version that calls uuid rather than statically define generated-id. This this, we could re-mock the function, providing the second parameter, getting the standard output. This would not be common practice.

As in:

jest.mock('@elastic/eui/lib/services/accessibility/html_id_generator', () => {
  const { htmlIdGenerator } = jest.requireActual(
    '@elastic/eui/lib/services/accessibility/html_id_generator'
  );
  return { htmlIdGenerator: () => htmlIdGenerator('', true) };
});

@chandlerprall
Copy link
Contributor

As that requires re-mocking the generator anyway, might be cleaner to let the mock do its own thing instead of building that extra support into the shipped mock?

@thompsongl
Copy link
Contributor Author

might be cleaner to let the mock do its own thing instead

I'm good with this approach, also. What do you think, @myasonik?

@myasonik
Copy link
Contributor

myasonik commented Nov 3, 2020

Seems good to me!

@thompsongl
Copy link
Contributor Author

Because we're in agreement on not adding more to the mock, this is ready for review.

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_4212/

Copy link
Contributor

@chandlerprall chandlerprall left a comment

Choose a reason for hiding this comment

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

Changes LGTM!

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_4212/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[makeId] Makes it difficult for consumers to create Jest tests
4 participants