Reputation: 27387
Is there a way to alter the controller's property value inside acceptance test?
test('should add new post', function(assert) {
visit('/posts/new');
fillIn('input.title', 'My new post');
click('button.submit');
andThen(() => assert.equal(find('ul.posts li:first').text(), 'My new post'));
});
For example, I would like to set the default value for an input before running the test.
Upvotes: 2
Views: 1234
Reputation: 480
The currently accepted answer by @ebrahim-pasbani is correct for any Ember v 2.x apps that are using the legacy QUnit Testing API by [email protected]
or lower.
Please note though, that in case that you are using the more recent QUnit Testing API that is available in your app if you are either
[email protected]
or greater orthe following will work by leveraging the public owner
API and the setupApplicationTest
helper:
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { visit, fillIn, click, find } from '@ember/test-helpers';
import { run } from '@ember/runloop';
module('Acceptance | posts', function(hooks) {
setupApplicationTest(hooks);
test('should add new post', async function(assert) {
await run(() => this.owner.lookup('controller:posts/new').set('val', 'default'));
await visit('/posts/new');
await fillIn('input.title', 'My new post');
await click('button.submit');
assert.equal(find('ul.posts li:first').textContent.trim(), 'My new post');
});
});
If you are unfamiliar with the new QUnit Testing API yet, I can recommend reading Robert Jackson's introduction to the API, the updated official Testing Guides as well as the original RFC for even more context.
Upvotes: 4
Reputation: 9406
You can access to application registry and lookup the controller.
moduleForAcceptance
sets application.
test('should add new post', function(assert) {
let controller = this.application.__container__.lookup('controller:posts/new');
controller.set('val', 'default');
visit('/posts/new');
fillIn('input.title', 'My new post');
click('button.submit');
andThen(() => assert.equal(find('ul.posts li:first').text(), 'My new post'));
});
Please take a look at this twiddle
Upvotes: 5