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