How to test the route will operate in Ember?

How can I check this code in Ember? Please explain to me the concept as a whole.

// 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 do this. Maybe a stub model is on the way? I found that the store is not available in the route test.

After Ruby and RSpec, all of these new javascript worlds are a bit confusing). But I would still like to know.

+4
source share
1 answer

In unit tests, the idea is to drown out 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);
});

, , this.subject(), , ( ), , rollbackAttributes().

assert.expect(1); QUnit 1 .

+2

Source: https://habr.com/ru/post/1657294/


All Articles