summaryrefslogtreecommitdiff
path: root/spec/frontend/pipelines/test_reports/stores/getters_spec.js
blob: f8298fdaba5432e05a9cbaa4f011245a50b61236 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { getJSONFixture } from 'helpers/fixtures';
import * as getters from '~/pipelines/stores/test_reports/getters';
import {
  iconForTestStatus,
  formatFilePath,
  formattedTime,
} from '~/pipelines/stores/test_reports/utils';

describe('Getters TestReports Store', () => {
  let state;

  const testReports = getJSONFixture('pipelines/test_report.json');

  const defaultState = {
    blobPath: '/test/blob/path',
    testReports,
    selectedSuiteIndex: 0,
    pageInfo: {
      page: 1,
      perPage: 2,
    },
  };

  const emptyState = {
    blobPath: '',
    testReports: {},
    selectedSuite: null,
    pageInfo: {
      page: 1,
      perPage: 2,
    },
  };

  beforeEach(() => {
    state = {
      testReports,
    };
  });

  const setupState = (testState = defaultState) => {
    state = testState;
  };

  describe('getTestSuites', () => {
    it('should return the test suites', () => {
      setupState();

      const suites = getters.getTestSuites(state);
      const expected = testReports.test_suites.map((x) => ({
        ...x,
        formattedTime: formattedTime(x.total_time),
      }));

      expect(suites).toEqual(expected);
    });

    it('should return an empty array when testReports is empty', () => {
      setupState(emptyState);

      expect(getters.getTestSuites(state)).toEqual([]);
    });
  });

  describe('getSelectedSuite', () => {
    it('should return the selected suite', () => {
      setupState();

      const selectedSuite = getters.getSelectedSuite(state);
      const expected = testReports.test_suites[state.selectedSuiteIndex];

      expect(selectedSuite).toEqual(expected);
    });
  });

  describe('getSuiteTests', () => {
    it('should return the current page of test cases inside the suite', () => {
      setupState();

      const cases = getters.getSuiteTests(state);
      const expected = testReports.test_suites[0].test_cases
        .map((x) => ({
          ...x,
          filePath: `${state.blobPath}/${formatFilePath(x.file)}`,
          formattedTime: formattedTime(x.execution_time),
          icon: iconForTestStatus(x.status),
        }))
        .slice(0, state.pageInfo.perPage);

      expect(cases).toEqual(expected);
    });

    it('should return an empty array when testReports is empty', () => {
      setupState(emptyState);

      expect(getters.getSuiteTests(state)).toEqual([]);
    });

    describe('when a test case classname property is null', () => {
      it('should return an empty string value for the classname property', () => {
        const testCases = testReports.test_suites[0].test_cases;
        setupState({
          ...defaultState,
          testReports: {
            ...testReports,
            test_suites: [
              {
                test_cases: testCases.map((testCase) => ({
                  ...testCase,
                  classname: null,
                })),
              },
            ],
          },
        });

        const expected = testCases
          .map((x) => ({
            ...x,
            classname: '',
            filePath: `${state.blobPath}/${formatFilePath(x.file)}`,
            formattedTime: formattedTime(x.execution_time),
            icon: iconForTestStatus(x.status),
          }))
          .slice(0, state.pageInfo.perPage);

        expect(getters.getSuiteTests(state)).toEqual(expected);
      });
    });

    describe('when a test case name property is null', () => {
      it('should return an empty string value for the name property', () => {
        const testCases = testReports.test_suites[0].test_cases;
        setupState({
          ...defaultState,
          testReports: {
            ...testReports,
            test_suites: [
              {
                test_cases: testCases.map((testCase) => ({
                  ...testCase,
                  name: null,
                })),
              },
            ],
          },
        });

        const expected = testCases
          .map((x) => ({
            ...x,
            name: '',
            filePath: `${state.blobPath}/${formatFilePath(x.file)}`,
            formattedTime: formattedTime(x.execution_time),
            icon: iconForTestStatus(x.status),
          }))
          .slice(0, state.pageInfo.perPage);

        expect(getters.getSuiteTests(state)).toEqual(expected);
      });
    });
  });

  describe('getSuiteTestCount', () => {
    it('should return the total number of test cases', () => {
      setupState();

      const testCount = getters.getSuiteTestCount(state);
      const expected = testReports.test_suites[0].test_cases.length;

      expect(testCount).toEqual(expected);
    });
  });
});