chore: 🤖 add sessions property to target
This commit is contained in:
4 files changed
+86
-2
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import GeneratedSessionModel from '../generated/models/session';
|
||||
import { computed } from '@ember/object';
|
||||
import { equal } from '@ember/object/computed';
|
||||
import { attr } from '@ember-data/model';
|
||||
|
||||
export default class SessionModel extends GeneratedSessionModel {
|
||||
@@ -9,12 +10,15 @@ export default class SessionModel extends GeneratedSessionModel {
|
||||
@attr('string', { readOnly: true }) proxy_address;
|
||||
@attr('number', { readOnly: true }) proxy_port;
|
||||
|
||||
@equal('status', 'active') isActive;
|
||||
@equal('status', 'pending') isPending;
|
||||
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
@computed('status')
|
||||
@computed('isActive', 'isPending')
|
||||
get isCancelable() {
|
||||
return this.status?.match(/(active)|(pending)/i);
|
||||
return this.isActive || this.isPending;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -36,6 +36,24 @@ export default class TargetModel extends GeneratedTargetModel {
|
||||
.filter((hostSetRef) => hostSetRef.model !== null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sessions associated with this target (but only already loaded sessions).
|
||||
* @type {SessionModel[]}
|
||||
*/
|
||||
get sessions() {
|
||||
return this.store.peekAll('session').filter(s => s.target_id === this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* True if any sessions associated with this target are active or pending.
|
||||
* @type {boolean}
|
||||
*/
|
||||
get isActive() {
|
||||
const pendingOrActiveSessions =
|
||||
this.sessions.filter(s => s.isActive || s.isPending);
|
||||
return Boolean(pendingOrActiveSessions.length);
|
||||
}
|
||||
|
||||
// =methods
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,6 +27,28 @@ module('Unit | Model | session', function(hooks) {
|
||||
assert.ok(model.isCancelable);
|
||||
});
|
||||
|
||||
test('it computes isActive', function (assert) {
|
||||
assert.expect(2);
|
||||
let store = this.owner.lookup('service:store');
|
||||
let model = store.createRecord('session', {
|
||||
status: 'pending'
|
||||
});
|
||||
assert.notOk(model.isActive);
|
||||
model.status = 'active';
|
||||
assert.ok(model.isActive);
|
||||
});
|
||||
|
||||
test('it computes isPending', function (assert) {
|
||||
assert.expect(2);
|
||||
let store = this.owner.lookup('service:store');
|
||||
let model = store.createRecord('session', {
|
||||
status: 'active'
|
||||
});
|
||||
assert.notOk(model.isPending);
|
||||
model.status = 'pending';
|
||||
assert.ok(model.isPending);
|
||||
});
|
||||
|
||||
test('it computes a proxy attribute', function (assert) {
|
||||
let store = this.owner.lookup('service:store');
|
||||
let model = store.createRecord('session');
|
||||
|
||||
@@ -179,4 +179,44 @@ module('Unit | Model | target', function (hooks) {
|
||||
const model = store.peekRecord('target', '123abc');
|
||||
await model.removeHostSet('3');
|
||||
});
|
||||
|
||||
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',
|
||||
type: 'target'
|
||||
}
|
||||
});
|
||||
const target = store.peekRecord('target', '123abc');
|
||||
assert.equal( target.sessions.length, 0);
|
||||
store.push({
|
||||
data: {
|
||||
id: '1',
|
||||
type: 'session',
|
||||
attributes: { target_id: '123abc' }
|
||||
}
|
||||
});
|
||||
store.push({
|
||||
data: {
|
||||
id: '2',
|
||||
type: 'session',
|
||||
attributes: { target_id: '123abc' }
|
||||
}
|
||||
});
|
||||
store.push({
|
||||
data: {
|
||||
id: '3',
|
||||
type: 'session',
|
||||
attributes: { target_id: '456xyz' }
|
||||
}
|
||||
});
|
||||
assert.equal(store.peekAll('session').length, 3, 'There are 3 sessions loaded in the store');
|
||||
assert.equal(
|
||||
target.sessions.length,
|
||||
2,
|
||||
'Target has two associated sessions loaded in the store'
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user