Skip to content

Instantly share code, notes, and snippets.

@Herriau
Last active January 18, 2017 19:37
Show Gist options
  • Save Herriau/1d60eabde01623c875a0a278d5d186bd to your computer and use it in GitHub Desktop.
Save Herriau/1d60eabde01623c875a0a278d5d186bd to your computer and use it in GitHub Desktop.
Mirage HasMany Issue
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Mirage from 'ember-cli-mirage';
export default function() {
window.server = this;
this.get('/projects/:id');
};
import { Factory } from 'ember-cli-mirage';
export default Factory.extend({
name(i) {
return `Project #${i}`;
}
});
import { Model, hasMany } from 'ember-cli-mirage';
export default Model.extend({
parents: hasMany('project')
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { hasMany } from "ember-data/relationships";
export default Model.extend({
name: attr('string'),
parents: hasMany('project', { async: true }),
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('Testing the has many model generation');
test('testing has-many', function(assert) {
let project1 = server.create('project'),
project2 = server.create('project', {
parents: [project1]
});
visit('/');
getService('store').find('project', 2)
.then(project => project.get('parents'))
.then(parents => {
assert.equal(parents.get('length'), 1);
});
});
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import Em from 'ember';
export default Em.Test.registerHelper('getService', function(app, serviceName) {
return app.__container__.lookup(`service:${serviceName}`);
});
import { module } from 'qunit';
import Ember from 'ember';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
const { RSVP: { Promise } } = Ember;
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
return options.beforeEach.apply(this, arguments);
}
},
afterEach() {
let afterEach = options.afterEach && options.afterEach.apply(this, arguments);
return Promise.resolve(afterEach).then(() => 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';
import getService from './get-service';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
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.10.7",
"ENV": {
"ember-cli-mirage": {
"enabled": true
}
},
"EmberENV": {
"FEATURES": {
"ds-finder-include": true
}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.10.0",
"ember-data": "2.10.0",
"ember-template-compiler": "2.10.0",
"ember-testing": "2.10.0"
},
"addons": {
"ember-cli-mirage": "0.3.0-beta.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment