Implement eligibility toggling in the data layer
This commit is contained in:
parent
94955c8b08
commit
aad8536949
|
@ -1,3 +1,22 @@
|
|||
import Watchable from './watchable';
|
||||
import addToPath from 'nomad-ui/utils/add-to-path';
|
||||
|
||||
export default Watchable.extend();
|
||||
export default Watchable.extend({
|
||||
setEligible(node) {
|
||||
return this.setEligibility(node, true);
|
||||
},
|
||||
|
||||
setIneligible(node) {
|
||||
return this.setEligibility(node, false);
|
||||
},
|
||||
|
||||
setEligibility(node, isEligible) {
|
||||
const url = addToPath(this.urlForFindRecord(node.id, 'node'), '/eligibility');
|
||||
return this.ajax(url, 'POST', {
|
||||
data: {
|
||||
NodeID: node.id,
|
||||
Eligibility: isEligible ? 'eligible' : 'ineligible',
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
|
|
@ -4,6 +4,7 @@ import Model from 'ember-data/model';
|
|||
import attr from 'ember-data/attr';
|
||||
import { hasMany } from 'ember-data/relationships';
|
||||
import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes';
|
||||
import RSVP from 'rsvp';
|
||||
import shortUUIDProperty from '../utils/properties/short-uuid';
|
||||
import ipParts from '../utils/ip-parts';
|
||||
|
||||
|
@ -70,4 +71,14 @@ export default Model.extend({
|
|||
return this.status;
|
||||
}
|
||||
}),
|
||||
|
||||
setEligible() {
|
||||
if (this.isEligible) return RSVP.resolve();
|
||||
return this.store.adapterFor('node').setEligible(this);
|
||||
},
|
||||
|
||||
setIneligible() {
|
||||
if (!this.isEligible) return RSVP.resolve();
|
||||
return this.store.adapterFor('node').setIneligible(this);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -85,6 +85,52 @@ module('Unit | Adapter | Node', function(hooks) {
|
|||
'The deleted allocation is removed from the store and the allocations associated with the other node are untouched'
|
||||
);
|
||||
});
|
||||
|
||||
test('setEligible makes the correct POST request to /:node_id/eligibility', async function(assert) {
|
||||
const { pretender } = this.server;
|
||||
const node = await run(() => this.store.findRecord('node', 'node-1'));
|
||||
|
||||
await this.subject().setEligible(node);
|
||||
|
||||
const request = pretender.handledRequests.lastObject;
|
||||
assert.equal(
|
||||
request.url,
|
||||
`/v1/node/${node.id}/eligibility`,
|
||||
'Request was made to /:node_id/eligibility'
|
||||
);
|
||||
assert.equal(request.method, 'POST', 'Request was made with the POST method');
|
||||
assert.deepEqual(
|
||||
JSON.parse(request.requestBody),
|
||||
{
|
||||
NodeID: node.id,
|
||||
Eligibility: 'eligible',
|
||||
},
|
||||
'POST request is made with the correct body arguments'
|
||||
);
|
||||
});
|
||||
|
||||
test('setIneligible makes the correct POST request to /:node_id/eligibility', async function(assert) {
|
||||
const { pretender } = this.server;
|
||||
const node = await run(() => this.store.findRecord('node', 'node-1'));
|
||||
|
||||
await this.subject().setIneligible(node);
|
||||
|
||||
const request = pretender.handledRequests.lastObject;
|
||||
assert.equal(
|
||||
request.url,
|
||||
`/v1/node/${node.id}/eligibility`,
|
||||
'Request was made to /:node_id/eligibility'
|
||||
);
|
||||
assert.equal(request.method, 'POST', 'Request was made with the POST method');
|
||||
assert.deepEqual(
|
||||
JSON.parse(request.requestBody),
|
||||
{
|
||||
NodeID: node.id,
|
||||
Eligibility: 'ineligible',
|
||||
},
|
||||
'POST request is made with the correct body arguments'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// Using fetchLink on a model's hasMany relationship exercises the adapter's
|
||||
|
|
Loading…
Reference in a new issue