github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/unit/utils/generate-exec-url-test.js (about)

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