Files
threshold-ui/addons/api/tests/unit/serializers/managed-group-test.js
T
Zhihe Li f8429b15e9 Chore/cleanup filter attribute (#1500)
* refactor: 💡 Rename attributes called filter

* refactor: 💡 Remove adapter handling of old attribute errors

* refactor: 💡 Check for undefined in serializer instead of falsy
2022-12-02 13:44:11 -05:00

197 lines
5.5 KiB
JavaScript

import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Serializer | Managed group', function (hooks) {
setupTest(hooks);
test('it serializes correctly on create with no filter provided', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('managed-group');
const record = store.createRecord('managed-group', {
name: 'Group',
description: 'Description',
member_ids: ['1', '2'],
version: 1,
auth_method_id: '1234',
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
name: 'Group',
description: 'Description',
version: 1,
auth_method_id: '1234',
type: null,
attributes: {
filter: null,
},
});
});
test('it serializes correctly on create when filter provided', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('managed-group');
const record = store.createRecord('managed-group', {
name: 'Group',
description: 'Description',
member_ids: ['1', '2'],
version: 1,
auth_method_id: '1234',
type: 'oidc',
filter_string: 'key=value',
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
name: 'Group',
description: 'Description',
version: 1,
auth_method_id: '1234',
type: 'oidc',
attributes: {
filter: 'key=value',
},
});
});
test('it does not serialize version when it has null or undefined value', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('managed-group');
store.push({
data: {
id: '1',
type: 'managed-group',
attributes: {
name: 'Group Test',
description: 'Description for the test',
member_ids: ['1', '2'],
version: undefined,
auth_method_id: '1234',
type: 'oidc',
filter_string: 'key=value',
},
},
});
const record = store.peekRecord('managed-group', '1');
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
name: 'Group Test',
description: 'Description for the test',
auth_method_id: '1234',
type: 'oidc',
attributes: {
filter: 'key=value',
},
});
});
test('it does not serialize readOnly attributes', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('managed-group');
const record = store.createRecord('managed-group', {
name: 'Group',
description: 'Description',
version: 1,
auth_method_id: '1234',
member_ids: ['1', '2'],
created_time: Date.now(),
updated_time: Date.now(),
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
name: 'Group',
description: 'Description',
version: 1,
auth_method_id: '1234',
type: null,
attributes: {
filter: null,
},
});
});
test('it serializes correctly on update', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('managed-group');
store.push({
data: {
id: '1',
type: 'managed-group',
attributes: {
name: 'Group Test',
description: 'Description for the test',
member_ids: ['1', '2'],
version: 1,
auth_method_id: '1234',
type: 'oidc',
filter_string: 'key=value',
},
},
});
const record = store.peekRecord('managed-group', '1');
const snapshot = record._createSnapshot();
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
name: 'Group Test',
description: 'Description for the test',
version: 1,
auth_method_id: '1234',
type: 'oidc',
attributes: {
filter: 'key=value',
},
});
});
test('it normalizes correctly', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('managed-group');
const hostSetModelClass = store.createRecord('managed-group').constructor;
const payload = {
id: '1',
description: 'Description for the test',
version: 1,
auth_method_id: '1234',
type: 'oidc',
attributes: {
filter: 'key=value',
},
};
const normalized = serializer.normalizeSingleResponse(
store,
hostSetModelClass,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
attributes: {
type: 'oidc',
version: 1,
authorized_actions: [],
member_ids: [],
description: 'Description for the test',
auth_method_id: '1234',
filter_string: 'key=value',
},
type: 'managed-group',
relationships: {},
},
});
});
});