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