github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/integration/components/primary-metric/primary-metric.js (about) 1 import EmberObject, { computed } from '@ember/object'; 2 import Service from '@ember/service'; 3 import { find, render, clearRender } from '@ember/test-helpers'; 4 import { test } from 'qunit'; 5 import { task } from 'ember-concurrency'; 6 import sinon from 'sinon'; 7 8 export function setupPrimaryMetricMocks(hooks, tasks = []) { 9 hooks.beforeEach(function () { 10 const getTrackerSpy = (this.getTrackerSpy = sinon.spy()); 11 const trackerPollSpy = (this.trackerPollSpy = sinon.spy()); 12 const trackerSignalPauseSpy = (this.trackerSignalPauseSpy = sinon.spy()); 13 14 const MockTracker = EmberObject.extend({ 15 poll: task(function* () { 16 yield trackerPollSpy(); 17 }), 18 signalPause: task(function* () { 19 yield trackerSignalPauseSpy(); 20 }), 21 22 cpu: computed(function () { 23 return []; 24 }), 25 memory: computed(function () { 26 return []; 27 }), 28 tasks, 29 }); 30 31 const mockStatsTrackersRegistry = Service.extend({ 32 getTracker(...args) { 33 getTrackerSpy(...args); 34 return MockTracker.create(); 35 }, 36 }); 37 38 this.owner.register( 39 'service:stats-trackers-registry', 40 mockStatsTrackersRegistry 41 ); 42 this.statsTrackersRegistry = this.owner.lookup( 43 'service:stats-trackers-registry' 44 ); 45 }); 46 } 47 48 export function primaryMetric({ template, findResource, preload }) { 49 test('Contains a line chart, a percentage bar, a percentage figure, and an absolute usage figure', async function (assert) { 50 const metric = 'cpu'; 51 52 await preload(this.store); 53 54 const resource = findResource(this.store); 55 this.setProperties({ resource, metric }); 56 57 await render(template); 58 59 assert.ok(find('[data-test-line-chart]'), 'Line chart'); 60 assert.ok(find('[data-test-percentage-bar]'), 'Percentage bar'); 61 assert.ok(find('[data-test-percentage]'), 'Percentage figure'); 62 assert.ok(find('[data-test-absolute-value]'), 'Absolute usage figure'); 63 }); 64 65 test('The CPU metric maps to is-info', async function (assert) { 66 const metric = 'cpu'; 67 68 await preload(this.store); 69 70 const resource = findResource(this.store); 71 this.setProperties({ resource, metric }); 72 73 await render(template); 74 75 assert.ok( 76 find('[data-test-current-value]').classList.contains('is-info'), 77 'Info class for CPU metric' 78 ); 79 }); 80 81 test('The Memory metric maps to is-danger', async function (assert) { 82 const metric = 'memory'; 83 84 await preload(this.store); 85 86 const resource = findResource(this.store); 87 this.setProperties({ resource, metric }); 88 89 await render(template); 90 91 assert.ok( 92 find('[data-test-current-value]').classList.contains('is-danger'), 93 'Danger class for Memory metric' 94 ); 95 }); 96 97 test('Gets the tracker from the tracker registry', async function (assert) { 98 const metric = 'cpu'; 99 100 await preload(this.store); 101 102 const resource = findResource(this.store); 103 this.setProperties({ resource, metric }); 104 105 await render(template); 106 107 const spy = 108 this.getTrackerSpy.calledWith(resource) || 109 this.getTrackerSpy.calledWith(resource.allocation); 110 111 assert.ok( 112 spy, 113 'Uses the tracker registry to get the tracker for the provided resource' 114 ); 115 }); 116 117 test('Immediately polls the tracker', async function (assert) { 118 const metric = 'cpu'; 119 120 await preload(this.store); 121 122 const resource = findResource(this.store); 123 this.setProperties({ resource, metric }); 124 125 await render(template); 126 127 assert.ok( 128 this.trackerPollSpy.calledOnce, 129 'The tracker is polled immediately' 130 ); 131 }); 132 133 test('A pause signal is sent to the tracker when the component is destroyed', async function (assert) { 134 const metric = 'cpu'; 135 136 // Capture a reference to the spy before the component is destroyed 137 const trackerSignalPauseSpy = this.trackerSignalPauseSpy; 138 139 await preload(this.store); 140 141 const resource = findResource(this.store); 142 this.setProperties({ resource, metric }); 143 await render(template); 144 145 assert.notOk( 146 trackerSignalPauseSpy.called, 147 'No pause signal has been sent yet' 148 ); 149 await clearRender(); 150 151 assert.ok( 152 trackerSignalPauseSpy.calledOnce, 153 'A pause signal is sent to the tracker' 154 ); 155 }); 156 }