In Open Event Frontend, we have integration tests for ember components which are used throughout the project. But even after those tests, user interaction could pose some errors. We perform acceptance tests to alleviate such scenarios.
Acceptance tests interact with application as the user does and ensures proper functionality of a feature or to determine whether or not the software system has met the requirement specifications. They are quite helpful for ensuring that our core features work properly.
Let us write an acceptance test for register feature in Open Event Frontend.
import { test } from 'qunit';
import moduleForAcceptance from 'open-event-frontend/tests/helpers/module-for-acceptance';
moduleForAcceptance('Acceptance | register');
In the first line we import test from ‘ember-qunit’ (default unit testing helper suite for Ember) which contains all the required test functions. For example, here we are using test function to check the rendering of our component. We can use test function multiple times to check multiple components.
Next, we import moduleForAcceptance from ‘open-event-frontend/tests/helpers/module-for-acceptance’ which deals with application setup and teardown.
test('visiting /register', function(assert) {
visit('/register');
andThen(function() {
assert.equal(currentURL(), '/register');
});
});
Inside our test function, we simulate visiting /register route and then check for the current route to be /register.
test('visiting /register and registering with existing user', function(assert) {
visit('/register');
andThen(function() {
assert.equal(currentURL(), '/register');
fillIn('input[name=email]', 'opev_test_user@nada.email');
fillIn('input[name=password]', 'opev_test_user');
fillIn('input[name=password_repeat]', 'opev_test_user');
click('button[type=submit]');
andThen(function() {
assert.equal(currentURL(), '/register');
// const errorMessageDiv = findWithAssert('.ui.negative.message');
// assert.equal(errorMessageDiv[0].textContent.trim(), 'An unexpected error occurred.');
});
});
});
Then we simulate visiting /register route and register a dummy user. For this, we first go to /register route and then check for the current route to be register route. We fill the register form with appropriate data and hit submit.
test('visiting /register after login', function(assert) {
login(assert);
andThen(function() {
visit('/register');
andThen(function() {
assert.equal(currentURL(), '/');
});
});
});
The third test is to simulate visiting /register route with user logged in and this is very simple. We just visit /register route and then check if we are are at / route or not because a user redirects to / route when he tries to visit /register after login.
And since we checked for all the possible combinations, to run the test we simply use the following command-
ember test --server
But there is a little demerit to acceptance tests. They boot up the whole EmberJS application and start us at the application.index route. We then have to navigate to the page that contains the feature being tested. Writing acceptance tests for each and every feature would be a big waste of time and CPU cycles. For this reason, only core features are tested for acceptance.
Resources
- Ember guides
- Blog on acceptance testing
- Video tutorial on acceptance testing