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

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