When testing React and Angular OWA, we will create a mock webservice to test requests to the database. This is useful so that we can test our code without having to be connected to an online webservice. We see a mock webservice being created in an example test cases below. For React OWAs, in addition to testing database request, tests are usually made for proper rendering and state changes.
Choose your test type
Depending on what you selected when building your web app, you will either use React or Angular to test your OWA. Below are examples of each test type that you can use for your web app.
Your test file(s) should be stored as yourOWAfolder/test/exampleTest.js.
React (With Redux)
React OpenMRS Open Web Apps primarily use the framework Jest for its unit testing. Setting up Jest can be found here.
*All examples below are credited towards the Order Entry UIModule*
Testing Actions
When testing an action, the objective is to dispatch a function into a mock store and compare the action it dispatched to what action you expected. Your function will likely make request to a database and this can be tested by mocking a request. The example below uses moxios to mock axios request. The response of the request will determine what action is dispatched.
When testing a component, the objective is to check if the reducers change the application state as intended given a specific action. Your function will most likely compare two states. This example below creates an action and initial state then passes them to the reducer.
Main code under test
Reducer test
Setup
OpenMRS React OWA typically have a setup file for test to handle import that will be used across all tests. This is to help abide by DRY and avoid repetitive code. An example of this is shown below:
Order Entry UI's test setup file
In order to properly run tests and apply the setup file, Jest needs to be configured. An example configuration file is shown below:
Angular OpenMRS Open Web Apps also primarily use the framework Jasmine and the test runner Karma for its unit testing. To set up our mock backend, we use $httpbackend service. $httpbackend has the method whenGet that will create a new GET request. There needs to be enough whenGet calls that will receive all of the information that your controller expects. $rootScope needs to be created before each spec to observe model changes. $componentController creates an instance of a controller. Make sure you have the name of controller that you are testing in the parameters.
In the root of your project directory, run the command:
Run Tests Only
Build Project and Run Tests
Troubleshooting
Angular Owa Fails to Run Tests and Build after Generating OWA
This is a potential fix for this error message:
1) Open package.json which is in your OWA's root folder, and under dependencies, remove the "^" before the version number of each following Angular dependency: angular, angular-route, and angular-mocks.
2) In the root of your project directory, run the command: