github.com/hernad/nomad@v1.6.112/ui/tests/acceptance/regions-test.js (about)

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