Skip to content

Fixes #38673 - dev docs - ui tests #10654

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions developer_docs/ui-testing-guidelines.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[[ui-testing-guidelines]]
= UI Testing Guidelines
:toc: right
:toclevels: 5

== Running jest tests
To run jest tests, you can use the following command:
```bash
npm run test
```
This will run all .js.test files.
To test plugins, you can use the following command:
```bash
npm run test:plugins <plugin-name>
```
plugin name is optional, if not provided, all plugins will be tested.
As the output for tests is long, the command will list the plugins that have failed tests at the end.

== Running linters
To run linters, you can use the following command:
```bash
npm run lint
```
To run linters for plugins, you can use the following command:
```bash
npm run lint:plugins <plugin-name>
```
plugin name is optional, if not provided, all plugins will be linted.

== Writing tests
Suggestions for simple and complex components, but developers should use their best judgement.

=== Simple components - React Testing Library
For simple components, that dont rely on API, a simple React Testing Library test is enough.
In the React Testing Library test, render the component and check that the text you expect to see is there.
and when a user action happens, see that the change is reflected after the action and not before.
You can read more about React Testing Library in https://testing-library.com/docs/guiding-principles/[React Testing Guiding Principles].
Test examples:

- webpack/assets/javascripts/react_app/components/Layout/components/TaxonomySwitcher/TaxonomySwitcher.test.js
- webpack/assets/javascripts/react_app/routes/Upgrade/UpgradePage/\\__tests__/UpgradePage.test.js
- webpack/assets/javascripts/react_app/components/Loading/\\__tests__/Loading.test.js

- Any test that ran by jest (`npm run test`) might use mocked functions.
You can set a mock per test, or globally by adding the mock to `\\__mocks__` folder.
Some mocked files are already in `\\__mocks__` folder, but dont define all the functions from the original file, so they will show up as undefined.
- Tests files should be named like `*.test.js`, and should be in the same folder as the component, or in the `\\__tests__` folder.
- Tests can mock the API in a few ways:
* By adding the mock data to the store using `redux-mock-store`
Copy link
Member

Choose a reason for hiding this comment

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

redux-mox-store is already deprecated. Are we sure we want to document an already deprecated technology? They point to redux documentation for more info on how to test redux enabled components.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the note, I'll add one test that uses redux correctly to this pr
Should we encourage people in the guidelines to use AI ?

* By mocking the selectors return values
* By mocking the API calls directly

=== Complex components - Capybara
For more complex components, that rely on API or too many other components, write a Capybara test.
You can create db items with FactoryBot.
To test simple index you can use assert_index_page.

Test examples:

- test/integration/domain_test.rb
- test/integration/domain_js_test.rb
- test/integration/audit_js_test.rb

=== Timing issues
A note on both types of tests, is sometimes they might fail because of timing issues. (popovers, modals, api calls, etc.)

For React Testing Library tests, you can use:

* jest.useFakeTimers(); and then jest.advanceTimersByTime(1000);

For Capybara tests, you can use:

* wait_for_ajax
* or other wait_for_... methods
Loading