2021-03-22 06:01:22 +00:00
|
|
|
import EmberObject, { computed } from '@ember/object';
|
|
|
|
import Service from '@ember/service';
|
|
|
|
import { find, render, clearRender } from '@ember/test-helpers';
|
|
|
|
import { test } from 'qunit';
|
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
|
|
|
|
export function setupPrimaryMetricMocks(hooks, tasks = []) {
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.beforeEach(function () {
|
2021-03-22 06:01:22 +00:00
|
|
|
const getTrackerSpy = (this.getTrackerSpy = sinon.spy());
|
|
|
|
const trackerPollSpy = (this.trackerPollSpy = sinon.spy());
|
|
|
|
const trackerSignalPauseSpy = (this.trackerSignalPauseSpy = sinon.spy());
|
|
|
|
|
|
|
|
const MockTracker = EmberObject.extend({
|
2021-12-28 14:45:20 +00:00
|
|
|
poll: task(function* () {
|
2021-03-22 06:01:22 +00:00
|
|
|
yield trackerPollSpy();
|
|
|
|
}),
|
2021-12-28 14:45:20 +00:00
|
|
|
signalPause: task(function* () {
|
2021-03-22 06:01:22 +00:00
|
|
|
yield trackerSignalPauseSpy();
|
|
|
|
}),
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
cpu: computed(function () {
|
2021-03-22 06:01:22 +00:00
|
|
|
return [];
|
|
|
|
}),
|
2021-12-28 14:45:20 +00:00
|
|
|
memory: computed(function () {
|
2021-03-22 06:01:22 +00:00
|
|
|
return [];
|
|
|
|
}),
|
|
|
|
tasks,
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockStatsTrackersRegistry = Service.extend({
|
|
|
|
getTracker(...args) {
|
|
|
|
getTrackerSpy(...args);
|
|
|
|
return MockTracker.create();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
this.owner.register(
|
|
|
|
'service:stats-trackers-registry',
|
|
|
|
mockStatsTrackersRegistry
|
|
|
|
);
|
|
|
|
this.statsTrackersRegistry = this.owner.lookup(
|
|
|
|
'service:stats-trackers-registry'
|
|
|
|
);
|
2021-03-22 06:01:22 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function primaryMetric({ template, findResource, preload }) {
|
2021-12-28 14:45:20 +00:00
|
|
|
test('Contains a line chart, a percentage bar, a percentage figure, and an absolute usage figure', async function (assert) {
|
2021-03-22 06:01:22 +00:00
|
|
|
const metric = 'cpu';
|
|
|
|
|
|
|
|
await preload(this.store);
|
|
|
|
|
|
|
|
const resource = findResource(this.store);
|
|
|
|
this.setProperties({ resource, metric });
|
|
|
|
|
|
|
|
await render(template);
|
|
|
|
|
|
|
|
assert.ok(find('[data-test-line-chart]'), 'Line chart');
|
|
|
|
assert.ok(find('[data-test-percentage-bar]'), 'Percentage bar');
|
|
|
|
assert.ok(find('[data-test-percentage]'), 'Percentage figure');
|
|
|
|
assert.ok(find('[data-test-absolute-value]'), 'Absolute usage figure');
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('The CPU metric maps to is-info', async function (assert) {
|
2021-03-22 06:01:22 +00:00
|
|
|
const metric = 'cpu';
|
|
|
|
|
|
|
|
await preload(this.store);
|
|
|
|
|
|
|
|
const resource = findResource(this.store);
|
|
|
|
this.setProperties({ resource, metric });
|
|
|
|
|
|
|
|
await render(template);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
find('[data-test-current-value]').classList.contains('is-info'),
|
|
|
|
'Info class for CPU metric'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('The Memory metric maps to is-danger', async function (assert) {
|
2021-03-22 06:01:22 +00:00
|
|
|
const metric = 'memory';
|
|
|
|
|
|
|
|
await preload(this.store);
|
|
|
|
|
|
|
|
const resource = findResource(this.store);
|
|
|
|
this.setProperties({ resource, metric });
|
|
|
|
|
|
|
|
await render(template);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
find('[data-test-current-value]').classList.contains('is-danger'),
|
|
|
|
'Danger class for Memory metric'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('Gets the tracker from the tracker registry', async function (assert) {
|
2021-03-22 06:01:22 +00:00
|
|
|
const metric = 'cpu';
|
|
|
|
|
|
|
|
await preload(this.store);
|
|
|
|
|
|
|
|
const resource = findResource(this.store);
|
|
|
|
this.setProperties({ resource, metric });
|
|
|
|
|
|
|
|
await render(template);
|
|
|
|
|
|
|
|
assert.ok(
|
2021-12-28 16:08:12 +00:00
|
|
|
this.getTrackerSpy.calledWith(resource) ||
|
|
|
|
this.getTrackerSpy.calledWith(resource.allocation),
|
2021-03-22 06:01:22 +00:00
|
|
|
'Uses the tracker registry to get the tracker for the provided resource'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('Immediately polls the tracker', async function (assert) {
|
2021-03-22 06:01:22 +00:00
|
|
|
const metric = 'cpu';
|
|
|
|
|
|
|
|
await preload(this.store);
|
|
|
|
|
|
|
|
const resource = findResource(this.store);
|
|
|
|
this.setProperties({ resource, metric });
|
|
|
|
|
|
|
|
await render(template);
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.ok(
|
|
|
|
this.trackerPollSpy.calledOnce,
|
|
|
|
'The tracker is polled immediately'
|
|
|
|
);
|
2021-03-22 06:01:22 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('A pause signal is sent to the tracker when the component is destroyed', async function (assert) {
|
2021-03-22 06:01:22 +00:00
|
|
|
const metric = 'cpu';
|
|
|
|
|
|
|
|
// Capture a reference to the spy before the component is destroyed
|
|
|
|
const trackerSignalPauseSpy = this.trackerSignalPauseSpy;
|
|
|
|
|
|
|
|
await preload(this.store);
|
|
|
|
|
|
|
|
const resource = findResource(this.store);
|
|
|
|
this.setProperties({ resource, metric });
|
|
|
|
await render(template);
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.notOk(
|
|
|
|
trackerSignalPauseSpy.called,
|
|
|
|
'No pause signal has been sent yet'
|
|
|
|
);
|
2021-03-22 06:01:22 +00:00
|
|
|
await clearRender();
|
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
assert.ok(
|
|
|
|
trackerSignalPauseSpy.calledOnce,
|
|
|
|
'A pause signal is sent to the tracker'
|
|
|
|
);
|
2021-03-22 06:01:22 +00:00
|
|
|
});
|
|
|
|
}
|