github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/acceptance/regions-test.js (about) 1 import { currentURL } from '@ember/test-helpers'; 2 import { module, test } from 'qunit'; 3 import { setupApplicationTest } from 'ember-qunit'; 4 import { selectChoose } from 'ember-power-select/test-support'; 5 import { setupMirage } from 'ember-cli-mirage/test-support'; 6 import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit'; 7 import JobsList from 'nomad-ui/tests/pages/jobs/list'; 8 import ClientsList from 'nomad-ui/tests/pages/clients/list'; 9 import Layout from 'nomad-ui/tests/pages/layout'; 10 import Allocation from 'nomad-ui/tests/pages/allocations/detail'; 11 12 module('Acceptance | regions (only one)', function(hooks) { 13 setupApplicationTest(hooks); 14 setupMirage(hooks); 15 16 hooks.beforeEach(function() { 17 server.create('agent'); 18 server.create('node'); 19 server.createList('job', 2, { createAllocations: false, noDeployments: true }); 20 }); 21 22 test('it passes an accessibility audit', async function(assert) { 23 await JobsList.visit(); 24 await a11yAudit(assert); 25 }); 26 27 test('when there is only one region, the region switcher is not shown in the nav bar and the region is not in the page title', async function(assert) { 28 server.create('region', { id: 'global' }); 29 30 await JobsList.visit(); 31 32 assert.notOk(Layout.navbar.regionSwitcher.isPresent, 'No region switcher'); 33 assert.equal(document.title, 'Jobs - Nomad'); 34 }); 35 36 test('when the only region is not named "global", the region switcher still is not shown', async function(assert) { 37 server.create('region', { id: 'some-region' }); 38 39 await JobsList.visit(); 40 41 assert.notOk(Layout.navbar.regionSwitcher.isPresent, 'No region switcher'); 42 }); 43 44 test('pages do not include the region query param', async function(assert) { 45 server.create('region', { id: 'global' }); 46 47 await JobsList.visit(); 48 assert.equal(currentURL(), '/jobs', 'No region query param'); 49 50 const jobId = JobsList.jobs.objectAt(0).id; 51 await JobsList.jobs.objectAt(0).clickRow(); 52 assert.equal(currentURL(), `/jobs/${jobId}`, 'No region query param'); 53 54 await ClientsList.visit(); 55 assert.equal(currentURL(), '/clients', 'No region query param'); 56 }); 57 58 test('api requests do not include the region query param', async function(assert) { 59 server.create('region', { id: 'global' }); 60 61 await JobsList.visit(); 62 await JobsList.jobs.objectAt(0).clickRow(); 63 await Layout.gutter.visitClients(); 64 await Layout.gutter.visitServers(); 65 server.pretender.handledRequests.forEach(req => { 66 assert.notOk(req.url.includes('region='), req.url); 67 }); 68 }); 69 }); 70 71 module('Acceptance | regions (many)', function(hooks) { 72 setupApplicationTest(hooks); 73 setupMirage(hooks); 74 75 hooks.beforeEach(function() { 76 server.create('agent'); 77 server.create('node'); 78 server.createList('job', 2, { createAllocations: false, noDeployments: true }); 79 server.create('allocation'); 80 server.create('region', { id: 'global' }); 81 server.create('region', { id: 'region-2' }); 82 }); 83 84 test('the region switcher is rendered in the nav bar and the region is in the page title', async function(assert) { 85 await JobsList.visit(); 86 87 assert.ok(Layout.navbar.regionSwitcher.isPresent, 'Region switcher is shown'); 88 assert.equal(document.title, 'Jobs - global - Nomad'); 89 }); 90 91 test('when on the default region, pages do not include the region query param', async function(assert) { 92 await JobsList.visit(); 93 94 assert.equal(currentURL(), '/jobs', 'No region query param'); 95 assert.equal(window.localStorage.nomadActiveRegion, 'global', 'Region in localStorage'); 96 }); 97 98 test('switching regions sets localStorage and the region query param', async function(assert) { 99 const newRegion = server.db.regions[1].id; 100 101 await JobsList.visit(); 102 103 await selectChoose('[data-test-region-switcher]', newRegion); 104 105 assert.ok( 106 currentURL().includes(`region=${newRegion}`), 107 'New region is the region query param value' 108 ); 109 assert.equal(window.localStorage.nomadActiveRegion, newRegion, 'New region in localStorage'); 110 }); 111 112 test('switching regions to the default region, unsets the region query param', async function(assert) { 113 const startingRegion = server.db.regions[1].id; 114 const defaultRegion = server.db.regions[0].id; 115 116 await JobsList.visit({ region: startingRegion }); 117 118 await selectChoose('[data-test-region-switcher]', defaultRegion); 119 120 assert.notOk(currentURL().includes('region='), 'No region query param for the default region'); 121 assert.equal( 122 window.localStorage.nomadActiveRegion, 123 defaultRegion, 124 'New region in localStorage' 125 ); 126 }); 127 128 test('switching regions on deep pages redirects to the application root', async function(assert) { 129 const newRegion = server.db.regions[1].id; 130 131 await Allocation.visit({ id: server.db.allocations[0].id }); 132 133 await selectChoose('[data-test-region-switcher]', newRegion); 134 135 assert.ok(currentURL().includes('/jobs?'), 'Back at the jobs page'); 136 }); 137 138 test('navigating directly to a page with the region query param sets the application to that region', async function(assert) { 139 const allocation = server.db.allocations[0]; 140 const region = server.db.regions[1].id; 141 await Allocation.visit({ id: allocation.id, region }); 142 143 assert.equal( 144 currentURL(), 145 `/allocations/${allocation.id}?region=${region}`, 146 'Region param is persisted when navigating straight to a detail page' 147 ); 148 assert.equal( 149 window.localStorage.nomadActiveRegion, 150 region, 151 'Region is also set in localStorage from a detail page' 152 ); 153 }); 154 155 test('when the region is not the default region, all api requests other than the agent/self request include the region query param', async function(assert) { 156 window.localStorage.removeItem('nomadTokenSecret'); 157 const region = server.db.regions[1].id; 158 159 await JobsList.visit({ region }); 160 161 await JobsList.jobs.objectAt(0).clickRow(); 162 await Layout.gutter.visitClients(); 163 await Layout.gutter.visitServers(); 164 const [ 165 , 166 , 167 // License request 168 // Token/policies request 169 regionsRequest, 170 defaultRegionRequest, 171 ...appRequests 172 ] = server.pretender.handledRequests; 173 174 assert.notOk( 175 regionsRequest.url.includes('region='), 176 'The regions request is made without a region qp' 177 ); 178 assert.notOk( 179 defaultRegionRequest.url.includes('region='), 180 'The default region request is made without a region qp' 181 ); 182 183 appRequests.forEach(req => { 184 if (req.url === '/v1/agent/self') { 185 assert.notOk(req.url.includes('region='), `(no region) ${req.url}`); 186 } else { 187 assert.ok(req.url.includes(`region=${region}`), req.url); 188 } 189 }); 190 }); 191 });