From d0d1ef67eb6c0dee9730305e2240779050542835 Mon Sep 17 00:00:00 2001 From: Susmitha Girumala Date: Wed, 21 Oct 2020 16:20:34 -0400 Subject: [PATCH] feat: Add 'addHost' method to a host set model --- addons/api/addon/models/host-set.js | 10 ++ addons/api/tests/unit/models/host-set-test.js | 141 +++++++++++++++++- 2 files changed, 146 insertions(+), 5 deletions(-) diff --git a/addons/api/addon/models/host-set.js b/addons/api/addon/models/host-set.js index 583971735..2750c0cb8 100644 --- a/addons/api/addon/models/host-set.js +++ b/addons/api/addon/models/host-set.js @@ -39,6 +39,16 @@ export default class HostSetModel extends GeneratedHostSetModel { }); } + /** + * Add a single host via the `add-hosts` method. + * @param {string} hostID + * @param {object} options + * @return {Promise} + */ + addHost(hostID, options) { + return this.addHosts([hostID], options); + } + /** * Delete hosts via the `remove-hosts` method. * See serializer and adapter for more information. diff --git a/addons/api/tests/unit/models/host-set-test.js b/addons/api/tests/unit/models/host-set-test.js index 540718c4e..c7228a81f 100644 --- a/addons/api/tests/unit/models/host-set-test.js +++ b/addons/api/tests/unit/models/host-set-test.js @@ -1,13 +1,144 @@ import { module, test } from 'qunit'; import { setupTest } from 'ember-qunit'; +import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; module('Unit | Model | host set', function (hooks) { setupTest(hooks); + setupMirage(hooks); - // Replace this with your real tests. - test('it exists', function (assert) { - let store = this.owner.lookup('service:store'); - let model = store.createRecord('host-set', {}); - assert.ok(model); + test('it has an `addHosts` method that targets a specific POST API endpoint and serialization', async function (assert) { + assert.expect(1); + this.server.post('/v1/host-sets/123abc:add-hosts', (schema, request) => { + const body = JSON.parse(request.requestBody); + assert.deepEqual(body, { + host_ids: ['123_abc', 'foobar'], + version: 1, + }); + return { id: '123abc' }; + }); + const store = this.owner.lookup('service:store'); + store.push({ + data: { + id: '123abc', + type: 'host-set', + attributes: { + name: 'Host Set', + description: 'Description', + host_ids: [ + {value: '1'}, + {value: '3'}, + ], + version: 1, + scope: { + scope_id: 'o_1', + type: 'scope', + }, + }, + }, + }); + const model = store.peekRecord('host-set', '123abc'); + await model.addHosts(['123_abc', 'foobar']); + }); + + test('it has an `addHost` method that adds a single host using `addHosts` method', async function (assert) { + assert.expect(1); + this.server.post('/v1/host-sets/123abc:add-hosts', (schema, request) => { + const body = JSON.parse(request.requestBody); + assert.deepEqual(body, { + host_ids: ['foobar'], + version: 1, + }); + return { id: '123abc' }; + }); + const store = this.owner.lookup('service:store'); + store.push({ + data: { + id: '123abc', + type: 'host-set', + attributes: { + name: 'Host Set', + description: 'Description', + host_ids: [ + {value: '1'}, + {value: '3'}, + ], + version: 1, + scope: { + scope_id: 'o_1', + type: 'scope', + }, + }, + }, + }); + const model = store.peekRecord('host-set', '123abc'); + await model.addHost('foobar'); + }); + + test('it has a `removeHostSets` method that targets a specific POST API endpoint and serialization', async function (assert) { + assert.expect(1); + this.server.post('/v1/host-sets/123abc:remove-hosts', (schema, request) => { + const body = JSON.parse(request.requestBody); + assert.deepEqual(body, { + host_ids: ['3'], + version: 1, + }); + return { id: '123abc' }; + }); + const store = this.owner.lookup('service:store'); + store.push({ + data: { + id: '123abc', + type: 'host-set', + attributes: { + name: 'Host Set', + description: 'Description', + host_ids: [ + {value: '1'}, + {value: '3'}, + ], + version: 1, + scope: { + scope_id: 'o_1', + type: 'scope', + }, + }, + }, + }); + const model = store.peekRecord('host-set', '123abc'); + await model.removeHosts(['3']); + }); + + test('it has a `removeHost` method that removes a single host using `removeHosts` method', async function (assert) { + assert.expect(1); + this.server.post('/v1/host-sets/123abc:remove-hosts', (schema, request) => { + const body = JSON.parse(request.requestBody); + assert.deepEqual(body, { + host_ids: ['3'], + version: 1, + }); + return { id: '123abc' }; + }); + const store = this.owner.lookup('service:store'); + store.push({ + data: { + id: '123abc', + type: 'host-set', + attributes: { + name: 'Host Set', + description: 'Description', + host_ids: [ + {value: '1'}, + {value: '3'}, + ], + version: 1, + scope: { + scope_id: 'o_1', + type: 'scope', + }, + }, + }, + }); + const model = store.peekRecord('host-set', '123abc'); + await model.removeHost('3'); }); });