Welcome to mirror list, hosted at ThFree Co, Russian Federation.

util_spec.js « releases « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f40e572918890f0c1e09f9d9ede08c0460e5fb41 (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
import { cloneDeep } from 'lodash';
import { releaseToApiJson, apiJsonToRelease, convertGraphQLResponse } from '~/releases/util';
import { graphqlReleasesResponse as originalGraphqlReleasesResponse } from './mock_data';

describe('releases/util.js', () => {
  describe('releaseToApiJson', () => {
    it('converts a release JavaScript object into JSON that the Release API can accept', () => {
      const release = {
        tagName: 'tag-name',
        name: 'Release name',
        description: 'Release description',
        milestones: [{ id: 1, title: '13.2' }, { id: 2, title: '13.3' }],
        assets: {
          links: [{ url: 'https://gitlab.example.com/link', linkType: 'other' }],
        },
      };

      const expectedJson = {
        tag_name: 'tag-name',
        ref: null,
        name: 'Release name',
        description: 'Release description',
        milestones: ['13.2', '13.3'],
        assets: {
          links: [{ url: 'https://gitlab.example.com/link', link_type: 'other' }],
        },
      };

      expect(releaseToApiJson(release)).toEqual(expectedJson);
    });

    describe('when createFrom is provided', () => {
      it('adds the provided createFrom ref to the JSON as a "ref" property', () => {
        const createFrom = 'main';

        const release = {};

        const expectedJson = {
          ref: createFrom,
        };

        expect(releaseToApiJson(release, createFrom)).toMatchObject(expectedJson);
      });
    });

    describe('release.name', () => {
      it.each`
        input                 | output
        ${null}               | ${null}
        ${''}                 | ${null}
        ${' \t\n\r\n'}        | ${null}
        ${'  Release name  '} | ${'Release name'}
      `('converts a name like `$input` to `$output`', ({ input, output }) => {
        const release = { name: input };

        const expectedJson = {
          name: output,
        };

        expect(releaseToApiJson(release)).toMatchObject(expectedJson);
      });
    });

    describe('when release.milestones is falsy', () => {
      it('includes a "milestone" property in the returned result as an empty array', () => {
        const release = {};

        const expectedJson = {
          milestones: [],
        };

        expect(releaseToApiJson(release)).toMatchObject(expectedJson);
      });
    });
  });

  describe('apiJsonToRelease', () => {
    it('converts JSON received from the Release API into an object usable by the Vue application', () => {
      const json = {
        tag_name: 'tag-name',
        assets: {
          links: [
            {
              link_type: 'other',
            },
          ],
        },
      };

      const expectedRelease = {
        tagName: 'tag-name',
        assets: {
          links: [
            {
              linkType: 'other',
            },
          ],
        },
        milestones: [],
      };

      expect(apiJsonToRelease(json)).toEqual(expectedRelease);
    });
  });

  describe('convertGraphQLResponse', () => {
    let graphqlReleasesResponse;
    let converted;

    beforeEach(() => {
      graphqlReleasesResponse = cloneDeep(originalGraphqlReleasesResponse);
      converted = convertGraphQLResponse(graphqlReleasesResponse);
    });

    it('matches snapshot', () => {
      expect(converted).toMatchSnapshot();
    });

    describe('assets', () => {
      it("handles asset links that don't have a linkType", () => {
        expect(converted.data[0].assets.links[0].linkType).not.toBeUndefined();

        delete graphqlReleasesResponse.data.project.releases.nodes[0].assets.links.nodes[0]
          .linkType;

        converted = convertGraphQLResponse(graphqlReleasesResponse);

        expect(converted.data[0].assets.links[0].linkType).toBeUndefined();
      });
    });

    describe('_links', () => {
      it("handles releases that don't have any links", () => {
        expect(converted.data[0]._links.selfUrl).not.toBeUndefined();

        delete graphqlReleasesResponse.data.project.releases.nodes[0].links;

        converted = convertGraphQLResponse(graphqlReleasesResponse);

        expect(converted.data[0]._links.selfUrl).toBeUndefined();
      });
    });

    describe('commit', () => {
      it("handles releases that don't have any commit info", () => {
        expect(converted.data[0].commit).not.toBeUndefined();

        delete graphqlReleasesResponse.data.project.releases.nodes[0].commit;

        converted = convertGraphQLResponse(graphqlReleasesResponse);

        expect(converted.data[0].commit).toBeUndefined();
      });
    });
  });
});