Skip to content

Instantly share code, notes, and snippets.

@richmolj
Created August 31, 2016 13:56
Show Gist options
  • Select an option

  • Save richmolj/2f75f180da2b6a5966b6d9fcb94ae4ed to your computer and use it in GitHub Desktop.

Select an option

Save richmolj/2f75f180da2b6a5966b6d9fcb94ae4ed to your computer and use it in GitHub Desktop.
import Ember from 'ember';
import DS from 'ember-data';
import moduleForAcceptance from '../../../tests/helpers/module-for-acceptance';
import NestedRelationsMixin from 'ember-data-extensions/mixins/nested-relations';
import ModelMixin from 'ember-data-extensions/mixins/model';
QUnit.dump.maxDepth = 999999999;
let serializer = null;
let store = null;
const State = DS.Model.extend(ModelMixin, NestedRelationsMixin, {
name: DS.attr('string')
});
const Author = DS.Model.extend(ModelMixin, NestedRelationsMixin, {
name: DS.attr('string'),
state: DS.belongsTo()
});
const User = DS.Model.extend(ModelMixin, NestedRelationsMixin, {
name: DS.attr('string')
});
const Tag = DS.Model.extend(ModelMixin, NestedRelationsMixin, {
name: DS.attr('string'),
creator: DS.belongsTo('user')
});
const Genre = DS.Model.extend(ModelMixin, NestedRelationsMixin, {
name: DS.attr('string')
});
const Post = DS.Model.extend(ModelMixin, NestedRelationsMixin, {
title: DS.attr('string'),
genre: DS.belongsTo(),
author: DS.belongsTo(),
tags: DS.hasMany()
});
let TestSerializer = DS.JSONAPISerializer.extend(NestedRelationsMixin);
let TestStore = DS.Store.extend();
// Ripped from ember-model-fragments
// Ideally this uses Ember.getOwner...
const getOwner = function(context) {
let _context = context.application.__deprecatedInstance__;
if (!_context || !_context.lookup) {
_context = context.application.__container__;
}
return _context;
};
moduleForAcceptance('Unit | Mixin | nested-relations', {
beforeEach() {
getOwner(this).register('service:store', TestStore);
store = getOwner(this).lookup('service:store');
getOwner(this).register('test-container:test-serializer', TestSerializer);
serializer = getOwner(this).lookup('test-container:test-serializer');
serializer.store = store;
getOwner(this).register('model:post', Post);
getOwner(this).register('model:tag', Tag);
getOwner(this).register('model:author', Author);
getOwner(this).register('model:genre', Genre);
getOwner(this).register('model:state', State);
getOwner(this).register('model:user', User);
Ember.run.begin();
},
afterEach() {
Ember.run.end();
}
});
const serialize = function(record, adapterOptions) {
let snapshot = record._internalModel.createSnapshot({
adapterOptions: adapterOptions
});
let json = serializer.serialize(snapshot, {});
return json;
};
const seedPostWithAuthor = function() {
store.pushPayload({
data: {
type: 'posts',
id: 1,
relationships: {
author: {
data: {
type: 'authors',
id: 2
}
}
}
},
included: [
{
type: 'authors',
id: 2,
attributes: { name: 'Joe Author' }
}
]
});
};
test('it serializes has one marked for deletion correctly', function(assert) {
seedPostWithAuthor();
let post = store.peekRecord('post', 1);
post.get('author').set('markedForDeletion', true);
let json = serialize(post, { attributes: false, relationships: { author: {} } });
let expectedJSON = {
data: {
id: '1',
type: 'posts',
relationships: {
author: {
data: {
id: '2',
type: 'authors',
attributes: {
_delete: true
}
}
}
}
}
};
assert.deepEqual(json, expectedJSON, 'has correct json');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment