Skip to content

Commit

Permalink
add a test for meta and links in json:api
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Feb 4, 2022
1 parent b4cf8d8 commit 0e6619e
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions packages/-ember-data/tests/acceptance/record-links-meta-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { module, test } from 'qunit';
import { resolve } from 'rsvp';

import { setupRenderingTest } from 'ember-qunit';

import JSONAPIAdapter from '@ember-data/adapter/json-api';
import Model, { attr } from '@ember-data/model';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import Store from '@ember-data/store';

class ModelWithCollision extends Model {
@attr name;
@attr meta;
@attr links;
}

class ModelWithoutCollision extends Model {
@attr name;
}

class TestAdapter extends JSONAPIAdapter {
findRecord(store, { modelName }, id) {
let data = {
id,
type: modelName,
attributes: {
name: 'Contraption',
},
links: {
self: 'http://example.com/api/with-collisions/1',
},
meta: {
count: 8,
},
};

if (modelName === 'with-collision') {
data.attributes.links = 'no-links';
data.attributes.meta = 996;
}
return resolve({
data,
});
}
}

module('acceptance/record-links-meta', function (hooks) {
setupRenderingTest(hooks);

hooks.beforeEach(function () {
let { owner } = this;
owner.register('service:store', Store);
owner.register('model:with-collision', ModelWithCollision);
owner.register('model:without-collision', ModelWithoutCollision);
owner.register('adapter:application', TestAdapter);
owner.register('serializer:application', JSONAPISerializer);
});

test('JSONAPISerializer preserves the links and meta', async function (assert) {
let store = this.owner.lookup('service:store');

let adapter = store.adapterFor('application');
let serializer = store.serializerFor('application');
let model = store.modelFor('with-collision');

let data = await adapter.findRecord(store, model, 'id-1');

let result = serializer.normalizeResponse(store, model, data, 'id-1', 'findRecord');

assert.deepEqual(
result.data.links,
{
self: 'http://example.com/api/with-collisions/1',
},
'top level resource links are preserved'
);
assert.deepEqual(result.data.attributes.links, 'no-links', 'attribute members named "links" are preserved');

assert.deepEqual(
result.data.meta,
{
count: 8,
},
'top level resource meta is preserved'
);

assert.deepEqual(result.data.attributes.meta, 996, 'attribute members named "meta" are preserved');
});
});

0 comments on commit 0e6619e

Please sign in to comment.