github.com/anuvu/nomad@v0.8.7-atom1/ui/tests/integration/job-page/periodic-test.js (about)

     1  import { getOwner } from '@ember/application';
     2  import { test, moduleForComponent } from 'ember-qunit';
     3  import { click, find, findAll } from 'ember-native-dom-helpers';
     4  import wait from 'ember-test-helpers/wait';
     5  import hbs from 'htmlbars-inline-precompile';
     6  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
     7  import { jobURL, stopJob, expectStopError, expectDeleteRequest } from './helpers';
     8  
     9  moduleForComponent('job-page/periodic', 'Integration | Component | job-page/periodic', {
    10    integration: true,
    11    beforeEach() {
    12      window.localStorage.clear();
    13      this.store = getOwner(this).lookup('service:store');
    14      this.server = startMirage();
    15      this.server.create('namespace');
    16    },
    17    afterEach() {
    18      this.server.shutdown();
    19      window.localStorage.clear();
    20    },
    21  });
    22  
    23  const commonTemplate = hbs`
    24    {{job-page/periodic
    25      job=job
    26      sortProperty=sortProperty
    27      sortDescending=sortDescending
    28      currentPage=currentPage
    29      gotoJob=gotoJob}}
    30  `;
    31  
    32  const commonProperties = job => ({
    33    job,
    34    sortProperty: 'name',
    35    sortDescending: true,
    36    currentPage: 1,
    37    gotoJob: () => {},
    38  });
    39  
    40  test('Clicking Force Launch launches a new periodic child job', function(assert) {
    41    const childrenCount = 3;
    42  
    43    this.server.create('job', 'periodic', {
    44      id: 'parent',
    45      childrenCount,
    46      createAllocations: false,
    47    });
    48  
    49    this.store.findAll('job');
    50  
    51    return wait().then(() => {
    52      const job = this.store.peekAll('job').findBy('plainId', 'parent');
    53  
    54      this.setProperties(commonProperties(job));
    55      this.render(commonTemplate);
    56  
    57      return wait().then(() => {
    58        const currentJobCount = server.db.jobs.length;
    59  
    60        assert.equal(
    61          findAll('[data-test-job-name]').length,
    62          childrenCount,
    63          'The new periodic job launch is in the children list'
    64        );
    65  
    66        click('[data-test-force-launch]');
    67  
    68        return wait().then(() => {
    69          const expectedURL = jobURL(job, '/periodic/force');
    70  
    71          assert.ok(
    72            this.server.pretender.handledRequests
    73              .filterBy('method', 'POST')
    74              .find(req => req.url === expectedURL),
    75            'POST URL was correct'
    76          );
    77  
    78          assert.equal(server.db.jobs.length, currentJobCount + 1, 'POST request was made');
    79        });
    80      });
    81    });
    82  });
    83  
    84  test('Clicking force launch without proper permissions shows an error message', function(assert) {
    85    this.server.pretender.post('/v1/job/:id/periodic/force', () => [403, {}, null]);
    86  
    87    this.server.create('job', 'periodic', {
    88      id: 'parent',
    89      childrenCount: 1,
    90      createAllocations: false,
    91      status: 'running',
    92    });
    93  
    94    this.store.findAll('job');
    95  
    96    return wait().then(() => {
    97      const job = this.store.peekAll('job').findBy('plainId', 'parent');
    98  
    99      this.setProperties(commonProperties(job));
   100      this.render(commonTemplate);
   101  
   102      return wait().then(() => {
   103        assert.notOk(find('[data-test-job-error-title]'), 'No error message yet');
   104  
   105        click('[data-test-force-launch]');
   106  
   107        return wait().then(() => {
   108          assert.equal(
   109            find('[data-test-job-error-title]').textContent,
   110            'Could Not Force Launch',
   111            'Appropriate error is shown'
   112          );
   113          assert.ok(
   114            find('[data-test-job-error-body]').textContent.includes('ACL'),
   115            'The error message mentions ACLs'
   116          );
   117  
   118          click('[data-test-job-error-close]');
   119  
   120          assert.notOk(find('[data-test-job-error-title]'), 'Error message is dismissable');
   121        });
   122      });
   123    });
   124  });
   125  
   126  test('Stopping a job sends a delete request for the job', function(assert) {
   127    const mirageJob = this.server.create('job', 'periodic', {
   128      childrenCount: 0,
   129      createAllocations: false,
   130      status: 'running',
   131    });
   132  
   133    let job;
   134    this.store.findAll('job');
   135  
   136    return wait()
   137      .then(() => {
   138        job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   139  
   140        this.setProperties(commonProperties(job));
   141        this.render(commonTemplate);
   142  
   143        return wait();
   144      })
   145      .then(stopJob)
   146      .then(() => expectDeleteRequest(assert, this.server, job));
   147  });
   148  
   149  test('Stopping a job without proper permissions shows an error message', function(assert) {
   150    this.server.pretender.delete('/v1/job/:id', () => [403, {}, null]);
   151  
   152    const mirageJob = this.server.create('job', 'periodic', {
   153      childrenCount: 0,
   154      createAllocations: false,
   155      status: 'running',
   156    });
   157  
   158    this.store.findAll('job');
   159  
   160    return wait()
   161      .then(() => {
   162        const job = this.store.peekAll('job').findBy('plainId', mirageJob.id);
   163  
   164        this.setProperties(commonProperties(job));
   165        this.render(commonTemplate);
   166  
   167        return wait();
   168      })
   169      .then(stopJob)
   170      .then(expectStopError(assert));
   171  });