github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/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.findBy('name', group.name).update({ events });
    23  
    24        const jobModel = await this.store.find('job', JSON.stringify([job.id, 'default']));
    25        await jobModel.get('scaleState');
    26        return jobModel.taskGroups.findBy('name', group.name);
    27      };
    28    });
    29  
    30    hooks.afterEach(function() {
    31      this.server.shutdown();
    32    });
    33  
    34    const commonTemplate = hbs`<ScaleEventsAccordion @events={{this.events}} />`;
    35  
    36    test('it shows an accordion with an entry for each event', async function(assert) {
    37      const eventCount = 5;
    38      const taskGroup = await this.taskGroupWithEvents(server.createList('scale-event', eventCount));
    39      this.set('events', taskGroup.scaleState.events);
    40  
    41      await render(commonTemplate);
    42  
    43      assert.equal(findAll('[data-test-scale-events] [data-test-accordion-head]').length, eventCount);
    44      await componentA11yAudit(this.element, assert);
    45    });
    46  
    47    test('when an event is an error, an error icon is shown', async function(assert) {
    48      const taskGroup = await this.taskGroupWithEvents(
    49        server.createList('scale-event', 1, { error: true })
    50      );
    51      this.set('events', taskGroup.scaleState.events);
    52  
    53      await render(commonTemplate);
    54  
    55      assert.ok(find('[data-test-error]'));
    56      await componentA11yAudit(this.element, assert);
    57    });
    58  
    59    test('when an event has a count higher than previous count, a danger up arrow is shown', async function(assert) {
    60      const count = 5;
    61      const taskGroup = await this.taskGroupWithEvents(
    62        server.createList('scale-event', 1, { count, previousCount: count - 1, error: false })
    63      );
    64      this.set('events', taskGroup.scaleState.events);
    65  
    66      await render(commonTemplate);
    67  
    68      assert.notOk(find('[data-test-error]'));
    69      assert.equal(find('[data-test-count]').textContent, count);
    70      assert.ok(
    71        find('[data-test-count-icon]')
    72          .querySelector('.icon')
    73          .classList.contains('is-danger')
    74      );
    75      await componentA11yAudit(this.element, assert);
    76    });
    77  
    78    test('when an event has a count lower than previous count, a primary down arrow is shown', async function(assert) {
    79      const count = 5;
    80      const taskGroup = await this.taskGroupWithEvents(
    81        server.createList('scale-event', 1, { count, previousCount: count + 1, error: false })
    82      );
    83      this.set('events', taskGroup.scaleState.events);
    84  
    85      await render(commonTemplate);
    86  
    87      assert.notOk(find('[data-test-error]'));
    88      assert.equal(find('[data-test-count]').textContent, count);
    89      assert.ok(
    90        find('[data-test-count-icon]')
    91          .querySelector('.icon')
    92          .classList.contains('is-primary')
    93      );
    94    });
    95  
    96    test('when an event has no count, the count is omitted', async function(assert) {
    97      const taskGroup = await this.taskGroupWithEvents(
    98        server.createList('scale-event', 1, { count: null })
    99      );
   100      this.set('events', taskGroup.scaleState.events);
   101  
   102      await render(commonTemplate);
   103  
   104      assert.notOk(find('[data-test-count]'));
   105      assert.notOk(find('[data-test-count-icon]'));
   106    });
   107  
   108    test('when an event has no meta properties, the accordion entry is not expandable', async function(assert) {
   109      const taskGroup = await this.taskGroupWithEvents(
   110        server.createList('scale-event', 1, { meta: {} })
   111      );
   112      this.set('events', taskGroup.scaleState.events);
   113  
   114      await render(commonTemplate);
   115  
   116      assert.ok(find('[data-test-accordion-toggle]').classList.contains('is-invisible'));
   117      await componentA11yAudit(this.element, assert);
   118    });
   119  
   120    test('when an event has meta properties, the accordion entry is expanding, presenting the meta properties in a json viewer', async function(assert) {
   121      const meta = {
   122        prop: 'one',
   123        prop2: 'two',
   124        deep: {
   125          prop: 'here',
   126          'dot.separate.prop': 12,
   127        },
   128      };
   129      const taskGroup = await this.taskGroupWithEvents(server.createList('scale-event', 1, { meta }));
   130      this.set('events', taskGroup.scaleState.events);
   131  
   132      await render(commonTemplate);
   133      assert.notOk(find('[data-test-accordion-body]'));
   134  
   135      await click('[data-test-accordion-toggle]');
   136      assert.ok(find('[data-test-accordion-body]'));
   137  
   138      assert.equal(
   139        getCodeMirrorInstance('[data-test-json-viewer]').getValue(),
   140        JSON.stringify(meta, null, 2)
   141      );
   142      await componentA11yAudit(this.element, assert);
   143    });
   144  });