Skip to content

Writing Tests for LAP

Christine Dolendo edited this page Apr 23, 2018 · 1 revision

Rails/Ruby - Controllers/APi

We are using RSpec. To run test bundle exec rake spec

JavaScript

Test file examples

spec/javascript/components/IndexTable.test.js spec/javascript/components/ListingDetails.test.js

Testing with Jest and Enzyme:

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.

Writing a test

  1. Use Enzyme mount for full rendering (most of the time we are going to use mount, but shallow rendering might be helpfull too)
  2. Take a snapshot with Jest (passing the Enzyme wrapper)
  3. Manipulate the DOM with Enzyme by changing state, properties or simulating an event.
  4. Take another snapashot

Running the test

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

Recomendations

Printing the DOM in the console:

console.log(wrapper.debug())

Process for wrting test. I recommend the following.

  1. Take a snapshot
  2. Write your changes and simulations BUT comment them out. DO not run them yet
  3. Take a another snapshot
  4. Run your test, so the second snapshot gets generated.
  5. 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 :).