github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/unit/adapters/deployment-test.js (about)

     1  import { module, test } from 'qunit';
     2  import { setupTest } from 'ember-qunit';
     3  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
     4  
     5  module('Unit | Adapter | Deployment', function(hooks) {
     6    setupTest(hooks);
     7  
     8    hooks.beforeEach(async function() {
     9      this.store = this.owner.lookup('service:store');
    10      this.system = this.owner.lookup('service:system');
    11      this.subject = () => this.store.adapterFor('deployment');
    12  
    13      window.localStorage.clear();
    14  
    15      this.server = startMirage();
    16  
    17      this.initialize = async ({ region } = {}) => {
    18        if (region) window.localStorage.nomadActiveRegion = region;
    19  
    20        this.server.create('region', { id: 'region-1' });
    21        this.server.create('region', { id: 'region-2' });
    22  
    23        this.server.create('node');
    24        const job = this.server.create('job', { createAllocations: false });
    25        const deploymentRecord = server.schema.deployments.where({ jobId: job.id }).models[0];
    26  
    27        this.system.get('shouldIncludeRegion');
    28        await this.system.get('defaultRegion');
    29  
    30        const deployment = await this.store.findRecord('deployment', deploymentRecord.id);
    31        this.server.pretender.handledRequests.length = 0;
    32  
    33        return deployment;
    34      };
    35    });
    36  
    37    hooks.afterEach(function() {
    38      this.server.shutdown();
    39    });
    40  
    41    const testCases = [
    42      {
    43        variation: '',
    44        region: null,
    45        fail: id => `POST /v1/deployment/fail/${id}`,
    46        promote: id => `POST /v1/deployment/promote/${id}`,
    47      },
    48      {
    49        variation: 'with non-default region',
    50        region: 'region-2',
    51        fail: id => `POST /v1/deployment/fail/${id}?region=region-2`,
    52        promote: id => `POST /v1/deployment/promote/${id}?region=region-2`,
    53      },
    54    ];
    55  
    56    testCases.forEach(testCase => {
    57      test(`promote makes the correct API call ${testCase.variation}`, async function(assert) {
    58        const deployment = await this.initialize({ region: testCase.region });
    59        await this.subject().promote(deployment);
    60  
    61        const request = this.server.pretender.handledRequests[0];
    62  
    63        assert.equal(`${request.method} ${request.url}`, testCase.promote(deployment.id));
    64        assert.deepEqual(JSON.parse(request.requestBody), {
    65          DeploymentId: deployment.id,
    66          All: true,
    67        });
    68      });
    69  
    70      test(`fail makes the correct API call ${testCase.variation}`, async function(assert) {
    71        const deployment = await this.initialize({ region: testCase.region });
    72        await this.subject().fail(deployment);
    73  
    74        const request = this.server.pretender.handledRequests[0];
    75  
    76        assert.equal(`${request.method} ${request.url}`, testCase.fail(deployment.id));
    77        assert.deepEqual(JSON.parse(request.requestBody), {
    78          DeploymentId: deployment.id,
    79        });
    80      });
    81    });
    82  });