github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/tests/acceptance/job-definition-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 setupCodeMirror from 'nomad-ui/tests/helpers/codemirror';
     6  import Definition from 'nomad-ui/tests/pages/jobs/job/definition';
     7  
     8  let job;
     9  
    10  module('Acceptance | job definition', function(hooks) {
    11    setupApplicationTest(hooks);
    12    setupMirage(hooks);
    13    setupCodeMirror(hooks);
    14  
    15    hooks.beforeEach(async function() {
    16      server.create('node');
    17      server.create('job');
    18      job = server.db.jobs[0];
    19      await Definition.visit({ id: job.id });
    20    });
    21  
    22    test('visiting /jobs/:job_id/definition', async function(assert) {
    23      assert.equal(currentURL(), `/jobs/${job.id}/definition`);
    24      assert.equal(document.title, `Job ${job.name} definition - Nomad`);
    25    });
    26  
    27    test('the job definition page contains a json viewer component', async function(assert) {
    28      assert.ok(Definition.jsonViewer, 'JSON viewer found');
    29    });
    30  
    31    test('the job definition page requests the job to display in an unmutated form', async function(assert) {
    32      const jobURL = `/v1/job/${job.id}`;
    33      const jobRequests = server.pretender.handledRequests
    34        .map(req => req.url.split('?')[0])
    35        .filter(url => url === jobURL);
    36      assert.ok(jobRequests.length === 2, 'Two requests for the job were made');
    37    });
    38  
    39    test('the job definition can be edited', async function(assert) {
    40      assert.notOk(Definition.editor.isPresent, 'Editor is not shown on load');
    41  
    42      await Definition.edit();
    43  
    44      assert.ok(Definition.editor.isPresent, 'Editor is shown after clicking edit');
    45      assert.notOk(Definition.jsonViewer, 'Editor replaces the JSON viewer');
    46    });
    47  
    48    test('when in editing mode, the action can be canceled, showing the read-only definition again', async function(assert) {
    49      await Definition.edit();
    50  
    51      await Definition.editor.cancelEditing();
    52      assert.ok(Definition.jsonViewer, 'The JSON Viewer is back');
    53      assert.notOk(Definition.editor.isPresent, 'The editor is gone');
    54    });
    55  
    56    test('when in editing mode, the editor is prepopulated with the job definition', async function(assert) {
    57      const requests = server.pretender.handledRequests;
    58      const jobDefinition = requests.findBy('url', `/v1/job/${job.id}`).responseText;
    59      const formattedJobDefinition = JSON.stringify(JSON.parse(jobDefinition), null, 2);
    60  
    61      await Definition.edit();
    62  
    63      assert.equal(
    64        Definition.editor.editor.contents,
    65        formattedJobDefinition,
    66        'The editor already has the job definition in it'
    67      );
    68    });
    69  
    70    test('when changes are submitted, the site redirects to the job overview page', async function(assert) {
    71      await Definition.edit();
    72  
    73      await Definition.editor.plan();
    74      await Definition.editor.run();
    75      assert.equal(currentURL(), `/jobs/${job.id}`, 'Now on the job overview page');
    76    });
    77  
    78    test('when the job for the definition is not found, an error message is shown, but the URL persists', async function(assert) {
    79      await Definition.visit({ id: 'not-a-real-job' });
    80  
    81      assert.equal(
    82        server.pretender.handledRequests
    83          .filter(request => !request.url.includes('policy'))
    84          .findBy('status', 404).url,
    85        '/v1/job/not-a-real-job',
    86        'A request to the nonexistent job is made'
    87      );
    88      assert.equal(currentURL(), '/jobs/not-a-real-job/definition', 'The URL persists');
    89      assert.ok(Definition.error.isPresent, 'Error message is shown');
    90      assert.equal(Definition.error.title, 'Not Found', 'Error message is for 404');
    91    });
    92  });