github.com/hernad/nomad@v1.6.112/ui/tests/integration/components/job-page/parts/children-test.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import { assign } from '@ember/polyfills';
     7  import hbs from 'htmlbars-inline-precompile';
     8  import { findAll, find, render } from '@ember/test-helpers';
     9  import { module, test } from 'qunit';
    10  import { setupRenderingTest } from 'ember-qunit';
    11  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
    12  import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
    13  
    14  module('Integration | Component | job-page/parts/children', function (hooks) {
    15    setupRenderingTest(hooks);
    16  
    17    hooks.beforeEach(function () {
    18      window.localStorage.clear();
    19      this.store = this.owner.lookup('service:store');
    20      this.server = startMirage();
    21      this.server.create('namespace');
    22      this.server.create('node-pool');
    23    });
    24  
    25    hooks.afterEach(function () {
    26      this.server.shutdown();
    27      window.localStorage.clear();
    28    });
    29  
    30    const props = (job, options = {}) =>
    31      assign(
    32        {
    33          job,
    34          sortProperty: 'name',
    35          sortDescending: true,
    36          currentPage: 1,
    37        },
    38        options
    39      );
    40  
    41    test('lists each child', async function (assert) {
    42      this.server.create('job', 'periodic', {
    43        id: 'parent',
    44        childrenCount: 3,
    45        createAllocations: false,
    46      });
    47  
    48      await this.store.findAll('job');
    49  
    50      const parent = this.store.peekAll('job').findBy('plainId', 'parent');
    51  
    52      this.setProperties(props(parent));
    53  
    54      await render(hbs`
    55        <JobPage::Parts::Children
    56          @job={{job}}
    57          @sortProperty={{sortProperty}}
    58          @sortDescending={{sortDescending}}
    59          @currentPage={{currentPage}}
    60          @gotoJob={{gotoJob}} />
    61      `);
    62  
    63      assert.equal(
    64        findAll('[data-test-job-name]').length,
    65        parent.get('children.length'),
    66        'A row for each child'
    67      );
    68    });
    69  
    70    test('eventually paginates', async function (assert) {
    71      assert.expect(5);
    72  
    73      const pageSize = 10;
    74      window.localStorage.nomadPageSize = pageSize;
    75  
    76      this.server.create('job', 'periodic', {
    77        id: 'parent',
    78        childrenCount: 11,
    79        createAllocations: false,
    80      });
    81  
    82      await this.store.findAll('job');
    83  
    84      const parent = this.store.peekAll('job').findBy('plainId', 'parent');
    85  
    86      this.setProperties(props(parent));
    87  
    88      await render(hbs`
    89        <JobPage::Parts::Children
    90          @job={{job}}
    91          @sortProperty={{sortProperty}}
    92          @sortDescending={{sortDescending}}
    93          @currentPage={{currentPage}}
    94        />
    95      `);
    96  
    97      const childrenCount = parent.get('children.length');
    98      assert.ok(
    99        childrenCount > pageSize,
   100        'Parent has more children than one page size'
   101      );
   102      assert.equal(
   103        findAll('[data-test-job-name]').length,
   104        pageSize,
   105        'Table length maxes out at 10'
   106      );
   107      assert.ok(find('.pagination-next'), 'Next button is rendered');
   108  
   109      assert
   110        .dom('.pagination-numbers')
   111        .includesText(
   112          '1 – 10 of 11',
   113          'Formats pagination to follow formula `startingIdx - endingIdx of totalTableCount'
   114        );
   115  
   116      await componentA11yAudit(this.element, assert);
   117    });
   118  
   119    test('is sorted based on the sortProperty and sortDescending properties', async function (assert) {
   120      assert.expect(6);
   121  
   122      this.server.create('job', 'periodic', {
   123        id: 'parent',
   124        childrenCount: 3,
   125        createAllocations: false,
   126      });
   127  
   128      await this.store.findAll('job');
   129  
   130      const parent = this.store.peekAll('job').findBy('plainId', 'parent');
   131  
   132      this.setProperties(props(parent));
   133  
   134      await render(hbs`
   135        <JobPage::Parts::Children
   136          @job={{job}}
   137          @sortProperty={{sortProperty}}
   138          @sortDescending={{sortDescending}}
   139          @currentPage={{currentPage}}
   140          @gotoJob={{gotoJob}} />
   141      `);
   142  
   143      const sortedChildren = parent.get('children').sortBy('name');
   144      const childRows = findAll('[data-test-job-name]');
   145  
   146      sortedChildren.reverse().forEach((child, index) => {
   147        assert.equal(
   148          childRows[index].textContent.trim(),
   149          child.get('name'),
   150          `Child ${index} is ${child.get('name')}`
   151        );
   152      });
   153  
   154      await this.set('sortDescending', false);
   155  
   156      sortedChildren.forEach((child, index) => {
   157        assert.equal(
   158          childRows[index].textContent.trim(),
   159          child.get('name'),
   160          `Child ${index} is ${child.get('name')}`
   161        );
   162      });
   163    });
   164  });