-
-
Save jelhan/328e1448a6a50e8372f0709b4a4be3f6 to your computer and use it in GitHub Desktop.
Mirage dev boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
const { RESTAdapter } = DS; | |
export default RESTAdapter.extend({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function() { | |
this.get('posts'); | |
this.post('posts'); | |
this.get('posts/:id'); | |
this.get('comments'); | |
this.get('comments/:id'); | |
this.post('comments'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Factory, faker } from 'ember-cli-mirage'; | |
export default Factory.extend({ | |
name() { | |
return faker.name.findName(); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Model, belongsTo } from 'ember-cli-mirage'; | |
export default Model.extend({ | |
post: belongsTo() | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Model, hasMany } from 'ember-cli-mirage'; | |
export default Model.extend({ | |
comments: hasMany('comment') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function(server) { | |
// added this line because global wasn't created otherwise. | |
let post = server.create('post'); | |
server.createList('comment', 2, { post }); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { RestSerializer } from 'ember-cli-mirage'; | |
import { camelize, pluralize, singularize } from 'ember-cli-mirage/utils/inflector'; | |
export default RestSerializer.extend({ | |
keyForForeignKey(relationshipName) { | |
return relationshipName; | |
}, | |
keyForRelationshipIds(type) { | |
return type; | |
}, | |
normalize(payload) { | |
let type = Object.keys(payload)[0]; | |
let attrs = payload[type]; | |
let { belongsToAssociations, hasManyAssociations } = this.registry.schema._registry[type].class.prototype; | |
let jsonApiPayload = { | |
data: { | |
type: pluralize(type) | |
} | |
}; | |
Object.keys(attrs).forEach(key => { | |
if (key === 'id') { | |
// records id | |
jsonApiPayload.data.id = attrs.id; | |
} else if ( | |
belongsToAssociations.hasOwnProperty(key) || | |
hasManyAssociations.hasOwnProperty(key) | |
) { | |
// relationship | |
if (!jsonApiPayload.data.hasOwnProperty('relationships')) { | |
jsonApiPayload.data.relationships = {}; | |
} | |
let association = belongsToAssociations.hasOwnProperty(key) ? belongsToAssociations[key] : hasManyAssociations[key]; | |
let associationType = belongsToAssociations.hasOwnProperty(key) ? 'belongsTo' : 'hasMany'; | |
let associationModel = association.modelName; | |
let relationshipObject = {}; | |
switch (associationType) { | |
case 'belongsTo': | |
relationshipObject.data = { | |
type: associationModel, | |
id: attrs[key] | |
}; | |
break; | |
case 'hasMany': | |
relationshipObject.data = []; | |
attrs[key].forEach((value) => { | |
relationshipObject.data.push({ | |
type: associationModel, | |
id: value | |
}); | |
}); | |
break; | |
} | |
jsonApiPayload.data.relationships[key] = relationshipObject; | |
} else { | |
// attribute | |
if (!jsonApiPayload.data.hasOwnProperty('attributes')) { | |
jsonApiPayload.data.attributes = {}; | |
} | |
jsonApiPayload.data.attributes[dasherize(key)] = attrs[key]; | |
} | |
}); | |
return jsonApiPayload; | |
}, | |
serializeIds: 'always' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Model from "ember-data/model"; | |
import attr from "ember-data/attr"; | |
import { belongsTo, hasMany } from "ember-data/relationships"; | |
export default Model.extend({ | |
post: belongsTo('post') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Model from "ember-data/model"; | |
import attr from "ember-data/attr"; | |
import { belongsTo, hasMany } from "ember-data/relationships"; | |
export default Model.extend({ | |
comments: hasMany('comment') | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
const { RSVP } = Ember; | |
export default Ember.Route.extend({ | |
actions: { | |
saveRecords() { | |
this.store.createRecord('post').save().then((post) => { | |
this.store.createRecord('comment', { post }).save(); | |
}); | |
} | |
}, | |
model() { | |
return this.store.findAll('post'); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DS from 'ember-data'; | |
const { RESTSerializer } = DS; | |
export default RESTSerializer.extend({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default function destroyApp(application) { | |
Ember.run(application, 'destroy'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Resolver from '../../resolver'; | |
import config from '../../config/environment'; | |
const resolver = Resolver.create(); | |
resolver.namespace = { | |
modulePrefix: config.modulePrefix, | |
podModulePrefix: config.podModulePrefix | |
}; | |
export default resolver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import resolver from './helpers/resolver'; | |
import { | |
setResolver | |
} from 'ember-qunit'; | |
setResolver(resolver); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.10.4", | |
"ENV": { | |
"ember-cli-mirage": { | |
"enabled": true | |
} | |
}, | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.9.0", | |
"ember-data": "2.9.0", | |
"ember-template-compiler": "2.9.0", | |
"ember-testing": "2.9.0" | |
}, | |
"addons": { | |
"ember-cli-mirage": "0.3.0-beta.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment