summaryrefslogtreecommitdiff
path: root/spec/frontend/ci/pipeline_new/utils/format_refs_spec.js
blob: 137a93396490ce204a333fa22ffaffdc8a90023a (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
import { BRANCH_REF_TYPE, TAG_REF_TYPE } from '~/ci/pipeline_new/constants';
import {
  formatRefs,
  formatListBoxItems,
  searchByFullNameInListboxOptions,
} from '~/ci/pipeline_new/utils/format_refs';
import { mockBranchRefs, mockTagRefs } from '../mock_data';

describe('Format refs util', () => {
  it('formats branch ref correctly', () => {
    expect(formatRefs(mockBranchRefs, BRANCH_REF_TYPE)).toEqual([
      { fullName: 'refs/heads/main', shortName: 'main' },
      { fullName: 'refs/heads/dev', shortName: 'dev' },
      { fullName: 'refs/heads/release', shortName: 'release' },
    ]);
  });

  it('formats tag ref correctly', () => {
    expect(formatRefs(mockTagRefs, TAG_REF_TYPE)).toEqual([
      { fullName: 'refs/tags/1.0.0', shortName: '1.0.0' },
      { fullName: 'refs/tags/1.1.0', shortName: '1.1.0' },
      { fullName: 'refs/tags/1.2.0', shortName: '1.2.0' },
    ]);
  });
});

describe('formatListBoxItems', () => {
  it('formats branches and tags to listbox items correctly', () => {
    expect(formatListBoxItems(mockBranchRefs, mockTagRefs)).toEqual([
      {
        text: 'Branches',
        options: [
          { value: 'refs/heads/main', text: 'main' },
          { value: 'refs/heads/dev', text: 'dev' },
          { value: 'refs/heads/release', text: 'release' },
        ],
      },
      {
        text: 'Tags',
        options: [
          { value: 'refs/tags/1.0.0', text: '1.0.0' },
          { value: 'refs/tags/1.1.0', text: '1.1.0' },
          { value: 'refs/tags/1.2.0', text: '1.2.0' },
        ],
      },
    ]);

    expect(formatListBoxItems(mockBranchRefs, [])).toEqual([
      {
        text: 'Branches',
        options: [
          { value: 'refs/heads/main', text: 'main' },
          { value: 'refs/heads/dev', text: 'dev' },
          { value: 'refs/heads/release', text: 'release' },
        ],
      },
    ]);

    expect(formatListBoxItems([], mockTagRefs)).toEqual([
      {
        text: 'Tags',
        options: [
          { value: 'refs/tags/1.0.0', text: '1.0.0' },
          { value: 'refs/tags/1.1.0', text: '1.1.0' },
          { value: 'refs/tags/1.2.0', text: '1.2.0' },
        ],
      },
    ]);
  });
});

describe('searchByFullNameInListboxOptions', () => {
  const listbox = formatListBoxItems(mockBranchRefs, mockTagRefs);

  it.each`
    fullName             | expectedResult
    ${'refs/heads/main'} | ${{ fullName: 'refs/heads/main', shortName: 'main' }}
    ${'refs/tags/1.0.0'} | ${{ fullName: 'refs/tags/1.0.0', shortName: '1.0.0' }}
  `('should search item in listbox correctly', ({ fullName, expectedResult }) => {
    expect(searchByFullNameInListboxOptions(fullName, listbox)).toEqual(expectedResult);
  });
});