summaryrefslogtreecommitdiff
path: root/spec/frontend/runner/runner_update_form_utils_spec.js
blob: a633aee92f785f65ad6774dec61261330337ee89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { ACCESS_LEVEL_NOT_PROTECTED } from '~/runner/constants';
import { modelToUpdateMutationVariables, runnerToModel } from '~/runner/runner_update_form_utils';

const mockId = 'gid://gitlab/Ci::Runner/1';
const mockDescription = 'Runner Desc.';

const mockRunner = {
  id: mockId,
  description: mockDescription,
  maximumTimeout: 100,
  accessLevel: ACCESS_LEVEL_NOT_PROTECTED,
  active: true,
  locked: true,
  runUntagged: true,
  tagList: ['tag-1', 'tag-2'],
};

const mockModel = {
  ...mockRunner,
  tagList: 'tag-1, tag-2',
};

describe('~/runner/runner_update_form_utils', () => {
  describe('runnerToModel', () => {
    it('collects all model data', () => {
      expect(runnerToModel(mockRunner)).toEqual(mockModel);
    });

    it('does not collect other data', () => {
      const model = runnerToModel({
        ...mockRunner,
        unrelated: 'unrelatedValue',
      });

      expect(model.unrelated).toEqual(undefined);
    });

    it('tag list defaults to an empty string', () => {
      const model = runnerToModel({
        ...mockRunner,
        tagList: undefined,
      });

      expect(model.tagList).toEqual('');
    });
  });

  describe('modelToUpdateMutationVariables', () => {
    it('collects all model data', () => {
      expect(modelToUpdateMutationVariables(mockModel)).toEqual({
        input: {
          ...mockRunner,
        },
      });
    });

    it('collects a nullable timeout from the model', () => {
      const variables = modelToUpdateMutationVariables({
        ...mockModel,
        maximumTimeout: '',
      });

      expect(variables).toEqual({
        input: {
          ...mockRunner,
          maximumTimeout: null,
        },
      });
    });

    it.each`
      tagList                       | tagListInput
      ${''}                         | ${[]}
      ${'tag1, tag2'}               | ${['tag1', 'tag2']}
      ${'with spaces'}              | ${['with spaces']}
      ${',,,,, commas'}             | ${['commas']}
      ${'more ,,,,, commas'}        | ${['more', 'commas']}
      ${'  trimmed  ,  trimmed2  '} | ${['trimmed', 'trimmed2']}
    `('collect tags separated by commas for "$value"', ({ tagList, tagListInput }) => {
      const variables = modelToUpdateMutationVariables({
        ...mockModel,
        tagList,
      });

      expect(variables).toEqual({
        input: {
          ...mockRunner,
          tagList: tagListInput,
        },
      });
    });
  });
});