I'm trying to create an acceptance test for the login page of a webapp. Everything is almost working, except the await click(element)
promise never resolves:
import { module, test } from 'qunit';
import { visit, currentURL, fillIn, click, waitFor, getSettledState } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { invalidateSession } from 'ember-simple-auth/test-support';
import { setupMirage } from 'ember-cli-mirage/test-support';
module('Acceptance | login', function(hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
test('login page | login success', async function(assert) {
assert.timeout(5000);
console.log('start', getSettledState());
await visit('/login');
assert.equal(currentURL(), '/login');
await waitFor('.btn-primary:disabled');
await fillIn('input[type="text"]', 'mirage');
await fillIn('input[type="password"]', 'password1234');
await waitFor('.btn-primary:not([disabled])', 2000);
console.log('btn enabled');
let btnSubmit = document.querySelector('.btn-primary');
assert.equal(btnSubmit.disabled, false);
await click(btnSubmit);
console.log('await btn click');
await waitFor('.nav-tabs', 4000);
console.log('nav complete');
assert.equal(currentURL(), '/login-success');
console.log('finished', getSettledState());
});
});
If I run this test as-is, "await btn click" doesn't log in the console, until it times-out. I also get the qunit error "Uncaught (in promise) Error: pushFailure() assertion outside test context, in ___ at internalStart" (underscores added by me)
HOWEVER, if I remove the await
part of the click(btnSubmit)
call, the test completes successfully BUT, the last check of getSettledState() returns this:
hasPendingRequests: true
hasPendingTimers: true
hasPendingWaiters: false
hasRunLoop: true
pendingRequestCount: 1
Since there are pending requests and timers, the test still times-out, even though all the assert()
calls were successful.
So it looks like if I run the test correctly with await click(btnSubmit)
, the test times out on the click(), but if I just call click(btnSubmit)
, the tests complete successfully, although testem or qunit doesn't know all the tests completed. What am I doing wrong?
mirage login endpoint:
this.post('/login', function(db, request) {
let formData = JSON.parse(request.requestBody);
let auth = this.serialize(db.profiles.all());
if (formData.identification !== auth.data[0].attributes.loginid || formData.password !== auth.data[0].attributes.password) {
return new Response(401, {some: 'header', 'Content-Type': 'application/json'}, {
error_code: "err_loginAuthenticationFail"
});
}
let profile = this.serialize(db.profiles.all());
profile.data[0].attributes.id = profile.data[0].attributes.localid
delete profile.data[0].attributes.localid;
return { ...longAccessTokenObject }
});
The mirage endpoint is functioning correctly, it authenticates the one user/pw combo I setup, whether in tests or manually using the /login page in Chrome.