Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save navels/76136f2fa5503925edb4fbae17ec8956 to your computer and use it in GitHub Desktop.
Save navels/76136f2fa5503925edb4fbae17ec8956 to your computer and use it in GitHub Desktop.
Mirage Boilerplate
import Ember from 'ember';
export default Ember.Controller.extend({
});
export default function() {
//window.server = this;
this.get('authors/:id');
};
import { Model, hasMany } from 'ember-cli-mirage';
export default Model.extend({
books: hasMany()
});
import { Model, hasMany } from 'ember-cli-mirage';
export default Model.extend({
tags: hasMany()
});
import { Model, hasMany } from 'ember-cli-mirage';
export default Model.extend({
});
export default function(server) {
// added this line because global wasn't created otherwise.
let nonfiction = server.create('tag', { name: 'Nonfiction' });
let fiction = server.create('tag', { name: 'Fiction' });
let travel = server.create('tag', { name: 'Travel' });
let paperback = server.create('tag', { name: 'paperback' });
let hardcover = server.create('tag', { name: 'hardcover' });
let author = server.create('author', {
name: 'John Steinbeck'
});
let book = author.createBook({
title: 'Of Mice and Men',
tags: [ fiction, paperback ]
});
author.createBook({
title: 'The Grapes of Wrath',
tags: [ fiction, hardcover ]
});
author.createBook({
title: 'Travels with Charley',
tags: [ nonfiction, paperback, travel ]
});
let author = server.create('author', {
name: 'Roald Dahl',
books: [
{
title: 'Willy Wonka',
tags: [ fiction ]
},
{
title: 'James and the Giant Peach',
tags: [ fiction ]
}
]
});
}
import { JSONAPISerializer } from 'ember-cli-mirage';
export default JSONAPISerializer.extend({
});
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr(),
books: DS.hasMany()
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
title: attr(),
tags: hasMany()
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
name: attr()
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findRecord('author', 1, {
include: 'books.tags'
});
}
});
body {
margin: 0;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
h1 {
font-size: 12px;
font-weight: bold;
text-align: center;
text-shadow: none;
background: #2e4068;
color: white;
padding: 20px;
margin: 0;
text-transform: uppercase;
}
p {
font-weight: normal;
}
.pa4 {
padding: 20px;
}
hr {
border-top: none;
border-bottom: 1px solid #ddd;
}
<h1>Mirage boilerplate</h1>
<div class='pa4'>
<p><strong>{{model.name}}</strong>'s Books</p>
{{#each model.books as |book|}}
<hr />
<div>
<p><em>{{book.title}}</em></p>
<p>Tags:</p>
<ul>
{{#each book.tags as |tag|}}
<li>{{tag.name}}</li>
{{/each}}
</ul>
</div>
{{/each}}
</div>
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('Acceptance | Users');
test('I can see the users', function(assert) {
server.create('user', { name: 'Link' });
//visit('/');
andThen(function() {
assert.ok(true);
//assert.ok(find(':contains(Welcome Link!)').length);
});
});
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import { module } from 'qunit';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}
},
afterEach() {
if (options.afterEach) {
options.afterEach.apply(this, arguments);
}
destroyApp(this.application);
}
});
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
export default function startApp(attrs) {
let application;
let attributes = Ember.merge({rootElement: "#test-root"}, config.APP);
attributes = Ember.merge(attributes, attrs); // use defaults, but you can override;
Ember.run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"version": "0.15.0",
"ENV": {
"ember-cli-mirage": {
"enabled": true
}
},
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.2.2",
"ember-template-compiler": "3.2.2",
"ember-testing": "3.2.2"
},
"addons": {
"ember-data": "2.16.3",
"ember-cli-mirage": "0.4.9"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment