mardi 3 mai 2016

Handing errors i.e., 401, 403, etc. for Ember Test

Searching for suggestions on handling errors in an Ember test? Found a closed issue from last year, Allowing rejected promises in tests, which indicated that rejections cannot be conveniently handled. Is this still the case?

Right now, in our application, we are using ajax errors to return an unauthorized error if the user is logged out of our main Rails application.

Route

import Ember from 'ember';
import {isUnauthorizedError, isForbiddenError} from 'ember-ajax/errors';

export default Ember.Route.extend({
  userService: Ember.inject.service('user'),

  activate: function(){

    this.get('userService').facility().then(
      (data) => {
        this.get('controller').set('facility', data.data);
      }, (response) => {
        if (isUnauthorizedError(response)) {
          this.transitionTo('nsqip.failure.401');
        } else if (isForbiddenError(response)) {
          this.transitionTo('nsqip.failure.403');
        } else {
          //TODO implement generic handling
        }
      }
    ),

Acceptance Test

test('generates 401 error', function(assert) {
  visit('/nsqip/reporting');

  httpStubs.stubUser(server, {}, 401);
  httpStubs.stubFacility(server, {});

  httpStubs.stubReport(server, '/api/nsqip/v1/reports/summary.json', []);
  click('button.btn-filter')

  andThen(function(){
    assert.equal(currentURL(), '/users/login');
  });

Report Stub

  stubReport(server, url, data, status) {
    let statusCode = (status) ? status : 200;

    server.get(url, function() {

      return [
        statusCode,
        {'Content-Type': 'application/json'},
        JSON.stringify(data)
      ];
    });
  }




Aucun commentaire:

Enregistrer un commentaire