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