Jovica Šuša
Jovica Šuša

Reputation: 622

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());
  }); 
});

Upvotes: 0

Views: 159

Answers (1)

Sam Selikoff
Sam Selikoff

Reputation: 12694

If it's many-to-many, you should explicitly create a join record, as direct m2m relationship support does not yet exist.

// mirage/models/user.js
import { Model, hasMany } from 'ember-cli-mirage';

export default Model.extend({

  userResources: hasMany()

});

// mirage/models/resource.js
import { Model, hasMany } from 'ember-cli-mirage';

export default Model.extend({

  userResources: hasMany()

});

// mirage/models/user-resource.js
import { Model, belongsTo } from 'ember-cli-mirage';

export default Model.extend({

  user: belongsTo(),
  resource: belongsTo()

});

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

  // create the join records
  resources.forEach(resource => {
    server.create('user-resource', { user, resource });
  });

  loginUser(user.email);

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

If you need to mock an endpoint that exposes the m2m directly it will take a bit more work. But in general I find that if your Ember app exposes CRUD operations on the relationship, it's good to expose the join record, too. Makes things simpler.

That being said, Mirage will eventually support m2m relationships.

Upvotes: 1

Related Questions