github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/acceptance/regions-test.js (about)

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