summaryrefslogtreecommitdiff
path: root/spec/frontend/pipelines/pipeline_graph/utils_spec.js
blob: 12154df6fcffaec5a818b635af2d32f54043782d (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
import { createJobsHash, generateJobNeedsDict } from '~/pipelines/utils';

describe('utils functions', () => {
  const jobName1 = 'build_1';
  const jobName2 = 'build_2';
  const jobName3 = 'test_1';
  const jobName4 = 'deploy_1';
  const job1 = { name: jobName1, script: 'echo hello', stage: 'build' };
  const job2 = { name: jobName2, script: 'echo build', stage: 'build' };
  const job3 = {
    name: jobName3,
    script: 'echo test',
    stage: 'test',
    needs: [jobName1, jobName2],
  };
  const job4 = {
    name: jobName4,
    script: 'echo deploy',
    stage: 'deploy',
    needs: [jobName3],
  };
  const userDefinedStage = 'myStage';

  const pipelineGraphData = {
    stages: [
      {
        name: userDefinedStage,
        groups: [],
      },
      {
        name: job4.stage,
        groups: [
          {
            name: jobName4,
            jobs: [{ ...job4 }],
          },
        ],
      },
      {
        name: job1.stage,
        groups: [
          {
            name: jobName1,
            jobs: [{ ...job1 }],
          },
          {
            name: jobName2,
            jobs: [{ ...job2 }],
          },
        ],
      },
      {
        name: job3.stage,
        groups: [
          {
            name: jobName3,
            jobs: [{ ...job3 }],
          },
        ],
      },
    ],
  };

  describe('createJobsHash', () => {
    it('returns an empty object if there are no jobs received as argument', () => {
      expect(createJobsHash([])).toEqual({});
    });

    it('returns a hash with the jobname as key and all its data as value', () => {
      const jobs = {
        [jobName1]: job1,
        [jobName2]: job2,
        [jobName3]: job3,
        [jobName4]: job4,
      };

      expect(createJobsHash(pipelineGraphData.stages)).toEqual(jobs);
    });
  });

  describe('generateJobNeedsDict', () => {
    it('generates an empty object if it receives no jobs', () => {
      expect(generateJobNeedsDict({})).toEqual({});
    });

    it('generates a dict with empty needs if there are no dependencies', () => {
      const smallGraph = {
        [jobName1]: job1,
        [jobName2]: job2,
      };

      expect(generateJobNeedsDict(smallGraph)).toEqual({
        [jobName1]: [],
        [jobName2]: [],
      });
    });

    it('generates a dict where key is the a job and its value is an array of all its needs', () => {
      const jobsWithNeeds = {
        [jobName1]: job1,
        [jobName2]: job2,
        [jobName3]: job3,
        [jobName4]: job4,
      };

      expect(generateJobNeedsDict(jobsWithNeeds)).toEqual({
        [jobName1]: [],
        [jobName2]: [],
        [jobName3]: [jobName1, jobName2],
        [jobName4]: [jobName3, jobName1, jobName2],
      });
    });
  });
});