Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4828

Ember Mirage custom route

$
0
0

@belgoros wrote:

I can’t figure out why Mirage fails to mock a request and I’m getting the error when running an acceptance test:

#tests/acceptance/dashboard-test.js

import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { authenticateSession } from 'ember-simple-auth/test-support';
import setupMirageTest from 'ember-cli-mirage/test-support/setup-mirage';
import { setupWindowMock } from 'ember-window-mock';

module('Acceptance | Dashboard', function(hooks) {
  setupWindowMock(hooks);
  setupApplicationTest(hooks);
  setupMirageTest(hooks);

  test('Authenticated users can visit /dashboard', async function(assert) {
    let shop = this.server.create('shop');
    this.server.create('user', { shop });

    await authenticateSession({
      access_token: 'abcdDEF',
      token_type: 'Bearer'
    });

    await visit('/dashboard');

    assert.equal(currentURL(), '/dashboard', 'user is on dashboard page');
  });
});

The error happens when calling the following function in the application route:

#routes/application.js

_loadCurrentUser() {
    return this.get('currentUser').loadCurrentUser().catch(() => this.get('session').invalidate());
  },

Here is how loadCurrentUser function is defined in current-user service:

#services/current-user.js

export default Service.extend({
  session:        service(),
  store:          service(),
  flashMessages:  service(),
  intl:           service(),
  currentShop:    service(),
  shopService:    service(),

  async loadCurrentUser() {
    this.get('flashMessages').clearMessages();
    console.log('++++++++++++++ #loadCurrentUser: authenticated -> ' + this.get('session.isAuthenticated'));
    if (this.get('session.isAuthenticated')) {
      let user = await this.get('store').queryRecord('user', { me: true });
      console.log('++++++++++++++ #loadCurrentUser: USER -> ' + user);
      
      if (user) {
        this.get('flashMessages').success(this.get('intl').t('flash.signed_in'));
        this.get('currentShop').setShop(user.get('shop'));
        this.set('user', user);
        this.get('shopService').loadShops();
        return user;
      } else {
        this._respondWithError();
      }

    } else {
      this.get('flashMessages').info(this.get('intl').t('flash.signed_off'));
      return RSVP.resolve();
    }
  },

  _respondWithError() {
    this.get('flashMessages').danger(this.get('intl').t('flash.authentication.error'));
    return RSVP.reject();
  }
});

If I’m not mistaken, the error happens on the line let user = await this.get('store').queryRecord('user', { me: true }); and then catched in application route catch block.

Here is how I defined the routes in Mirage config.js file:

#../mirage/config.js

export default function() {

  this.get('/users/me', (schema) => {
    return schema.users.first;
  });
  
  this.get('/shops');
}

I also defined 2 models in mirage directory: shop and user:

#mirage/models/shop.js

import { Model } from 'ember-cli-mirage';

export default Model.extend({
});


#mirage/models/user.js

import { Model, belongsTo } from 'ember-cli-mirage';

export default Model.extend({
  shop: belongsTo()
});

The factories are also created for User and Shop:

#mirage/factories/user.js

import { Factory, faker, association } from 'ember-cli-mirage';

export default Factory.extend({
  firstName(i) {
      return `${faker.firstName}-${i}`;
  },

  lastName(i) {
      return `${faker.lastName}-${i}`;
  },

  username(i) {
      return `${faker.internet.userName}-${i}`;
  },
  shop: association()

});


#mirage/factories/shop.js

import { Factory, faker } from 'ember-cli-mirage';

export default Factory.extend({
  identifier() {
    return faker.random.number();
  },

  category(i) {
    return `category-${i}`;
  },

  name(i) {
    return `name-${i}`;
  },
});

When running the test, I do have true value for the line:

console.log('++++++++++++++ #loadCurrentUser: authenticated -> ' + this.get('session.isAuthenticated'));

So, I should enter the IF clause. But the line calling user/me end-point just fails as if Mirage CLI knows nothing about this route.

If I modify the catch block to display the catched error as follows:

# routes/application.js

_loadCurrentUser() {
    return this.get('currentUser').loadCurrentUser().catch((e) => console.log('+++++++++ catched: ' + e));
    //return this.get('currentUser').loadCurrentUser().catch(() => this.get('session').invalidate());
  },

it displays:

'\'+++++++++ catched: SyntaxError: Unexpected end of JSON input\'\n'

What am I missing? Thank you

Mirage CLI version: 0.4.15 ember: `3.8.2`` Thank you

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4828

Trending Articles