github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/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({
    26          jobId: job.id,
    27        }).models[0];
    28  
    29        this.system.get('shouldIncludeRegion');
    30        await this.system.get('defaultRegion');
    31  
    32        const deployment = await this.store.findRecord(
    33          'deployment',
    34          deploymentRecord.id
    35        );
    36        this.server.pretender.handledRequests.length = 0;
    37  
    38        return deployment;
    39      };
    40    });
    41  
    42    hooks.afterEach(function () {
    43      this.server.shutdown();
    44    });
    45  
    46    const testCases = [
    47      {
    48        variation: '',
    49        region: null,
    50        fail: (id) => `POST /v1/deployment/fail/${id}`,
    51        promote: (id) => `POST /v1/deployment/promote/${id}`,
    52      },
    53      {
    54        variation: 'with non-default region',
    55        region: 'region-2',
    56        fail: (id) => `POST /v1/deployment/fail/${id}?region=region-2`,
    57        promote: (id) => `POST /v1/deployment/promote/${id}?region=region-2`,
    58      },
    59    ];
    60  
    61    testCases.forEach((testCase) => {
    62      test(`promote makes the correct API call ${testCase.variation}`, async function (assert) {
    63        const deployment = await this.initialize({ region: testCase.region });
    64        await this.subject().promote(deployment);
    65  
    66        const request = this.server.pretender.handledRequests[0];
    67  
    68        assert.equal(
    69          `${request.method} ${request.url}`,
    70          testCase.promote(deployment.id)
    71        );
    72        assert.deepEqual(JSON.parse(request.requestBody), {
    73          DeploymentId: deployment.id,
    74          All: true,
    75        });
    76      });
    77  
    78      test(`fail makes the correct API call ${testCase.variation}`, async function (assert) {
    79        const deployment = await this.initialize({ region: testCase.region });
    80        await this.subject().fail(deployment);
    81  
    82        const request = this.server.pretender.handledRequests[0];
    83  
    84        assert.equal(
    85          `${request.method} ${request.url}`,
    86          testCase.fail(deployment.id)
    87        );
    88        assert.deepEqual(JSON.parse(request.requestBody), {
    89          DeploymentId: deployment.id,
    90        });
    91      });
    92    });
    93  });