Skip to content
This repository has been archived by the owner on May 26, 2019. It is now read-only.

testing/acceptance: Remove outdated "Custom test helpers" section #2254

Merged
merged 1 commit into from
Mar 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 0 additions & 108 deletions source/testing/acceptance.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,111 +152,3 @@ Synchronous helpers are performed immediately when triggered.
[7]: http://emberjs.com/api/classes/Ember.Test.html#method_currentRouteName
[8]: http://emberjs.com/api/classes/Ember.Test.html#method_currentURL
[9]: http://emberjs.com/api/classes/Ember.Test.html#method_find

### Custom Test Helpers

For creating your own test helper, run `ember generate test-helper
<helper-name>`. Here is the result of running `ember g test-helper
shouldHaveElementWithCount`:

```tests/helpers/should-have-element-with-count.js
import { registerAsyncHelper } from '@ember/test';

export default registerAsyncHelper(
'shouldHaveElementWithCount', function(app) {
});
```

[`Ember.Test.registerAsyncHelper`][10] and [`Ember.Test.registerHelper`][11]
are used to register test helpers that will be injected when `startApp` is
called. The difference between `Ember.Test.registerHelper` and
`Ember.Test.registerAsyncHelper` is that the latter will not run until any
previous async helper has completed and any subsequent async helper will wait
for it to finish before running.

[10]: http://emberjs.com/api/classes/Ember.Test.html#method_registerAsyncHelper
[11]: http://emberjs.com/api/classes/Ember.Test.html#method_registerHelper

The helper method will always be called with the current Application as the
first parameter. Other parameters, such as assert, need to be provided when calling the helper. Helpers need to be registered prior to calling
`startApp`, but ember-cli will take care of it for you.

Here is an example of a non-async helper:

```tests/helpers/should-have-element-with-count.js
import { registerHelper } from '@ember/test';

export default registerHelper('shouldHaveElementWithCount',
function(app, assert, selector, n, context) {
const el = findWithAssert(selector, context);
const count = el.length;
assert.equal(count, n, `found ${count} times`);
}
);

// shouldHaveElementWithCount(assert, 'ul li', 3);
```

Here is an example of an async helper:

```tests/helpers/dblclick.js
import { run } from '@ember/runloop';
import { registerAsyncHelper } from '@ember/test';

export default registerAsyncHelper('dblclick',
function(app, assert, selector, context) {
let $el = findWithAssert(selector, context);
run(() => $el.dblclick());
}
);

// dblclick(assert, '#person-1')
```

Async helpers also come in handy when you want to group interaction
into one helper. For example:

```tests/helpers/add-contact.js
import { registerAsyncHelper } from '@ember/test';

export default registerAsyncHelper('addContact',
function(app, name) {
fillIn('#name', name);
click('button.create');
}
);

// addContact('Bob');
// addContact('Dan');
```

Finally, don't forget to add your helpers in `tests/.eslintrc.js` and in
`tests/helpers/start-app.js`. In `tests/.eslintrc.js` you need to add it in the
`globals` section, otherwise you will get failing ESLint tests:

```tests/.eslintrc.js{-4,+5,+6,+7,+8,+9,+10}
module.exports = {
env: {
embertest: true
}
},
globals: {
shouldHaveElementWithCount: true,
dblclick: true,
addContact: true
}
};
```

In `tests/helpers/start-app.js` you need to import the helper file: it
will be registered then.

```tests/helpers/start-app.js
import Ember from 'ember';
import Application from '../../app';
import Router from '../../router';
import config from '../../config/environment';
import './should-have-element-with-count';
import './dblclick';
import './add-contact';
```