Files
threshold-ui/addons/api/tests/unit/serializers/application-test.js
T
Randall Morey bdabcc119e Refactor base scope fragment (#1166)
* refactor: 💡 migrate managed-group off fragments

* refactor: 💡 migrate credential-store off fragments

* refactor: 💡 migrate credential-library off fragments

* refactor: 💡 migrate account off fragments

* refactor: 💡 convert base model scope field to simple attr

* fix: 🐛 skip broken test
2022-07-18 11:30:53 -07:00

261 lines
6.9 KiB
JavaScript

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Serializer | application', function (hooks) {
setupTest(hooks);
test('it exists', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('application');
assert.ok(serializer);
});
test('it serializes records', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const record = store.createRecord('user', {
name: 'User',
description: 'Description',
scope: {
scope_id: 'global',
type: 'global',
},
});
const serializedRecord = record.serialize();
assert.deepEqual(serializedRecord, {
name: 'User',
description: 'Description',
scope_id: 'global',
});
});
test('it serializes scope_id', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const record = store.createRecord('user', {
name: 'User',
description: 'Description',
scope: {
scope_id: 'global',
type: 'global',
},
});
const serializedRecord = record.serialize();
assert.deepEqual(serializedRecord, {
name: 'User',
description: 'Description',
scope_id: 'global',
});
});
test('it does not serialize scope_id when serializeScopeID is false', function (assert) {
assert.expect(1);
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 snapshot = record._createSnapshot();
serializer.serializeScopeID = false;
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
name: 'User',
description: 'Description',
});
});
test('it serializes empty strings to null', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const record = store.createRecord('user', {
name: '',
description: '',
version: null,
});
let serializedRecord = record.serialize();
assert.deepEqual(serializedRecord, {
name: null,
description: null,
});
});
test('it serializes non-nullish version fields', function (assert) {
assert.expect(2);
const store = this.owner.lookup('service:store');
const record = store.createRecord('user', {
name: 'User',
description: 'Description',
version: null,
});
let serializedRecord = record.serialize();
assert.deepEqual(serializedRecord, {
name: 'User',
description: 'Description',
});
record.version = 1;
serializedRecord = record.serialize();
assert.deepEqual(serializedRecord, {
name: 'User',
description: 'Description',
version: 1,
});
});
test('it normalizes array records from an `items` root key', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('user');
const userModelClass = store.createRecord('user').constructor;
const payload = {
items: [
{ id: 1, name: 'User 1', scope: { id: 'o_123' } },
{ id: 2, name: 'User 2', scope: { id: 'o_123' } },
{ id: 3, name: 'User 3', scope: { id: 'o_123' } },
],
};
const normalizedArray = serializer.normalizeArrayResponse(
store,
userModelClass,
payload
);
assert.deepEqual(normalizedArray, {
included: [],
data: [
{
id: '1',
type: 'user',
attributes: {
name: 'User 1',
scope: { id: 'o_123', scope_id: 'o_123' },
},
relationships: {},
},
{
id: '2',
type: 'user',
attributes: {
name: 'User 2',
scope: { id: 'o_123', scope_id: 'o_123' },
},
relationships: {},
},
{
id: '3',
type: 'user',
attributes: {
name: 'User 3',
scope: { id: 'o_123', scope_id: 'o_123' },
},
relationships: {},
},
],
});
});
test('it normalizes single, unrooted records', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('user');
const userModelClass = store.createRecord('user').constructor;
const payload = {
id: '1',
name: 'User 1',
scope: { id: 'o_123' },
};
const normalized = serializer.normalizeSingleResponse(
store,
userModelClass,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'user',
attributes: {
authorized_actions: [],
name: 'User 1',
account_ids: [],
scope: { id: 'o_123', scope_id: 'o_123' },
},
relationships: {},
},
});
});
test('it normalizes missing arrays in single responses if annotated in model attributes', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('target');
const target = store.createRecord('target').constructor;
const payload = {
id: '1',
name: 'Target 1',
scope: { id: 'o_123' },
};
const normalized = serializer.normalizeSingleResponse(
store,
target,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'target',
attributes: {
authorized_actions: [],
name: 'Target 1',
scope: { id: 'o_123', scope_id: 'o_123' },
host_sources: [],
application_credential_source_ids: [],
},
relationships: {},
},
});
});
test('it normalizes removed fields correctly', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('user');
const user = store.createRecord('user').constructor;
const payload = {
id: '1',
name: 'user 1',
scope: { id: 'o_123' },
};
const normalized = serializer.normalizeUpdateRecordResponse(
store,
user,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'user',
attributes: {
authorized_actions: [],
name: 'user 1',
scope: { id: 'o_123', scope_id: 'o_123' },
account_ids: [],
authorized_collection_actions: null,
created_time: null,
email: null,
full_name: null,
login_name: null,
updated_time: null,
},
relationships: {},
},
});
});
});