github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/acceptance/job-allocations-test.js (about)

     1  import { currentURL } from '@ember/test-helpers';
     2  import { module, test } from 'qunit';
     3  import { setupApplicationTest } from 'ember-qunit';
     4  import { setupMirage } from 'ember-cli-mirage/test-support';
     5  import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
     6  import Allocations from 'nomad-ui/tests/pages/jobs/job/allocations';
     7  
     8  let job;
     9  let allocations;
    10  
    11  const makeSearchAllocations = server => {
    12    Array(10)
    13      .fill(null)
    14      .map((_, index) => {
    15        server.create('allocation', {
    16          id: index < 5 ? `ffffff-dddddd-${index}` : `111111-222222-${index}`,
    17          shallow: true,
    18        });
    19      });
    20  };
    21  
    22  module('Acceptance | job allocations', function(hooks) {
    23    setupApplicationTest(hooks);
    24    setupMirage(hooks);
    25  
    26    hooks.beforeEach(function() {
    27      server.create('node');
    28  
    29      job = server.create('job', { noFailedPlacements: true, createAllocations: false });
    30    });
    31  
    32    test('it passes an accessibility audit', async function(assert) {
    33      server.createList('allocation', Allocations.pageSize - 1, { shallow: true });
    34      allocations = server.schema.allocations.where({ jobId: job.id }).models;
    35  
    36      await Allocations.visit({ id: job.id });
    37      await a11yAudit(assert);
    38    });
    39  
    40    test('lists all allocations for the job', async function(assert) {
    41      server.createList('allocation', Allocations.pageSize - 1, { shallow: true });
    42      allocations = server.schema.allocations.where({ jobId: job.id }).models;
    43  
    44      await Allocations.visit({ id: job.id });
    45  
    46      assert.equal(
    47        Allocations.allocations.length,
    48        Allocations.pageSize - 1,
    49        'Allocations are shown in a table'
    50      );
    51  
    52      const sortedAllocations = allocations.sortBy('modifyIndex').reverse();
    53  
    54      Allocations.allocations.forEach((allocation, index) => {
    55        const shortId = sortedAllocations[index].id.split('-')[0];
    56        assert.equal(allocation.shortId, shortId, `Allocation ${index} is ${shortId}`);
    57      });
    58  
    59      assert.equal(document.title, `Job ${job.name} allocations - Nomad`);
    60    });
    61  
    62    test('allocations table is sortable', async function(assert) {
    63      server.createList('allocation', Allocations.pageSize - 1);
    64      allocations = server.schema.allocations.where({ jobId: job.id }).models;
    65  
    66      await Allocations.visit({ id: job.id });
    67      await Allocations.sortBy('taskGroupName');
    68  
    69      assert.equal(
    70        currentURL(),
    71        `/jobs/${job.id}/allocations?sort=taskGroupName`,
    72        'the URL persists the sort parameter'
    73      );
    74      const sortedAllocations = allocations.sortBy('taskGroup').reverse();
    75      Allocations.allocations.forEach((allocation, index) => {
    76        const shortId = sortedAllocations[index].id.split('-')[0];
    77        assert.equal(
    78          allocation.shortId,
    79          shortId,
    80          `Allocation ${index} is ${shortId} with task group ${sortedAllocations[index].taskGroup}`
    81        );
    82      });
    83    });
    84  
    85    test('allocations table is searchable', async function(assert) {
    86      makeSearchAllocations(server);
    87  
    88      allocations = server.schema.allocations.where({ jobId: job.id }).models;
    89  
    90      await Allocations.visit({ id: job.id });
    91      await Allocations.search('ffffff');
    92  
    93      assert.equal(Allocations.allocations.length, 5, 'List is filtered by search term');
    94    });
    95  
    96    test('when a search yields no results, the search box remains', async function(assert) {
    97      makeSearchAllocations(server);
    98  
    99      allocations = server.schema.allocations.where({ jobId: job.id }).models;
   100  
   101      await Allocations.visit({ id: job.id });
   102      await Allocations.search('^nothing will ever match this long regex$');
   103  
   104      assert.equal(
   105        Allocations.emptyState.headline,
   106        'No Matches',
   107        'List is empty and the empty state is about search'
   108      );
   109  
   110      assert.ok(Allocations.hasSearchBox, 'Search box is still shown');
   111    });
   112  
   113    test('when the job for the allocations is not found, an error message is shown, but the URL persists', async function(assert) {
   114      await Allocations.visit({ id: 'not-a-real-job' });
   115  
   116      assert.equal(
   117        server.pretender.handledRequests
   118          .filter(request => !request.url.includes('policy'))
   119          .findBy('status', 404).url,
   120        '/v1/job/not-a-real-job',
   121        'A request to the nonexistent job is made'
   122      );
   123      assert.equal(currentURL(), '/jobs/not-a-real-job/allocations', 'The URL persists');
   124      assert.ok(Allocations.error.isPresent, 'Error message is shown');
   125      assert.equal(Allocations.error.title, 'Not Found', 'Error message is for 404');
   126    });
   127  });