Files
threshold-ui/addons/api/tests/unit/models/group-test.js
T
Zhihe Li 999c21c66a Upgrade eslint (#3175)
* chore: 🤖 Upgrade eslint config for admin ui

* chore: 🤖 Fix eslint issues for admin

* chore: 🤖 Upgrade eslint config for desktop

* chore: 🤖 Fix linting errors in desktop

* chore: 🤖 Upgrade eslint for api addon

* chore: 🤖 Fix eslint issues in api addon

* chore: 🤖 Upgrade eslint for auth addon

* chore: 🤖 Fix eslint issues in auth addon

* chore: 🤖 Upgrade core eslint config

* chore: 🤖 Fix eslint issue for core addon

* chore: 🤖 Upgrade eslint config for rose addon

* chore: 🤖 Fix eslint issue for rose addon

* chore: 🤖 Upgrade e2e eslint config

* chore: 🤖 Fix eslint issues in e2e

* chore: 🤖 Fix CI linting issues

* chore: 🤖 Add back linting server files for the future

* chore: 🤖 Update qunit rules to use extends
2026-02-27 13:32:27 -05:00

173 lines
4.5 KiB
JavaScript

/**
* Copyright IBM Corp. 2021, 2026
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { setupMirage } from 'dummy/tests/helpers/mirage';
module('Unit | Model | group', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
test('it has a `members` array of resolved model instances (if those instances are already in the store)', function (assert) {
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.strictEqual(
group.member_ids.length,
2,
'Group has two entires in member_ids',
);
assert.strictEqual(
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: {},
},
});
group.member_ids = [...group.member_ids];
assert.strictEqual(
group.member_ids.length,
2,
'Group has two entires in member_ids',
);
assert.strictEqual(
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('/groups/123abc:add-members', (schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
member_ids: ['123_abc', 'foobar'],
version: 1,
});
return { id: '123abc' };
});
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('/groups/123abc:remove-members', (schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
member_ids: ['1', '3'],
version: 1,
});
return { id: '123abc' };
});
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('/groups/123abc:remove-members', (schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
member_ids: ['3'],
version: 1,
});
return { id: '123abc' };
});
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');
});
test('it defaults `members_ids` to an empty array when model instance does not define it', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
store.push({
data: {
id: 'group_123',
type: 'group',
},
});
const group = store.peekRecord('group', 'group_123');
assert.strictEqual(
group.member_ids.length,
0,
'Group has empty member_ids by default',
);
});
});