feat: 🎸 add group custom methods for add/remove members
This commit is contained in:
5 files changed
+295
-5
No files matched your search
@@ -1,12 +1,133 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
|
||||
|
||||
module('Unit | Model | group', function(hooks) {
|
||||
setupTest(hooks);
|
||||
setupMirage(hooks);
|
||||
|
||||
test('it exists', function(assert) {
|
||||
let store = this.owner.lookup('service:store');
|
||||
let model = store.createRecord('group', {});
|
||||
assert.ok(model);
|
||||
test('it has a `members` array of resolved model instances (if those instances are already in the store)', function(assert) {
|
||||
assert.expect(4);
|
||||
const store = this.owner.lookup('service:store');
|
||||
store.push({
|
||||
data: {
|
||||
id: 'group_123',
|
||||
type: 'group',
|
||||
attributes: {
|
||||
member_ids: [ '1', '2', ]
|
||||
}
|
||||
}
|
||||
});
|
||||
const group = store.peekRecord('group', 'group_123');
|
||||
assert.equal(group.member_ids.length, 2, 'Group has two entires in member_ids');
|
||||
assert.equal(group.members.length, 0, 'Group has no resolved members because they are not loaded yet');
|
||||
store.push({
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'user',
|
||||
attributes: {}
|
||||
}
|
||||
});
|
||||
store.push({
|
||||
data: {
|
||||
id: '2',
|
||||
type: 'user',
|
||||
attributes: {}
|
||||
}
|
||||
});
|
||||
// eslint-disable-next-line no-self-assign
|
||||
group.member_ids = [...group.member_ids];
|
||||
assert.equal(group.member_ids.length, 2, 'Group has two entires in member_ids');
|
||||
assert.equal(group.members.length, 2, 'Group has two resolved members');
|
||||
});
|
||||
|
||||
test('it has an `addMembers` method that groups a specific POST API endpoint and serialization', async function (assert) {
|
||||
assert.expect(1);
|
||||
this.server.post('/v1/scopes/o_1/groups/123abc:add-members', (schema, request) => {
|
||||
const body = JSON.parse(request.requestBody);
|
||||
assert.deepEqual(body, {
|
||||
member_ids: ['123_abc', 'foobar'],
|
||||
version: 1
|
||||
});
|
||||
});
|
||||
const store = this.owner.lookup('service:store');
|
||||
store.push({
|
||||
data: {
|
||||
id: '123abc',
|
||||
type: 'group',
|
||||
attributes: {
|
||||
name: 'Group',
|
||||
description: 'Description',
|
||||
member_ids: [ '1', '2' ],
|
||||
version: 1,
|
||||
scope: {
|
||||
scope_id: 'o_1',
|
||||
type: 'scope'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
const model = store.peekRecord('group', '123abc');
|
||||
await model.addMembers(['123_abc', 'foobar']);
|
||||
});
|
||||
|
||||
test('it has a `removeMembers` method that groups a specific POST API endpoint and serialization', async function (assert) {
|
||||
assert.expect(1);
|
||||
this.server.post('/v1/scopes/o_1/groups/123abc:remove-members', (schema, request) => {
|
||||
const body = JSON.parse(request.requestBody);
|
||||
assert.deepEqual(body, {
|
||||
member_ids: ['1', '3'],
|
||||
version: 1
|
||||
});
|
||||
});
|
||||
const store = this.owner.lookup('service:store');
|
||||
store.push({
|
||||
data: {
|
||||
id: '123abc',
|
||||
type: 'group',
|
||||
attributes: {
|
||||
name: 'group',
|
||||
description: 'Description',
|
||||
member_ids: [ '4', '5' ],
|
||||
version: 1,
|
||||
scope: {
|
||||
scope_id: 'o_1',
|
||||
type: 'scope'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
const model = store.peekRecord('group', '123abc');
|
||||
await model.removeMembers(['1', '3']);
|
||||
});
|
||||
|
||||
test('it has a `removeMember` method that deletes a single member set using `removeMembers` method', async function (assert) {
|
||||
assert.expect(1);
|
||||
this.server.post('/v1/scopes/o_1/groups/123abc:remove-members', (schema, request) => {
|
||||
const body = JSON.parse(request.requestBody);
|
||||
assert.deepEqual(body, {
|
||||
member_ids: ['3'],
|
||||
version: 1
|
||||
});
|
||||
});
|
||||
const store = this.owner.lookup('service:store');
|
||||
store.push({
|
||||
data: {
|
||||
id: '123abc',
|
||||
type: 'group',
|
||||
attributes: {
|
||||
name: 'group',
|
||||
description: 'Description',
|
||||
member_ids: [ '1', '3' ],
|
||||
version: 1,
|
||||
scope: {
|
||||
scope_id: 'o_1',
|
||||
type: 'scope'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
const model = store.peekRecord('group', '123abc');
|
||||
await model.removeMember('3');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'ember-qunit';
|
||||
|
||||
module('Unit | Serializer | group', function(hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
test('it serializes groups normally, without members', function (assert) {
|
||||
assert.expect(1);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('group');
|
||||
const record = store.createRecord('group', {
|
||||
name: 'Group',
|
||||
description: 'Description',
|
||||
member_ids: [ '1', '2', ],
|
||||
version: 1
|
||||
});
|
||||
const snapshot = record._createSnapshot();
|
||||
snapshot.adapterOptions = {};
|
||||
const serializedRecord = serializer.serialize(snapshot);
|
||||
assert.deepEqual(serializedRecord, {
|
||||
name: 'Group',
|
||||
description: 'Description',
|
||||
version: 1
|
||||
});
|
||||
});
|
||||
|
||||
test('it serializes only host sets and version when an `adapterOptions.memberIDs` array is passed', function (assert) {
|
||||
assert.expect(1);
|
||||
const store = this.owner.lookup('service:store');
|
||||
const serializer = store.serializerFor('group');
|
||||
const record = store.createRecord('group', {
|
||||
name: 'Group',
|
||||
description: 'Description',
|
||||
member_ids: [ '1', '2', ],
|
||||
version: 1
|
||||
});
|
||||
const snapshot = record._createSnapshot();
|
||||
snapshot.adapterOptions = {
|
||||
memberIDs: ['4', '5']
|
||||
};
|
||||
const serializedRecord = serializer.serialize(snapshot);
|
||||
assert.deepEqual(serializedRecord, {
|
||||
member_ids: ['4', '5'],
|
||||
version: 1
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user