github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/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 assert.expect(1); 25 26 await a11yAudit(assert, 'scrollable-region-focusable'); 27 }); 28 29 test('visiting /jobs/:job_id/definition', async function (assert) { 30 assert.equal(currentURL(), `/jobs/${job.id}/definition`); 31 assert.equal(document.title, `Job ${job.name} definition - Nomad`); 32 }); 33 34 test('the job definition page contains a json viewer component', async function (assert) { 35 assert.ok(Definition.jsonViewer, 'JSON viewer found'); 36 }); 37 38 test('the job definition page requests the job to display in an unmutated form', async function (assert) { 39 const jobURL = `/v1/job/${job.id}`; 40 const jobRequests = server.pretender.handledRequests 41 .map((req) => req.url.split('?')[0]) 42 .filter((url) => url === jobURL); 43 assert.strictEqual( 44 jobRequests.length, 45 2, 46 'Two requests for the job were made' 47 ); 48 }); 49 50 test('the job definition can be edited', async function (assert) { 51 assert.notOk(Definition.editor.isPresent, 'Editor is not shown on load'); 52 53 await Definition.edit(); 54 55 assert.ok( 56 Definition.editor.isPresent, 57 'Editor is shown after clicking edit' 58 ); 59 assert.notOk(Definition.jsonViewer, 'Editor replaces the JSON viewer'); 60 }); 61 62 test('when in editing mode, the action can be canceled, showing the read-only definition again', async function (assert) { 63 await Definition.edit(); 64 65 await Definition.editor.cancelEditing(); 66 assert.ok(Definition.jsonViewer, 'The JSON Viewer is back'); 67 assert.notOk(Definition.editor.isPresent, 'The editor is gone'); 68 }); 69 70 test('when in editing mode, the editor is prepopulated with the job definition', async function (assert) { 71 const requests = server.pretender.handledRequests; 72 const jobDefinition = requests.findBy( 73 'url', 74 `/v1/job/${job.id}` 75 ).responseText; 76 const formattedJobDefinition = JSON.stringify( 77 JSON.parse(jobDefinition), 78 null, 79 2 80 ); 81 82 await Definition.edit(); 83 84 assert.equal( 85 Definition.editor.editor.contents, 86 formattedJobDefinition, 87 'The editor already has the job definition in it' 88 ); 89 }); 90 91 test('when changes are submitted, the site redirects to the job overview page', async function (assert) { 92 await Definition.edit(); 93 94 await Definition.editor.plan(); 95 await Definition.editor.run(); 96 assert.equal( 97 currentURL(), 98 `/jobs/${job.id}@default`, 99 'Now on the job overview page' 100 ); 101 }); 102 103 test('when the job for the definition is not found, an error message is shown, but the URL persists', async function (assert) { 104 await Definition.visit({ id: 'not-a-real-job' }); 105 106 assert.equal( 107 server.pretender.handledRequests 108 .filter((request) => !request.url.includes('policy')) 109 .findBy('status', 404).url, 110 '/v1/job/not-a-real-job', 111 'A request to the nonexistent job is made' 112 ); 113 assert.equal( 114 currentURL(), 115 '/jobs/not-a-real-job/definition', 116 'The URL persists' 117 ); 118 assert.ok(Definition.error.isPresent, 'Error message is shown'); 119 assert.equal( 120 Definition.error.title, 121 'Not Found', 122 'Error message is for 404' 123 ); 124 }); 125 });