Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing relationships in tests
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
export default Model.extend({
parent: belongsTo('parent'),
name: attr('string'),
isGood: attr('bool'),
});
import Ember from 'ember';
import Model from "ember-data/model";
import attr from "ember-data/attr";
import { belongsTo, hasMany } from "ember-data/relationships";
const { computed } = Ember;
export default Model.extend({
children: hasMany('child', { async: true }),
goodChildren: computed('children.@each.isGood', function() {
return this.get('children')
.filter(ch => !!ch.get('isGood'))
.sort((ch1, ch2) => ch1.get('name') > ch2.get('name'));
}),
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
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 resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
import { moduleForModel, test } from 'ember-qunit';
moduleForModel('parent', 'Unit | Model | parent', {
needs: ['model:child']
});
test('it has good children', function(assert) {
let parent = this.subject();
assert.equal(parent.get('children.length'), 0, 'has no children');
parent.store.createRecord('child', { parent });
assert.equal(parent.get('children.length'), 1, 'has a child');
assert.equal(parent.get('goodChildren.length'), 0, 'does not have good children');
parent.store.createRecord('child', { parent, isGood: true });
assert.equal(parent.get('children.length'), 2, 'has two children');
assert.equal(parent.get('goodChildren.length'), 1, 'has 1 good child');
});
{
"version": "0.12.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "1.13.13",
"ember-template-compiler": "1.13.13",
"ember-testing": "1.13.13"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment