github.com/hernad/nomad@v1.6.112/ui/tests/unit/utils/generate-exec-url-test.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import generateExecUrl from 'nomad-ui/utils/generate-exec-url';
     7  import { module, test } from 'qunit';
     8  import sinon from 'sinon';
     9  
    10  const emptyOptions = { queryParams: {} };
    11  
    12  module('Unit | Utility | generate-exec-url', function (hooks) {
    13    hooks.beforeEach(function () {
    14      this.urlForSpy = sinon.spy();
    15      this.router = { urlFor: this.urlForSpy, currentRoute: { queryParams: {} } };
    16    });
    17  
    18    test('it generates an exec job URL', function (assert) {
    19      generateExecUrl(this.router, { job: { plainId: 'job-name' } });
    20  
    21      assert.ok(this.urlForSpy.calledWith('exec', 'job-name', emptyOptions));
    22    });
    23  
    24    test('it generates an exec job URL with an allocation and task group when there are multiple tasks', function (assert) {
    25      generateExecUrl(this.router, {
    26        job: { plainId: 'job-name' },
    27        allocation: {
    28          shortId: 'allocation-short-id',
    29          taskGroup: { name: 'task-group-name', tasks: [0, 1, 2] },
    30        },
    31      });
    32  
    33      assert.ok(
    34        this.urlForSpy.calledWith(
    35          'exec.task-group',
    36          'job-name',
    37          'task-group-name',
    38          {
    39            queryParams: { allocation: 'allocation-short-id' },
    40          }
    41        )
    42      );
    43    });
    44  
    45    test('it generates an exec job URL with an allocation, task group, and task when there is only one task', function (assert) {
    46      generateExecUrl(this.router, {
    47        job: { plainId: 'job-name' },
    48        allocation: {
    49          shortId: 'allocation-short-id',
    50          taskGroup: { name: 'task-group-name', tasks: [{ name: 'task-name' }] },
    51        },
    52      });
    53  
    54      assert.ok(
    55        this.urlForSpy.calledWith(
    56          'exec.task-group.task',
    57          'job-name',
    58          'task-group-name',
    59          'task-name',
    60          {
    61            queryParams: { allocation: 'allocation-short-id' },
    62          }
    63        )
    64      );
    65    });
    66  
    67    test('it generates an exec task group URL', function (assert) {
    68      generateExecUrl(this.router, {
    69        job: { plainId: 'job-name' },
    70        taskGroup: { name: 'task-group-name' },
    71      });
    72  
    73      assert.ok(
    74        this.urlForSpy.calledWith(
    75          'exec.task-group',
    76          'job-name',
    77          'task-group-name',
    78          emptyOptions
    79        )
    80      );
    81    });
    82  
    83    test('it generates an exec task URL', function (assert) {
    84      generateExecUrl(this.router, {
    85        allocation: { shortId: 'allocation-short-id' },
    86        job: { plainId: 'job-name' },
    87        taskGroup: { name: 'task-group-name' },
    88        task: { name: 'task-name' },
    89      });
    90  
    91      assert.ok(
    92        this.urlForSpy.calledWith(
    93          'exec.task-group.task',
    94          'job-name',
    95          'task-group-name',
    96          'task-name',
    97          { queryParams: { allocation: 'allocation-short-id' } }
    98        )
    99      );
   100    });
   101  
   102    test('it generates an exec task URL without an allocation', function (assert) {
   103      generateExecUrl(this.router, {
   104        job: { plainId: 'job-name' },
   105        taskGroup: { name: 'task-group-name' },
   106        task: { name: 'task-name' },
   107      });
   108  
   109      assert.ok(
   110        this.urlForSpy.calledWith(
   111          'exec.task-group.task',
   112          'job-name',
   113          'task-group-name',
   114          'task-name'
   115        )
   116      );
   117    });
   118  
   119    test('it includes job namespace and region when they exist', function (assert) {
   120      generateExecUrl(this.router, {
   121        job: {
   122          namespace: {
   123            name: 'a-namespace',
   124          },
   125          plainId: 'job-name',
   126          region: 'a-region',
   127        },
   128        allocation: {
   129          shortId: 'id',
   130          taskGroup: { name: 'task-group-name', tasks: [0, 1] },
   131        },
   132      });
   133  
   134      assert.ok(
   135        this.urlForSpy.calledWith(
   136          'exec.task-group',
   137          'job-name',
   138          'task-group-name',
   139          {
   140            queryParams: {
   141              allocation: 'id',
   142              namespace: 'a-namespace',
   143              region: 'a-region',
   144            },
   145          }
   146        )
   147      );
   148    });
   149  });