3

I am just starting looking at testing with Ember and I am bit confused on how to test the following:

1 - Route hooks

Here's an example what I have, I am not sure if should be done through unit testing or user acceptance testing? How do I triggers the hooks, wait the promise etc.?

/**
 * Model hook.
 */
model() {
    return this.modelFor('new');
},

/**
 * AfterModel hook.
 */
afterModel() {
    /**
     * Setup provinces.
     */
    return new Ember.RSVP.Promise((resolve) => {
        const provinces = Ember.A([
            Ember.Object.create({
                code: 'AB',
                description: 'Alberta'
            }),
            Ember.Object.create({
                code: 'BC',
                description: 'British Columbia'
            }),
            Ember.Object.create({
                code: 'MB',
                description: 'Manitoba'
            }),
            Ember.Object.create({
                code: 'NB',
                description: 'New Brunswick'
            }),
            Ember.Object.create({
                code: 'NL',
                description: 'Newfoundland and Labrador'
            }),
            Ember.Object.create({
                code: 'NS',
                description: 'Nova Scotia'
            }),
            Ember.Object.create({
                code: 'NT',
                description: 'Northwest Territories'
            }),
            Ember.Object.create({
                code: 'NU',
                description: 'Nunavut'
            }),
            Ember.Object.create({
                code: 'ON',
                description: 'Ontario'
            }),
            Ember.Object.create({
                code: 'PE',
                description: 'Prince Edward Island'
            }),
            Ember.Object.create({
                code: 'QC',
                description: 'Quebec'
            }),
            Ember.Object.create({
                code: 'SK',
                description: 'Saskatchewan'
            }),
            Ember.Object.create({
                code: 'YK',
                description: 'Yukon'
            })
        ]);
        resolve(provinces);
    }).then((provinces) => {
        this.set('provinces', provinces);
    });
},

/**
 * Setup controller hook.
 * @param controller the controller
 * @param model The model
 */
setupController(controller, model) {
    this._super(controller, model);
    controller.set('provinces', this.get('provinces'));
}

2 - Controller/Route Actions

Here I mostly just either going to different route or displaying error message, is this something that should be unit tested? if so how?

actions: {
    /**
     * Go previous step
     */
    back() {
        this.transitionToRoute('new.step1');
    },
    /**
     * Go to next step.
     */
    next() {
        this.get('model').save().then(() => {
            this.transitionToRoute('new.step3');
        }).catch(() => {
            this.get('notificationService')
                .notifyError('common.error.system_error');
        });
    }
}
jpoiri
  • 347
  • 1
  • 8

1 Answers1

2

You can test going to different routes or error messages with acceptance tests. This would cover your route's actions and all the model hooks.

If you wanted to test your route's actions, unit tests are a good idea. You can send an action to your route and see what you get back.

As in this example:

// Now use the routes send method to test the actual action route.send('displayAlert', expectedTextBar);

It's a good idea to break your code up into smaller chunks that you can test individually. As the documentation says: "separating code into smaller chunks (or "concerns"), allows it to be more readily isolated for testing, which in turn allows you to catch bugs more easily."

If your route is getting too complex and hard to test, it may be a sign that you need to abstract some code out into a service or something. This will make testing easier also.

As a side note: I see you're returning an array of objects in your afterModel hook. That will work just fine without the using RSVP.

Hope that helps.

Rimian
  • 36,864
  • 16
  • 117
  • 117