github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/acceptance/plugin-allocations-test.js (about)

     1  import { module, test } from 'qunit';
     2  import { currentURL } from '@ember/test-helpers';
     3  import { setupApplicationTest } from 'ember-qunit';
     4  import { setupMirage } from 'ember-cli-mirage/test-support';
     5  import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
     6  import pageSizeSelect from './behaviors/page-size-select';
     7  import PluginAllocations from 'nomad-ui/tests/pages/storage/plugins/plugin/allocations';
     8  
     9  module('Acceptance | plugin allocations', function(hooks) {
    10    setupApplicationTest(hooks);
    11    setupMirage(hooks);
    12  
    13    let plugin;
    14  
    15    hooks.beforeEach(function() {
    16      server.create('node');
    17      window.localStorage.clear();
    18    });
    19  
    20    test('it passes an accessibility audit', async function(assert) {
    21      plugin = server.create('csi-plugin', {
    22        shallow: true,
    23        controllerRequired: true,
    24        controllersExpected: 3,
    25        nodesExpected: 3,
    26      });
    27  
    28      await PluginAllocations.visit({ id: plugin.id });
    29      await a11yAudit(assert);
    30    });
    31  
    32    test('/csi/plugins/:id/allocations shows all allocations in a single table', async function(assert) {
    33      plugin = server.create('csi-plugin', {
    34        shallow: true,
    35        controllerRequired: true,
    36        controllersExpected: 3,
    37        nodesExpected: 3,
    38      });
    39  
    40      await PluginAllocations.visit({ id: plugin.id });
    41      assert.equal(PluginAllocations.allocations.length, 6);
    42    });
    43  
    44    pageSizeSelect({
    45      resourceName: 'allocation',
    46      pageObject: PluginAllocations,
    47      pageObjectList: PluginAllocations.allocations,
    48      async setup() {
    49        const total = PluginAllocations.pageSize;
    50        plugin = server.create('csi-plugin', {
    51          shallow: true,
    52          controllerRequired: true,
    53          controllersExpected: Math.floor(total / 2),
    54          nodesExpected: Math.ceil(total / 2),
    55        });
    56  
    57        await PluginAllocations.visit({ id: plugin.id });
    58      },
    59    });
    60  
    61    testFacet('Health', {
    62      facet: PluginAllocations.facets.health,
    63      paramName: 'healthy',
    64      async beforeEach() {
    65        plugin = server.create('csi-plugin', {
    66          shallow: true,
    67          controllerRequired: true,
    68          controllersExpected: 3,
    69          nodesExpected: 3,
    70        });
    71  
    72        await PluginAllocations.visit({ id: plugin.id });
    73      },
    74      filter: (allocation, selection) => selection.includes(allocation.healthy.toString()),
    75    });
    76  
    77    testFacet('Type', {
    78      facet: PluginAllocations.facets.type,
    79      paramName: 'type',
    80      async beforeEach() {
    81        plugin = server.create('csi-plugin', {
    82          shallow: true,
    83          controllerRequired: true,
    84          controllersExpected: 3,
    85          nodesExpected: 3,
    86        });
    87  
    88        await PluginAllocations.visit({ id: plugin.id });
    89      },
    90      filter: (allocation, selection) => {
    91        if (selection.length === 0 || selection.length === 2) return true;
    92        if (selection[0] === 'controller') return plugin.controllers.models.includes(allocation);
    93        return plugin.nodes.models.includes(allocation);
    94      },
    95    });
    96  
    97    function testFacet(label, { facet, paramName, beforeEach, filter }) {
    98      test(`the ${label} facet filters the allocations list by ${label}`, async function(assert) {
    99        let option;
   100  
   101        await beforeEach();
   102        await facet.toggle();
   103  
   104        option = facet.options.objectAt(0);
   105        await option.toggle();
   106  
   107        const selection = [option.key];
   108        const allAllocations = [...plugin.controllers.models, ...plugin.nodes.models];
   109        const expectedAllocations = allAllocations
   110          .filter(allocation => filter(allocation, selection))
   111          .sortBy('updateTime');
   112  
   113        PluginAllocations.allocations.forEach((allocation, index) => {
   114          assert.equal(allocation.id, expectedAllocations[index].allocID);
   115        });
   116      });
   117  
   118      test(`selecting multiple options in the ${label} facet results in a broader search`, async function(assert) {
   119        const selection = [];
   120  
   121        await beforeEach();
   122        await facet.toggle();
   123  
   124        const option1 = facet.options.objectAt(0);
   125        const option2 = facet.options.objectAt(1);
   126        await option1.toggle();
   127        selection.push(option1.key);
   128        await option2.toggle();
   129        selection.push(option2.key);
   130  
   131        const allAllocations = [...plugin.controllers.models, ...plugin.nodes.models];
   132        const expectedAllocations = allAllocations
   133          .filter(allocation => filter(allocation, selection))
   134          .sortBy('updateTime');
   135  
   136        PluginAllocations.allocations.forEach((allocation, index) => {
   137          assert.equal(allocation.id, expectedAllocations[index].allocID);
   138        });
   139      });
   140  
   141      test(`selecting options in the ${label} facet updates the ${paramName} query param`, async function(assert) {
   142        const selection = [];
   143  
   144        await beforeEach();
   145        await facet.toggle();
   146  
   147        const option1 = facet.options.objectAt(0);
   148        const option2 = facet.options.objectAt(1);
   149        await option1.toggle();
   150        selection.push(option1.key);
   151        await option2.toggle();
   152        selection.push(option2.key);
   153  
   154        const queryString = `${paramName}=${window.encodeURIComponent(JSON.stringify(selection))}`;
   155  
   156        assert.equal(currentURL(), `/csi/plugins/${plugin.id}/allocations?${queryString}`);
   157      });
   158    }
   159  });