-
Notifications
You must be signed in to change notification settings - Fork 1k
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
MariaAga
wants to merge
1
commit into
theforeman:develop
Choose a base branch
from
MariaAga:test-guidelines
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
* 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 ?