github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/tests/integration/job-page/parts/children-test.js (about)

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