lundi 27 juin 2016

Refactoring mock response in ember cli mirage 0.2.x

I'm using ember cli mirage to write some acceptance tests for my Ember app. I succeeded to mock server response for login but I'm not happy how I did it. Ember cli mirage have shorthands for route handlers and I would like to use them but everything I try throws me an error(except this solution). Can someone help me to refactor this response?

this.post('/login', ({ users, resources })=> {
  let user = users.first();

  if(!Ember.isEmpty(resources.first())){
    return {
      data: {
        type: 'user',
        id: user.id,
        attributes: user,
        relationships: {
          resources: {
            data: [
              { id: resources.first().id, type:  'resource' }
            ]
          }
        }
      },
    };
  } else {
    return {
      data: {
        type: 'user',
        id: user.id,
        attributes: user
      }
    };
  }
});

I have both user and resource model and factory defined, with relationships between them in user and resource model(it's many to many relationship). Here's how I create user in tests

test('User can login', function(assert){
  let resources = server.createList('resource', 2),
      user      = server.create('user', {resources: resources});

  loginUser(user.email);
  andThen(()=>{
    assert.ok(find('a:contains("Logout")'));
    assert.equal('resource.content', currentPath());
  }); 
});




Aucun commentaire:

Enregistrer un commentaire