ember-cli-mirage Tips and Tricks
Lock randomly generated data from faker for each test run for a consistent experience
// <project>/mirage/config.js
import { faker } from 'ember-cli-mirage'
// Lock randomly generated seed data from faker
faker.seed(123);
export default function() {
// Used for logging
window.server = this;
this.get('posts');
}
Even though we are setting a generic this.resource('posts');
handler. Mirage will still be smart enough to use the
custom this.patch('posts/:id')
to override the default this.resource('posts');
handler.
// <project>/mirage/config.js
export default function() {
this.resource('posts');
this.patch('posts/:id', function(schema, request) {
let post = schema.posts.find(request.params.id);
let attrs = this.normalizedRequestAttrs();
post.update(attrs);
// Simply return the model or the collection, in this case the `post` model.
// Mirage will take care of the rest by serializing and responding to the
// AJAX request from the front end with the appropriately serialized data.
return post;
});
}
// Browser Console:
// shows the current state of mirage's data in realtime
server.db.dump();
// get all of the ids for each tag associated with the first post
server.db.posts[0].tagIds;
// get all of the ids for each post associated with the first tag
server.db.tags[0].postIds;