test: 💍 add tests for adapter and serializer scope options

This commit is contained in:
Randall Morey committed 2020-09-03 14:48:59 -04:00
1 parent bce27df1f4
commit 2eeb73b622
6 files changed
+76 -8

No files matched your search

+2 -3
View File
@@ -41,7 +41,7 @@ function prenormalizeArrayResponse(response) {
export default class ApplicationAdapter extends RESTAdapter.extend(
AdapterBuildURLMixin
) {
// =attributes
/**
@@ -84,7 +84,6 @@ export default class ApplicationAdapter extends RESTAdapter.extend(
*/
urlPrefix(path, parentURL, modelName, id, snapshot) {
const prefix = super.urlPrefix(...arguments);
const isScope = modelName === 'scope';
let scopePath = '';
let scopeID = '';
if (snapshot && this.hasScopePrefix) {
@@ -101,7 +100,7 @@ export default class ApplicationAdapter extends RESTAdapter.extend(
// Only non-scope resources need a scope path, since scope resources
// aren't technically "scoped" the same way.
// Ensure a slash is added between prefix + scope path if needed.
if (!isScope && scopeID && prefix.charAt(prefix.length - 1) !== '/') {
if (scopeID && prefix.charAt(prefix.length - 1) !== '/') {
scopePath = `/scopes/${scopeID}`;
}
return `${prefix}${scopePath}`;
+10
View File
@@ -3,6 +3,16 @@ import { serializeIntoHash } from '@ember-data/adapter/-private';
export default class ScopeAdapter extends ApplicationAdapter {
// =attributes
/**
* Scopes do not include a scope prefix.
* @override
*/
hasScopePrefix = false;
// =methods
/**
* Identical to default method except that we pass a query parameter
* indicating the parent scope.
@@ -35,14 +35,16 @@ module('Unit | Adapter | application', function (hooks) {
assert.equal(prefix, `v1/scopes/${scopeID}`);
});
test('it does not prefixes URLs with `scopeID` for scope resources', function (assert) {
assert.expect(1);
test('it does not prefix URLs with `scopeID` when hasScopePrefix is `false`', function (assert) {
assert.expect(2);
const scopeID = 'global';
const adapter = this.owner.lookup('adapter:application');
const prefix = adapter.urlPrefix(null, null, 'scope', null, {
adapter.hasScopePrefix = false;
const prefix = adapter.urlPrefix(null, null, null, null, {
adapterOptions: { scopeID }
});
assert.equal(prefix, `v1`);
assert.ok(config.api.namespace);
assert.equal(prefix, 'v1');
});
test('it generates correct default URL suffixes', function (assert) {
@@ -6,6 +6,16 @@ module('Unit | Adapter | scope', function(hooks) {
setupTest(hooks);
setupMirage(hooks);
test('it does not prefix URLs with `scopeID` for scope resources', function (assert) {
assert.expect(1);
const scopeID = 'global';
const adapter = this.owner.lookup('adapter:scope');
const prefix = adapter.urlPrefix(null, null, 'scope', null, {
adapterOptions: { scopeID }
});
assert.equal(prefix, `v1`);
});
test('it generates correct createRecord URLs with a scope_id query parameter', function (assert) {
assert.expect(1);
this.server.post('/v1/scopes', (schema, { queryParams }) =>{
@@ -29,6 +29,48 @@ module('Unit | Serializer | application', function (hooks) {
});
});
test('it does not serialize scope_id when serializeScopeID is false', function (assert) {
assert.expect(2);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('user');
const record = store.createRecord('user', {
name: 'User',
description: 'Description',
scope: {
scope_id: 'global',
type: 'global'
}
});
const serializedRecord = record.serialize();
assert.equal(serializer.serializeScopeID, false);
assert.deepEqual(serializedRecord, {
name: 'User',
description: 'Description'
});
});
test('it does serialize scope_id when serializeScopeID is true', function (assert) {
assert.expect(2);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('user');
serializer.serializeScopeID = true;
const record = store.createRecord('user', {
name: 'User',
description: 'Description',
scope: {
scope_id: 'global',
type: 'global'
}
});
const serializedRecord = record.serialize();
assert.equal(serializer.serializeScopeID, true);
assert.deepEqual(serializedRecord, {
name: 'User',
description: 'Description',
scope_id: 'global'
});
});
test('it serializes non-nullish version fields', function (assert) {
assert.expect(2);
const store = this.owner.lookup('service:store');
@@ -15,6 +15,10 @@ module('Unit | Serializer | target', function(hooks) {
{ host_set_id: '1', host_catalog_id: '2' },
{ host_set_id: '3', host_catalog_id: '4' }
],
scope: {
scope_id: 'org_1',
type: 'org'
},
version: 1
});
const snapshot = record._createSnapshot();
@@ -24,7 +28,8 @@ module('Unit | Serializer | target', function(hooks) {
name: 'User',
description: 'Description',
version: 1,
protocol: null
protocol: null,
scope_id: 'org_1'
});
});