github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/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/setup-mirage';
     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    });
    25  
    26    test('the job definition page contains a json viewer component', async function(assert) {
    27      assert.ok(Definition.jsonViewer, 'JSON viewer found');
    28    });
    29  
    30    test('the job definition page requests the job to display in an unmutated form', async function(assert) {
    31      const jobURL = `/v1/job/${job.id}`;
    32      const jobRequests = server.pretender.handledRequests
    33        .map(req => req.url.split('?')[0])
    34        .filter(url => url === jobURL);
    35      assert.ok(jobRequests.length === 2, 'Two requests for the job were made');
    36    });
    37  
    38    test('the job definition can be edited', async function(assert) {
    39      assert.notOk(Definition.editor.isPresent, 'Editor is not shown on load');
    40  
    41      await Definition.edit();
    42  
    43      assert.ok(Definition.editor.isPresent, 'Editor is shown after clicking edit');
    44      assert.notOk(Definition.jsonViewer, 'Editor replaces the JSON viewer');
    45    });
    46  
    47    test('when in editing mode, the action can be canceled, showing the read-only definition again', async function(assert) {
    48      await Definition.edit();
    49  
    50      await Definition.editor.cancelEditing();
    51      assert.ok(Definition.jsonViewer, 'The JSON Viewer is back');
    52      assert.notOk(Definition.editor.isPresent, 'The editor is gone');
    53    });
    54  
    55    test('when in editing mode, the editor is prepopulated with the job definition', async function(assert) {
    56      const requests = server.pretender.handledRequests;
    57      const jobDefinition = requests.findBy('url', `/v1/job/${job.id}`).responseText;
    58      const formattedJobDefinition = JSON.stringify(JSON.parse(jobDefinition), null, 2);
    59  
    60      await Definition.edit();
    61  
    62      assert.equal(
    63        Definition.editor.editor.contents,
    64        formattedJobDefinition,
    65        'The editor already has the job definition in it'
    66      );
    67    });
    68  
    69    test('when changes are submitted, the site redirects to the job overview page', async function(assert) {
    70      await Definition.edit();
    71  
    72      await Definition.editor.plan();
    73      await Definition.editor.run();
    74      assert.equal(currentURL(), `/jobs/${job.id}`, 'Now on the job overview page');
    75    });
    76  
    77    test('when the job for the definition is not found, an error message is shown, but the URL persists', async function(assert) {
    78      await Definition.visit({ id: 'not-a-real-job' });
    79  
    80      assert.equal(
    81        server.pretender.handledRequests.findBy('status', 404).url,
    82        '/v1/job/not-a-real-job',
    83        'A request to the nonexistent job is made'
    84      );
    85      assert.equal(currentURL(), '/jobs/not-a-real-job/definition', 'The URL persists');
    86      assert.ok(Definition.error.isPresent, 'Error message is shown');
    87      assert.equal(Definition.error.title, 'Not Found', 'Error message is for 404');
    88    });
    89  });