-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #3603 - Validate JSON API documents returned by serializer#norm…
…alizeResponse
- Loading branch information
1 parent
fb79a08
commit 99feb94
Showing
2 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/ember-data/tests/integration/store/json-api-validation-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
var Person, store, env; | ||
var run = Ember.run; | ||
|
||
module("integration/store/json-validation", { | ||
setup: function() { | ||
Person = DS.Model.extend({ | ||
updatedAt: DS.attr('string'), | ||
name: DS.attr('string'), | ||
firstName: DS.attr('string'), | ||
lastName: DS.attr('string') | ||
}); | ||
|
||
env = setupStore({ | ||
person: Person | ||
}); | ||
store = env.store; | ||
}, | ||
|
||
teardown: function() { | ||
run(store, 'destroy'); | ||
} | ||
}); | ||
|
||
test("when normalizeResponse returns undefined (or doesn't return), throws an error", function() { | ||
|
||
env.registry.register('serializer:person', DS.Serializer.extend({ | ||
normalizeResponse() {} | ||
})); | ||
|
||
env.registry.register('adapter:person', DS.Adapter.extend({ | ||
findRecord() { | ||
return Ember.RSVP.resolve({}); | ||
} | ||
})); | ||
|
||
throws(function () { | ||
run(function() { | ||
store.find('person', 1); | ||
}); | ||
}, /Top level of a JSON API document must be an object/); | ||
}); | ||
|
||
test("when normalizeResponse returns null, throws an error", function() { | ||
|
||
env.registry.register('serializer:person', DS.Serializer.extend({ | ||
normalizeResponse() {return null;} | ||
})); | ||
|
||
env.registry.register('adapter:person', DS.Adapter.extend({ | ||
findRecord() { | ||
return Ember.RSVP.resolve({}); | ||
} | ||
})); | ||
|
||
throws(function () { | ||
run(function() { | ||
store.find('person', 1); | ||
}); | ||
}, /Top level of a JSON API document must be an object/); | ||
}); | ||
|
||
|
||
test("when normalizeResponse returns an empty object, throws an error", function() { | ||
|
||
env.registry.register('serializer:person', DS.Serializer.extend({ | ||
normalizeResponse() {return {};} | ||
})); | ||
|
||
env.registry.register('adapter:person', DS.Adapter.extend({ | ||
findRecord() { | ||
return Ember.RSVP.resolve({}); | ||
} | ||
})); | ||
|
||
throws(function () { | ||
run(function() { | ||
store.find('person', 1); | ||
}); | ||
}, /One or more of the following keys must be present/); | ||
}); | ||
|
||
test("when normalizeResponse returns a document with both data and errors, throws an error", function() { | ||
|
||
env.registry.register('serializer:person', DS.Serializer.extend({ | ||
normalizeResponse() { | ||
return { | ||
data: [], | ||
errors: [] | ||
}; | ||
} | ||
})); | ||
|
||
env.registry.register('adapter:person', DS.Adapter.extend({ | ||
findRecord() { | ||
return Ember.RSVP.resolve({}); | ||
} | ||
})); | ||
|
||
throws(function () { | ||
run(function() { | ||
store.find('person', 1); | ||
}); | ||
}, /cannot both be present/); | ||
}); |