5

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.

ykaragol
  • 6,139
  • 3
  • 29
  • 56
Molfar
  • 1,411
  • 3
  • 18
  • 49
  • Cour code is wrong ;), `willTransition` is an hook, not an action. Don't put it in the `actions` hash. And you could just call it to rest it. You can stub the store. – Lux Oct 10 '16 at 15:16
  • @Lux to write our logic in `willTransition` hook we should define it in actions right ?. – Ember Freak Oct 10 '16 at 15:35
  • @Lux, Hmm, you know, according to the guide https://guides.emberjs.com/v2.8.0/routing/preventing-and-retrying-transitions/ willTransition appears in actions hash. "When a transition is attempted, whether via {{link-to}}, transitionTo, or a URL change, a willTransition action is fired on the currently active routes." - they call it "action", not "hook". – Molfar Oct 10 '16 at 16:05
  • 1
    Unrelated, but currentModel is a private method, and is not the"ember" way of grabbing your.....current model.... in the route: https://github.com/emberjs/ember.js/issues/11760 I'm not very knowledgeable about testing in Ember yet, but if you cannot access the store from the test, I am not sure you can test this. Rollback attributes, in the given example, will delete the unsaved model from the store, or remove any unsaved changes from an existing model - again from the store. – bambery Oct 10 '16 at 22:32
  • okay, obviously I was wrong. sorry. Thats interesting. – Lux Oct 11 '16 at 03:28

1 Answers1

4

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.

jetpackpony
  • 1,270
  • 1
  • 12
  • 22
  • Just a minor note, instead of using `route.actions.willTransition.call(route); ` you could call `route.send('willTransition');` – Alan Dong Jul 29 '19 at 21:12