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