github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/ui/tests/integration/job-page/parts/children-test.js (about)

     1  import { assign } from '@ember/polyfills';
     2  import { run } from '@ember/runloop';
     3  import hbs from 'htmlbars-inline-precompile';
     4  import { findAll, find, click } from 'ember-native-dom-helpers';
     5  import sinon from 'sinon';
     6  import { module, test } from 'qunit';
     7  import { setupRenderingTest } from 'ember-qunit';
     8  import { render, settled } from '@ember/test-helpers';
     9  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
    10  
    11  module('Integration | Component | job-page/parts/children', function(hooks) {
    12    setupRenderingTest(hooks);
    13  
    14    hooks.beforeEach(function() {
    15      window.localStorage.clear();
    16      this.store = this.owner.lookup('service:store');
    17      this.server = startMirage();
    18      this.server.create('namespace');
    19    });
    20  
    21    hooks.afterEach(function() {
    22      this.server.shutdown();
    23      window.localStorage.clear();
    24    });
    25  
    26    const props = (job, options = {}) =>
    27      assign(
    28        {
    29          job,
    30          sortProperty: 'name',
    31          sortDescending: true,
    32          currentPage: 1,
    33          gotoJob: () => {},
    34        },
    35        options
    36      );
    37  
    38    test('lists each child', function(assert) {
    39      let parent;
    40  
    41      this.server.create('job', 'periodic', {
    42        id: 'parent',
    43        childrenCount: 3,
    44        createAllocations: false,
    45      });
    46  
    47      this.store.findAll('job');
    48  
    49      return settled().then(async () => {
    50        run(() => {
    51          parent = this.store.peekAll('job').findBy('plainId', 'parent');
    52        });
    53  
    54        this.setProperties(props(parent));
    55  
    56        await render(hbs`
    57          {{job-page/parts/children
    58            job=job
    59            sortProperty=sortProperty
    60            sortDescending=sortDescending
    61            currentPage=currentPage
    62            gotoJob=gotoJob}}
    63        `);
    64  
    65        return settled().then(() => {
    66          assert.equal(
    67            findAll('[data-test-job-name]').length,
    68            parent.get('children.length'),
    69            'A row for each child'
    70          );
    71        });
    72      });
    73    });
    74  
    75    test('eventually paginates', function(assert) {
    76      let parent;
    77  
    78      this.server.create('job', 'periodic', {
    79        id: 'parent',
    80        childrenCount: 11,
    81        createAllocations: false,
    82      });
    83  
    84      this.store.findAll('job');
    85  
    86      return settled().then(async () => {
    87        run(() => {
    88          parent = this.store.peekAll('job').findBy('plainId', 'parent');
    89        });
    90  
    91        this.setProperties(props(parent));
    92  
    93        await render(hbs`
    94          {{job-page/parts/children
    95            job=job
    96            sortProperty=sortProperty
    97            sortDescending=sortDescending
    98            currentPage=currentPage
    99            gotoJob=gotoJob}}
   100        `);
   101  
   102        return settled().then(() => {
   103          const childrenCount = parent.get('children.length');
   104          assert.ok(childrenCount > 10, 'Parent has more children than one page size');
   105          assert.equal(findAll('[data-test-job-name]').length, 10, 'Table length maxes out at 10');
   106          assert.ok(find('.pagination-next'), 'Next button is rendered');
   107  
   108          assert.ok(
   109            new RegExp(`1.10.+?${childrenCount}`).test(find('.pagination-numbers').textContent.trim())
   110          );
   111        });
   112      });
   113    });
   114  
   115    test('is sorted based on the sortProperty and sortDescending properties', function(assert) {
   116      let parent;
   117  
   118      this.server.create('job', 'periodic', {
   119        id: 'parent',
   120        childrenCount: 3,
   121        createAllocations: false,
   122      });
   123  
   124      this.store.findAll('job');
   125  
   126      return settled().then(async () => {
   127        run(() => {
   128          parent = this.store.peekAll('job').findBy('plainId', 'parent');
   129        });
   130  
   131        this.setProperties(props(parent));
   132  
   133        await render(hbs`
   134          {{job-page/parts/children
   135            job=job
   136            sortProperty=sortProperty
   137            sortDescending=sortDescending
   138            currentPage=currentPage
   139            gotoJob=gotoJob}}
   140        `);
   141  
   142        return settled().then(() => {
   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          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          return settled();
   165        });
   166      });
   167    });
   168  
   169    test('gotoJob is called when a job row is clicked', function(assert) {
   170      let parent;
   171      const gotoJobSpy = sinon.spy();
   172  
   173      this.server.create('job', 'periodic', {
   174        id: 'parent',
   175        childrenCount: 1,
   176        createAllocations: false,
   177      });
   178  
   179      this.store.findAll('job');
   180  
   181      return settled().then(async () => {
   182        run(() => {
   183          parent = this.store.peekAll('job').findBy('plainId', 'parent');
   184        });
   185  
   186        this.setProperties(
   187          props(parent, {
   188            gotoJob: gotoJobSpy,
   189          })
   190        );
   191  
   192        await render(hbs`
   193          {{job-page/parts/children
   194            job=job
   195            sortProperty=sortProperty
   196            sortDescending=sortDescending
   197            currentPage=currentPage
   198            gotoJob=gotoJob}}
   199        `);
   200  
   201        return settled().then(() => {
   202          click('tr.job-row');
   203          assert.ok(
   204            gotoJobSpy.withArgs(parent.get('children.firstObject')).calledOnce,
   205            'Clicking the job row calls the gotoJob action'
   206          );
   207        });
   208      });
   209    });
   210  });