How can I test this code in Ember? Explain me please the concept, in general.
// app/routes/products/new.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.createRecord('product');
},
actions: {
willTransition() {
this._super(...arguments);
this.get('controller.model').rollbackAttributes();
}
}
});
I have no idea how to make this. May be stub model in route? I found that store is not available in route test.
After Ruby and RSpec, all these new javascript world is confusing a little bit) But I'd like to learn it anyway.
First, you can run the test suite by entering the command ember test , or ember t , in your terminal. This will run the suite just once. Suppose, instead, you want the suite to run after every file change. You can enter ember test --server , or ember t -s .
ember install ember-route-action-helper. The route-action helper allows you to bubble closure actions, which will delegate it to the currently active route hierarchy per the bubbling rules explained under actions . Like closure actions, route-action will also have a return value.
A route consistency test takes two different route advertisements to the same destination as input and outputs true if the routes are consistent and outputs false otherwise. Consistency is abstractly defined as follows: If both route announcements are valid then the output is true.
In Ember. js, controllers allow you to decorate your models with display logic. In general, your models will have properties that are saved to the server, while controllers will have properties that your app does not need to save to the server.
In unit tests the idea is to stub all external dependencies. In ember you can do this:
// tests/unit/products/new/route-test.js
test('it should rollback changes on transition', function(assert) {
assert.expect(1);
let route = this.subject({
controller: Ember.Object.create({
model: Ember.Object.create({
rollbackAttributes() {
assert.ok(true, 'should call rollbackAttributes on a model');
}
})
})
});
route.actions.willTransition.call(route);
});
Basically you stub controller and model passing them to this.subject()
, then call whatever function you are testing (in this case you have to use call or apply to call an action with the correct scope), and then assert that rollbackAttributes()
was called.
assert.expect(1);
at the start of a test tells QUnit to wait for exactly 1 assertion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With