github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/integration/components/task-group-row-test.js (about)

     1  import { module, test } from 'qunit';
     2  import { setupRenderingTest } from 'ember-qunit';
     3  import { click, find, render, settled, waitUntil } from '@ember/test-helpers';
     4  import hbs from 'htmlbars-inline-precompile';
     5  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
     6  import { initialize as fragmentSerializerInitializer } from 'nomad-ui/initializers/fragment-serializer';
     7  import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
     8  
     9  const jobName = 'test-job';
    10  const jobId = JSON.stringify([jobName, 'default']);
    11  
    12  const countChange = () => {
    13    const initial = find('[data-test-task-group-count]').textContent;
    14    return () => find('[data-test-task-group-count]').textContent !== initial;
    15  };
    16  
    17  let managementToken;
    18  let clientToken;
    19  
    20  const makeJob = (server, props = {}) => {
    21    // These tests require a job with particular task groups. This requires
    22    // mild Mirage surgery.
    23    const job = server.create('job', {
    24      id: jobName,
    25      groupCount: 0,
    26      createAllocations: false,
    27      shallow: true,
    28      ...props,
    29    });
    30    const noScalingGroup = server.create('task-group', {
    31      job,
    32      name: 'no-scaling',
    33      shallow: true,
    34      withScaling: false,
    35    });
    36    const scalingGroup = server.create('task-group', {
    37      job,
    38      count: 2,
    39      name: 'scaling',
    40      shallow: true,
    41      withScaling: true,
    42    });
    43    job.update({
    44      taskGroupIds: [noScalingGroup.id, scalingGroup.id],
    45    });
    46  };
    47  
    48  module('Integration | Component | task group row', function(hooks) {
    49    setupRenderingTest(hooks);
    50  
    51    hooks.beforeEach(async function() {
    52      fragmentSerializerInitializer(this.owner);
    53      this.store = this.owner.lookup('service:store');
    54      this.token = this.owner.lookup('service:token');
    55      this.server = startMirage();
    56      this.server.create('node');
    57  
    58      managementToken = this.server.create('token');
    59      clientToken = this.server.create('token');
    60      window.localStorage.nomadTokenSecret = managementToken.secretId;
    61    });
    62  
    63    hooks.afterEach(function() {
    64      this.server.shutdown();
    65      window.localStorage.clear();
    66    });
    67  
    68    const commonTemplate = hbs`
    69      <TaskGroupRow @taskGroup={{group}} />
    70    `;
    71  
    72    test('Task group row conditionally shows scaling buttons based on the presence of the scaling attr on the task group', async function(assert) {
    73      makeJob(this.server, { noActiveDeployment: true });
    74      this.token.fetchSelfTokenAndPolicies.perform();
    75      await settled();
    76  
    77      const job = await this.store.find('job', jobId);
    78      this.set('group', job.taskGroups.findBy('name', 'no-scaling'));
    79  
    80      await render(commonTemplate);
    81      assert.notOk(find('[data-test-scale]'));
    82  
    83      this.set('group', job.taskGroups.findBy('name', 'scaling'));
    84  
    85      await settled();
    86      assert.ok(find('[data-test-scale]'));
    87  
    88      await componentA11yAudit(this.element, assert);
    89    });
    90  
    91    test('Clicking scaling buttons immediately updates the rendered count but debounces the scaling API request', async function(assert) {
    92      makeJob(this.server, { noActiveDeployment: true });
    93      this.token.fetchSelfTokenAndPolicies.perform();
    94      await settled();
    95  
    96      const job = await this.store.find('job', jobId);
    97      this.set('group', job.taskGroups.findBy('name', 'scaling'));
    98  
    99      await render(commonTemplate);
   100      assert.equal(find('[data-test-task-group-count]').textContent, 2);
   101  
   102      click('[data-test-scale="increment"]');
   103      await waitUntil(countChange());
   104      assert.equal(find('[data-test-task-group-count]').textContent, 3);
   105  
   106      click('[data-test-scale="increment"]');
   107      await waitUntil(countChange());
   108      assert.equal(find('[data-test-task-group-count]').textContent, 4);
   109  
   110      assert.notOk(
   111        server.pretender.handledRequests.find(
   112          req => req.method === 'POST' && req.url.endsWith('/scale')
   113        )
   114      );
   115  
   116      await settled();
   117      const scaleRequests = server.pretender.handledRequests.filter(
   118        req => req.method === 'POST' && req.url.endsWith('/scale')
   119      );
   120      assert.equal(scaleRequests.length, 1);
   121      assert.equal(JSON.parse(scaleRequests[0].requestBody).Count, 4);
   122    });
   123  
   124    test('When the current count is equal to the max count, the increment count button is disabled', async function(assert) {
   125      makeJob(this.server, { noActiveDeployment: true });
   126      this.token.fetchSelfTokenAndPolicies.perform();
   127      await settled();
   128  
   129      const job = await this.store.find('job', jobId);
   130      const group = job.taskGroups.findBy('name', 'scaling');
   131      group.set('count', group.scaling.max);
   132      this.set('group', group);
   133  
   134      await render(commonTemplate);
   135      assert.ok(find('[data-test-scale="increment"]:disabled'));
   136  
   137      await componentA11yAudit(this.element, assert);
   138    });
   139  
   140    test('When the current count is equal to the min count, the decrement count button is disabled', async function(assert) {
   141      makeJob(this.server, { noActiveDeployment: true });
   142      this.token.fetchSelfTokenAndPolicies.perform();
   143      await settled();
   144  
   145      const job = await this.store.find('job', jobId);
   146      const group = job.taskGroups.findBy('name', 'scaling');
   147      group.set('count', group.scaling.min);
   148      this.set('group', group);
   149  
   150      await render(commonTemplate);
   151      assert.ok(find('[data-test-scale="decrement"]:disabled'));
   152  
   153      await componentA11yAudit(this.element, assert);
   154    });
   155  
   156    test('When there is an active deployment, both scale buttons are disabled', async function(assert) {
   157      makeJob(this.server, { activeDeployment: true });
   158      this.token.fetchSelfTokenAndPolicies.perform();
   159      await settled();
   160  
   161      const job = await this.store.find('job', jobId);
   162      this.set('group', job.taskGroups.findBy('name', 'scaling'));
   163  
   164      await render(commonTemplate);
   165      assert.ok(find('[data-test-scale="increment"]:disabled'));
   166      assert.ok(find('[data-test-scale="decrement"]:disabled'));
   167  
   168      await componentA11yAudit(this.element, assert);
   169    });
   170  
   171    test('When the current ACL token does not have the namespace:scale-job or namespace:submit-job policy rule', async function(assert) {
   172      makeJob(this.server, { noActiveDeployment: true });
   173      window.localStorage.nomadTokenSecret = clientToken.secretId;
   174      this.token.fetchSelfTokenAndPolicies.perform();
   175      await settled();
   176  
   177      const job = await this.store.find('job', jobId);
   178      this.set('group', job.taskGroups.findBy('name', 'scaling'));
   179  
   180      await render(commonTemplate);
   181      assert.ok(find('[data-test-scale="increment"]:disabled'));
   182      assert.ok(find('[data-test-scale="decrement"]:disabled'));
   183      assert.ok(
   184        find('[data-test-scale-controls]')
   185          .getAttribute('aria-label')
   186          .includes("You aren't allowed")
   187      );
   188    });
   189  });