-
Notifications
You must be signed in to change notification settings - Fork 1
Writing Tests for LAP
We are using RSpec
.
To run test
bundle exec rake spec
spec/javascript/components/IndexTable.test.js spec/javascript/components/ListingDetails.test.js
The examples are using Enzyme and Jest Snapshots. https://facebook.github.io/jest/docs/en/snapshot-testing.html http://airbnb.io/enzyme/docs/api/
Snapshot in a few words: it takes a snapshot of the DOM at a giving time in the test process and compares with old results. Enzyme let you search in the React DOM and simulate events
..so, we are going to use Enzyme to simulate events or change state and take snapshots with Jest to record those changes.
- Use Enzyme
mount
for full rendering (most of the time we are going to use mount, butshallow
rendering might be helpfull too) - Take a snapshot with Jest (passing the Enzyme wrapper)
- Manipulate the DOM with Enzyme by changing state, properties or simulating an event.
- Take another snapashot
yarn test
If you made a legitimate change in the view and a snapshot fails then you have to tell Jest to update the snapshots. Run:
yarn test -u
Note1: This updates all the snapshots Note2: Snapshots should be pushed to the repo Note3: It's a good practice to inspect snapshots and verify the DOM generated
Printing the DOM in the console:
console.log(wrapper.debug())
Process for wrting test. I recommend the following.
- Take a snapshot
- Write your changes and simulations BUT comment them out. DO not run them yet
- Take a another snapshot
- Run your test, so the second snapshot gets generated.
- Uncomment your simulations and run your tests. Second snapshot should fail and Jest should show the difference. It's a easy way to inspect the changes in the DOM :).