github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/ui/tests/acceptance/regions-test.js (about)

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