Files
threshold-ui/addons/api/tests/unit/models/target-test.js
T

226 lines
6.2 KiB
JavaScript
Raw Normal View History

2020-08-21 11:48:24 -04:00
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
2020-08-21 11:48:24 -04:00
module('Unit | Model | target', function (hooks) {
2020-08-21 11:48:24 -04:00
setupTest(hooks);
setupMirage(hooks);
2020-08-21 11:48:24 -04:00
2020-09-16 14:29:02 -04:00
test('it has a `hostSets` array of resolved model instances (if those instances are already in the store)', function (assert) {
assert.expect(6);
const store = this.owner.lookup('service:store');
store.push({
data: {
id: '123abc',
type: 'target',
attributes: {
host_sets: [
{ host_set_id: '1', host_catalog_id: '2' },
2020-09-16 14:29:02 -04:00
{ host_set_id: '3', host_catalog_id: '2' },
],
},
},
});
const target = store.peekRecord('target', '123abc');
2020-09-16 14:29:02 -04:00
assert.equal(
target.host_sets.length,
2,
'Target has two entires in host_sets'
);
assert.equal(
target.hostSets.length,
0,
'Target has no resolved hostSets because they are not loaded yet'
);
store.push({
data: {
id: '1',
type: 'host-set',
2020-09-16 14:29:02 -04:00
attributes: {},
},
});
store.push({
data: {
id: '3',
type: 'host-set',
2020-09-16 14:29:02 -04:00
attributes: {},
},
});
// Since `hostSets` is computed on `host_sets`, not the store itself,
// it's necessary to do this assignment to kick-off the computed update.
// eslint-disable-next-line no-self-assign
target.host_sets = target.host_sets;
2020-09-16 14:29:02 -04:00
assert.equal(
target.host_sets.length,
2,
'Target has two entires in host_sets'
);
assert.equal(target.hostSets.length, 2, 'Target has two resolved hostSets');
2020-09-16 14:29:02 -04:00
assert.notOk(
target.hostSets[0].hostCatalog,
'Host catalog was not resolved because it is not loaded yet'
);
store.push({
data: {
id: '2',
type: 'host-catalog',
2020-09-16 14:29:02 -04:00
attributes: {},
},
});
// eslint-disable-next-line no-self-assign
target.host_sets = target.host_sets;
assert.ok(target.hostSets[0].hostCatalog, 'Host catalog is resolved');
2020-08-21 11:48:24 -04:00
});
test('it has an `addHostSets` method that targets a specific POST API endpoint and serialization', async function (assert) {
assert.expect(1);
2020-09-03 14:35:05 -04:00
this.server.post('/v1/targets/123abc:add-host-sets', (schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
host_set_ids: ['123_abc', 'foobar'],
2020-09-16 14:29:02 -04:00
version: 1,
});
return { id: '123abc' };
});
const store = this.owner.lookup('service:store');
store.push({
data: {
id: '123abc',
type: 'target',
attributes: {
name: 'Target',
description: 'Description',
host_sets: [
{ host_set_id: '1', host_catalog_id: '2' },
2020-09-16 14:29:02 -04:00
{ host_set_id: '3', host_catalog_id: '4' },
],
version: 1,
scope: {
scope_id: 'o_1',
2020-09-16 14:29:02 -04:00
type: 'scope',
},
},
},
});
const model = store.peekRecord('target', '123abc');
2020-08-26 14:33:26 -04:00
await model.addHostSets(['123_abc', 'foobar']);
});
test('it has a `removeHostSets` method that targets a specific POST API endpoint and serialization', async function (assert) {
assert.expect(1);
2020-09-16 14:29:02 -04:00
this.server.post(
'/v1/targets/123abc:remove-host-sets',
(schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
host_set_ids: ['1', '3'],
version: 1,
});
return { id: '123abc' };
2020-09-16 14:29:02 -04:00
}
);
const store = this.owner.lookup('service:store');
store.push({
data: {
id: '123abc',
type: 'target',
attributes: {
name: 'Target',
description: 'Description',
host_sets: [
{ host_set_id: '1', host_catalog_id: '2' },
2020-09-16 14:29:02 -04:00
{ host_set_id: '3', host_catalog_id: '4' },
],
version: 1,
scope: {
scope_id: 'o_1',
2020-09-16 14:29:02 -04:00
type: 'scope',
},
},
},
});
const model = store.peekRecord('target', '123abc');
await model.removeHostSets(['1', '3']);
});
test('it has a `removeHostSet` method that deletes a single host set using `removeHostSets` method', async function (assert) {
assert.expect(1);
2020-09-16 14:29:02 -04:00
this.server.post(
'/v1/targets/123abc:remove-host-sets',
(schema, request) => {
const body = JSON.parse(request.requestBody);
assert.deepEqual(body, {
host_set_ids: ['3'],
version: 1,
});
return { id: '123abc' };
2020-09-16 14:29:02 -04:00
}
);
const store = this.owner.lookup('service:store');
store.push({
data: {
id: '123abc',
type: 'target',
attributes: {
name: 'Target',
description: 'Description',
host_sets: [
{ host_set_id: '1', host_catalog_id: '2' },
2020-09-16 14:29:02 -04:00
{ host_set_id: '3', host_catalog_id: '4' },
],
version: 1,
scope: {
scope_id: 'o_1',
2020-09-16 14:29:02 -04:00
type: 'scope',
},
},
},
});
const model = store.peekRecord('target', '123abc');
await model.removeHostSet('3');
});
2021-02-05 16:57:06 -05:00
test('it has a `sessions` array of session instances associated with the target (if those instances are already in the store)', function (assert) {
assert.expect(3);
const store = this.owner.lookup('service:store');
store.push({
data: {
id: '123abc',
2021-03-04 15:47:38 -05:00
type: 'target',
},
2021-02-05 16:57:06 -05:00
});
const target = store.peekRecord('target', '123abc');
2021-03-04 15:47:38 -05:00
assert.equal(target.sessions.length, 0);
2021-02-05 16:57:06 -05:00
store.push({
data: {
id: '1',
type: 'session',
2021-03-04 15:47:38 -05:00
attributes: { target_id: '123abc' },
},
2021-02-05 16:57:06 -05:00
});
store.push({
data: {
id: '2',
type: 'session',
2021-03-04 15:47:38 -05:00
attributes: { target_id: '123abc' },
},
2021-02-05 16:57:06 -05:00
});
store.push({
data: {
id: '3',
type: 'session',
2021-03-04 15:47:38 -05:00
attributes: { target_id: '456xyz' },
},
2021-02-05 16:57:06 -05:00
});
2021-03-04 15:47:38 -05:00
assert.equal(
store.peekAll('session').length,
3,
'There are 3 sessions loaded in the store'
);
2021-02-05 16:57:06 -05:00
assert.equal(
target.sessions.length,
2,
'Target has two associated sessions loaded in the store'
);
});
2020-08-21 11:48:24 -04:00
});