summaryrefslogtreecommitdiff
path: root/spec/frontend/pipelines/unwrapping_utils_spec.js
blob: 3533599611f6d6a52bc0ac7e5ceac05a2042500b (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import {
  unwrapArrayOfJobs,
  unwrapGroups,
  unwrapNodesWithName,
  unwrapStagesWithNeeds,
} from '~/pipelines/components/unwrapping_utils';

const groupsArray = [
  {
    name: 'build_a',
    size: 1,
    status: {
      label: 'passed',
      group: 'success',
      icon: 'status_success',
    },
  },
  {
    name: 'bob_the_build',
    size: 1,
    status: {
      label: 'passed',
      group: 'success',
      icon: 'status_success',
    },
  },
];

const basicStageInfo = {
  name: 'center_stage',
  status: {
    action: null,
  },
};

const stagesAndGroups = [
  {
    ...basicStageInfo,
    groups: {
      nodes: groupsArray,
    },
  },
];

const needArray = [
  {
    name: 'build_b',
  },
];

const elephantArray = [
  {
    name: 'build_b',
    elephant: 'gray',
  },
];

const baseJobs = {
  name: 'test_d',
  status: {
    icon: 'status_success',
    tooltip: null,
    hasDetails: true,
    detailsPath: '/root/abcd-dag/-/pipelines/162',
    group: 'success',
    action: null,
  },
};

const jobArrayWithNeeds = [
  {
    ...baseJobs,
    needs: {
      nodes: needArray,
    },
  },
];

const jobArrayWithElephant = [
  {
    ...baseJobs,
    needs: {
      nodes: elephantArray,
    },
  },
];

const completeMock = [
  {
    ...basicStageInfo,
    groups: {
      nodes: groupsArray.map(group => ({ ...group, jobs: { nodes: jobArrayWithNeeds } })),
    },
  },
];

describe('Shared pipeline unwrapping utils', () => {
  describe('unwrapArrayOfJobs', () => {
    it('returns an empty array if the input is an empty undefined', () => {
      expect(unwrapArrayOfJobs(undefined)).toEqual([]);
    });

    it('returns an empty array if the input is an empty array', () => {
      expect(unwrapArrayOfJobs([])).toEqual([]);
    });

    it('returns a flatten array of each job with their data and stage name', () => {
      expect(
        unwrapArrayOfJobs([
          { name: 'build', groups: [{ name: 'job_a_1' }, { name: 'job_a_2' }] },
          { name: 'test', groups: [{ name: 'job_b' }] },
        ]),
      ).toMatchObject([
        { category: 'build', name: 'job_a_1' },
        { category: 'build', name: 'job_a_2' },
        { category: 'test', name: 'job_b' },
      ]);
    });
  });

  describe('unwrapGroups', () => {
    it('takes stages without nodes and returns the unwrapped groups', () => {
      expect(unwrapGroups(stagesAndGroups)[0].groups).toEqual(groupsArray);
    });

    it('keeps other stage properties intact', () => {
      expect(unwrapGroups(stagesAndGroups)[0]).toMatchObject(basicStageInfo);
    });
  });

  describe('unwrapNodesWithName', () => {
    it('works with no field argument', () => {
      expect(unwrapNodesWithName(jobArrayWithNeeds, 'needs')[0].needs).toEqual([needArray[0].name]);
    });

    it('works with custom field argument', () => {
      expect(unwrapNodesWithName(jobArrayWithElephant, 'needs', 'elephant')[0].needs).toEqual([
        elephantArray[0].elephant,
      ]);
    });
  });

  describe('unwrapStagesWithNeeds', () => {
    it('removes nodes from groups, jobs, and needs', () => {
      const firstProcessedGroup = unwrapStagesWithNeeds(completeMock)[0].groups[0];
      expect(firstProcessedGroup).toMatchObject(groupsArray[0]);
      expect(firstProcessedGroup.jobs[0]).toMatchObject(baseJobs);
      expect(firstProcessedGroup.jobs[0].needs[0]).toBe(needArray[0].name);
    });
  });
});