Skip to content

Instantly share code, notes, and snippets.

@jamesarosen
Created March 4, 2015 06:38
Show Gist options
  • Select an option

  • Save jamesarosen/b6a771820465d867a14d to your computer and use it in GitHub Desktop.

Select an option

Save jamesarosen/b6a771820465d867a14d to your computer and use it in GitHub Desktop.
It's hard to test this Ember-Data Serializer
import DS from "ember-data";
function ensureRoot(rootName, payload) {
var result;
if (payload[rootName] == null) {
result = {};
result[rootName] = payload;
} else {
result = payload;
}
return result;
}
function ensureID(rootName, payload, requestedID) {
if (payload[rootName].id == null) {
payload[rootName].id = requestedID;
}
return payload;
}
export default DS.ActiveModelSerializer.extend({
extractSingle: function(store, type, payload, id, requestType) {
payload = ensureRoot(type.typeKey, payload);
payload = ensureID(type.typeKey, payload, id);
return this._super(store, type, payload, id, requestType);
}
});

My ApplicationSerializer inherits from DS.ActiveModelSerializer, which in turn inherits from DS.RestSerializer

Thus, my extractSingle overrides and calls this one, which is pretty thorny. Most notably, if I want to stub store, I need

  • store.modelFactoryFor -- returns an Ember.Object subclass
  • store.modelFor -- returns a complex key object, the structure of which does not seem to be publicly documented
  • store.serializerFor -- maybe returns an instance of my serializer?
  • store.push, which I could probably have be a no-op since I don't care about side-loads
  • store.pushMany (called if there are relationships), ibid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment