...

/

Testing Band Creation

Testing Band Creation

In this lesson, you’ll learn how to test a specific feature of your Ember.js application.

We now have the main elements for writing acceptance tests in place, so let’s add a couple more scenarios in the same module to prevent features from breaking in the future.

First, we’ll test creating a band:

Press + to interact
// tests/acceptance/bands-test.js
import { module, test } from 'qunit';
// import { visit } from '@ember/test-helpers';
import { visit, click, fillIn } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { getPageTitle } from 'ember-page-title/test-support';
module('Acceptance | Bands', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
test('List bands', async function (assert) {
this.server.create('band', { name: 'Radiohead' });
this.server.create('band', { name: 'Long Distance Calling' });
await visit('/');
assert.equal(getPageTitle(), 'Bands | Rock & Roll with Octane');
let bandLinks = document.querySelectorAll('.mb-2 > a');
assert.equal(bandLinks.length, 2, 'All band links are rendered');
assert.ok(
bandLinks[0].textContent.includes('Radiohead'),
'First band link contains the band name'
);
assert.ok(
bandLinks[1].textContent.includes('Long Distance Calling'),
'The other band link contains the band name'
);
});
test('Create a band', async function (assert) {
this.server.create('band', { name: 'Royal Blood' });
await visit('/');
await click('a[href="/bands/new"]');
await fillIn('input', 'Caspian');
await click('button');
let bandLinks = document.querySelectorAll('.mb-2 > a');
assert.equal(
bandLinks.length,
2,
'All band links are rendered',
'A new band link is rendered'
);
assert.ok(
bandLinks[1].textContent.includes('Caspian'),
'The new band link is rendered as the last item'
);
assert.ok(
document
.querySelector('.border-b-4.border-purple-400')
.textContent.includes('Songs'),
'The Songs tab is active'
);
});
});

We’ll have to overcome all kinds of problems to make this new test pass. Luckily none of them are existential.

First off, the Mirage server, the mock backend, needs to handle the POST /bands request as it doesn’t currently. We can do this with another magic shorthand handler:

Press + to interact
// mirage/config.js
export default function() {
this.get('/bands');
this.get('/bands/:id');
this.post('/bands');
}

This doesn’t get us far, though. The next error message on the console is pretty scary:

...
Access this course and 1400+ top-rated courses and projects.