fix: 🐛 prenormalize findAll responses in application adapter
Ember Data fetch-manager will throw an error if an array resposne is an
empty object before it reaches the serializer. Thus we need to
intercept the response in the adapter and "prenormalize" it to contain
an empty items array `{items: []}`.
This commit is contained in:
7 files changed
+120
No files matched your search
@@ -0,0 +1,26 @@
|
||||
export default function() {
|
||||
|
||||
// These comments are here to help you get started. Feel free to delete them.
|
||||
|
||||
/*
|
||||
Config (with defaults).
|
||||
|
||||
Note: these only affect routes defined *after* them!
|
||||
*/
|
||||
|
||||
// this.urlPrefix = ''; // make this `http://localhost:8080`, for example, if your API is on a different server
|
||||
// this.namespace = ''; // make this `/api`, for example, if your API is namespaced
|
||||
// this.timing = 400; // delay for each request, automatically set to 0 during testing
|
||||
|
||||
/*
|
||||
Shorthand cheatsheet:
|
||||
|
||||
this.get('/posts');
|
||||
this.post('/posts');
|
||||
this.get('/posts/:id');
|
||||
this.put('/posts/:id'); // or this.patch
|
||||
this.del('/posts/:id');
|
||||
|
||||
https://www.ember-cli-mirage.com/docs/route-handlers/shorthands
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export default function(/* server */) {
|
||||
|
||||
/*
|
||||
Seed your development database using your factories.
|
||||
This data will not be loaded in your tests.
|
||||
*/
|
||||
|
||||
// server.createList('post', 10);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { JSONAPISerializer } from 'ember-cli-mirage';
|
||||
|
||||
export default JSONAPISerializer.extend({
|
||||
});
|
||||
@@ -3,9 +3,12 @@ import { setupTest } from 'ember-qunit';
|
||||
import config from 'ember-get-config';
|
||||
import RESTAdapter from '@ember-data/adapter/rest';
|
||||
import { InvalidError } from '@ember-data/adapter/error';
|
||||
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
||||
import { Response } from 'miragejs';
|
||||
|
||||
module('Unit | Adapter | application', function (hooks) {
|
||||
setupTest(hooks);
|
||||
setupMirage(hooks);
|
||||
|
||||
test('its namespace is equal to the configured namespace', function (assert) {
|
||||
assert.expect(2);
|
||||
@@ -24,12 +27,23 @@ module('Unit | Adapter | application', function (hooks) {
|
||||
assert.expect(1);
|
||||
const adapter = this.owner.lookup('adapter:application');
|
||||
// TODO this is icky, should be changed to a spy or stub
|
||||
const originalAjax = RESTAdapter.prototype.ajax;
|
||||
RESTAdapter.prototype.ajax = (url, type) => {
|
||||
assert.equal(type, 'PATCH');
|
||||
RESTAdapter.prototype.ajax = originalAjax;
|
||||
};
|
||||
adapter.ajax('/', 'PUT');
|
||||
});
|
||||
|
||||
test('it prenormalizes "empty" responses into a form that the fetch-manager will not reject', async function (assert) {
|
||||
assert.expect(1);
|
||||
const adapter = this.owner.lookup('adapter:project');
|
||||
const store = this.owner.lookup('service:store');
|
||||
this.server.get('/v1/projects', () => new Response({}));
|
||||
const prenormalized = await adapter.findAll(store, {modelName: 'project'}, null, []);
|
||||
assert.deepEqual(prenormalized, {items: []});
|
||||
});
|
||||
|
||||
test('it correctly identifies 400 responses as invalid', function (assert) {
|
||||
assert.expect(2);
|
||||
const adapter = this.owner.lookup('adapter:application');
|
||||
|
||||
Reference in new issue
Block a user