Skip to content

Instantly share code, notes, and snippets.

@snewcomer
Last active September 23, 2016 15:19
Show Gist options
  • Save snewcomer/fec7083a6be365bda161fba846502f3b to your computer and use it in GitHub Desktop.
Save snewcomer/fec7083a6be365bda161fba846502f3b to your computer and use it in GitHub Desktop.
Unit model test
import Ember from 'ember';
const { run } = Ember;
import { moduleForModel, test } from 'ember-qunit';
import startMirage from '../../helpers/setup-mirage-for-integration';
import Pretender from 'pretender';
let expense, store, user, serverz;
moduleForModel('expense', 'Unit | Model | expense', {
// belongsTo User w/ validation from ember-cp-validations
needs: ['model:user', 'validator:presence', 'validator:length', 'validator:format'],
beforeEach: function() {
const container = this.container;
store = container.lookup('service:store');
startMirage(this.container);
// to allow save and start with hasDirtyAttributes === false
serverz = new Pretender(function() {
this.post('/expenses', function() {
var response = {
'data':
{'id': '1', 'type': 'expense', 'attributes': {'id': '1'},
'included': { 'status': {} }},
};
return [201, { 'Content-Type': 'application/vnd.api+json' }, JSON.stringify(response)];
});
});
run(() => {
// Mirage
server.createList('user', 2); // belongsTo
// Ember Data - need access to relationship and dirty tracking methods
user = store.createRecord('user', server.db.users[0]);
expense = store.createRecord('expense', { id: 1, assignee: user, assigneeId: user.get('id') }); // setup belongsTo
expense.save(); // mocked out from pretender call but on the flipside makes the expense model clean (hasDirtyAttributes is false)
});
},
afterEach(){
serverz.shutdown();
}
});
// Dirty and Relationships
test('amount is dirty trackable', function(assert) {
// Ember Data specific
assert.notOk(expense.get('hasDirtyAttributes'));
// I wrote this myself
assert.ok(expense.get('relationshipsIsNotDirty'));
assert.equal(expense.get('amount'), undefined);
run(() => {
expense.set('amount', 'zzwatttt');
});
assert.equal(expense.get('id'), 1);
assert.equal(expense.get('amount'), 'zzwatttt');
assert.ok(expense.get('hasDirtyAttributes'));
assert.ok(expense.get('relationshipsIsNotDirty'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment