github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/integration/components/scale-events-accordion-test.js (about) 1 import { module, test } from 'qunit'; 2 import { setupRenderingTest } from 'ember-qunit'; 3 import { click, find, findAll, render } from '@ember/test-helpers'; 4 import hbs from 'htmlbars-inline-precompile'; 5 import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage'; 6 import setupCodeMirror from 'nomad-ui/tests/helpers/codemirror'; 7 import { initialize as fragmentSerializerInitializer } from 'nomad-ui/initializers/fragment-serializer'; 8 import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit'; 9 10 module('Integration | Component | scale-events-accordion', function (hooks) { 11 setupRenderingTest(hooks); 12 setupCodeMirror(hooks); 13 14 hooks.beforeEach(function () { 15 fragmentSerializerInitializer(this.owner); 16 this.store = this.owner.lookup('service:store'); 17 this.server = startMirage(); 18 this.server.create('node'); 19 this.taskGroupWithEvents = async function (events) { 20 const job = this.server.create('job', { createAllocations: false }); 21 const group = job.taskGroups.models[0]; 22 job.jobScale.taskGroupScales.models 23 .findBy('name', group.name) 24 .update({ events }); 25 26 const jobModel = await this.store.find( 27 'job', 28 JSON.stringify([job.id, 'default']) 29 ); 30 await jobModel.get('scaleState'); 31 return jobModel.taskGroups.findBy('name', group.name); 32 }; 33 }); 34 35 hooks.afterEach(function () { 36 this.server.shutdown(); 37 }); 38 39 const commonTemplate = hbs`<ScaleEventsAccordion @events={{this.events}} />`; 40 41 test('it shows an accordion with an entry for each event', async function (assert) { 42 assert.expect(2); 43 44 const eventCount = 5; 45 const taskGroup = await this.taskGroupWithEvents( 46 server.createList('scale-event', eventCount) 47 ); 48 this.set('events', taskGroup.scaleState.events); 49 50 await render(commonTemplate); 51 52 assert.equal( 53 findAll('[data-test-scale-events] [data-test-accordion-head]').length, 54 eventCount 55 ); 56 await componentA11yAudit(this.element, assert); 57 }); 58 59 test('when an event is an error, an error icon is shown', async function (assert) { 60 assert.expect(2); 61 62 const taskGroup = await this.taskGroupWithEvents( 63 server.createList('scale-event', 1, { error: true }) 64 ); 65 this.set('events', taskGroup.scaleState.events); 66 67 await render(commonTemplate); 68 69 assert.ok(find('[data-test-error]')); 70 await componentA11yAudit(this.element, assert); 71 }); 72 73 test('when an event has a count higher than previous count, a danger up arrow is shown', async function (assert) { 74 assert.expect(4); 75 76 const count = 5; 77 const taskGroup = await this.taskGroupWithEvents( 78 server.createList('scale-event', 1, { 79 count, 80 previousCount: count - 1, 81 error: false, 82 }) 83 ); 84 this.set('events', taskGroup.scaleState.events); 85 86 await render(commonTemplate); 87 88 assert.notOk(find('[data-test-error]')); 89 assert.equal(find('[data-test-count]').textContent, count); 90 assert.ok( 91 find('[data-test-count-icon]') 92 .querySelector('.icon') 93 .classList.contains('is-danger') 94 ); 95 await componentA11yAudit(this.element, assert); 96 }); 97 98 test('when an event has a count lower than previous count, a primary down arrow is shown', async function (assert) { 99 const count = 5; 100 const taskGroup = await this.taskGroupWithEvents( 101 server.createList('scale-event', 1, { 102 count, 103 previousCount: count + 1, 104 error: false, 105 }) 106 ); 107 this.set('events', taskGroup.scaleState.events); 108 109 await render(commonTemplate); 110 111 assert.notOk(find('[data-test-error]')); 112 assert.equal(find('[data-test-count]').textContent, count); 113 assert.ok( 114 find('[data-test-count-icon]') 115 .querySelector('.icon') 116 .classList.contains('is-primary') 117 ); 118 }); 119 120 test('when an event has no count, the count is omitted', async function (assert) { 121 const taskGroup = await this.taskGroupWithEvents( 122 server.createList('scale-event', 1, { count: null }) 123 ); 124 this.set('events', taskGroup.scaleState.events); 125 126 await render(commonTemplate); 127 128 assert.notOk(find('[data-test-count]')); 129 assert.notOk(find('[data-test-count-icon]')); 130 }); 131 132 test('when an event has no meta properties, the accordion entry is not expandable', async function (assert) { 133 assert.expect(2); 134 135 const taskGroup = await this.taskGroupWithEvents( 136 server.createList('scale-event', 1, { meta: {} }) 137 ); 138 this.set('events', taskGroup.scaleState.events); 139 140 await render(commonTemplate); 141 142 assert.ok( 143 find('[data-test-accordion-toggle]').classList.contains('is-invisible') 144 ); 145 await componentA11yAudit(this.element, assert); 146 }); 147 148 test('when an event has meta properties, the accordion entry is expanding, presenting the meta properties in a json viewer', async function (assert) { 149 assert.expect(4); 150 151 const meta = { 152 prop: 'one', 153 prop2: 'two', 154 deep: { 155 prop: 'here', 156 'dot.separate.prop': 12, 157 }, 158 }; 159 const taskGroup = await this.taskGroupWithEvents( 160 server.createList('scale-event', 1, { meta }) 161 ); 162 this.set('events', taskGroup.scaleState.events); 163 164 await render(commonTemplate); 165 assert.notOk(find('[data-test-accordion-body]')); 166 167 await click('[data-test-accordion-toggle]'); 168 assert.ok(find('[data-test-accordion-body]')); 169 170 assert.equal( 171 getCodeMirrorInstance('[data-test-json-viewer]').getValue(), 172 JSON.stringify(meta, null, 2) 173 ); 174 await componentA11yAudit(this.element, assert); 175 }); 176 });