summaryrefslogtreecommitdiff
path: root/spec/frontend/packages/details/store/getters_spec.js
blob: e5b130cd958e8ff6698ab8f8deb0f89eb44c72a7 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import {
  conanInstallationCommand,
  conanSetupCommand,
  packagePipeline,
  packageTypeDisplay,
  packageIcon,
  mavenInstallationXml,
  mavenInstallationCommand,
  mavenSetupXml,
  npmInstallationCommand,
  npmSetupCommand,
  nugetInstallationCommand,
  nugetSetupCommand,
  pypiPipCommand,
  pypiSetupCommand,
  composerRegistryInclude,
  composerPackageInclude,
  groupExists,
} from '~/packages/details/store/getters';
import { NpmManager } from '~/packages/details/constants';
import {
  conanPackage,
  npmPackage,
  nugetPackage,
  mockPipelineInfo,
  mavenPackage as packageWithoutBuildInfo,
  pypiPackage,
} from '../../mock_data';
import {
  generateMavenCommand,
  generateXmlCodeBlock,
  generateMavenSetupXml,
  registryUrl,
  pypiSetupCommandStr,
} from '../mock_data';

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

  const defaultState = {
    packageEntity: packageWithoutBuildInfo,
    conanPath: registryUrl,
    mavenPath: registryUrl,
    npmPath: registryUrl,
    nugetPath: registryUrl,
    pypiPath: registryUrl,
  };

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

  const conanInstallationCommandStr = `conan install ${conanPackage.name} --remote=gitlab`;
  const conanSetupCommandStr = `conan remote add gitlab ${registryUrl}`;

  const mavenCommandStr = generateMavenCommand(packageWithoutBuildInfo.maven_metadatum);
  const mavenInstallationXmlBlock = generateXmlCodeBlock(packageWithoutBuildInfo.maven_metadatum);
  const mavenSetupXmlBlock = generateMavenSetupXml();

  const npmInstallStr = `npm i ${npmPackage.name}`;
  const npmSetupStr = `echo @Test:registry=${registryUrl}/ >> .npmrc`;
  const yarnInstallStr = `yarn add ${npmPackage.name}`;
  const yarnSetupStr = `echo \\"@Test:registry\\" \\"${registryUrl}/\\" >> .yarnrc`;

  const nugetInstallationCommandStr = `nuget install ${nugetPackage.name} -Source "GitLab"`;
  const nugetSetupCommandStr = `nuget source Add -Name "GitLab" -Source "${registryUrl}" -UserName <your_username> -Password <your_token>`;

  const pypiPipCommandStr = `pip install ${pypiPackage.name} --extra-index-url ${registryUrl}`;
  const composerRegistryIncludeStr =
    'composer config repositories.gitlab.com/123 \'{"type": "composer", "url": "foo"}\'';
  const composerPackageIncludeStr = `composer req ${[packageWithoutBuildInfo.name]}:${
    packageWithoutBuildInfo.version
  }`;

  describe('packagePipeline', () => {
    it('should return the pipeline info when pipeline exists', () => {
      setupState({
        packageEntity: {
          ...npmPackage,
          pipeline: mockPipelineInfo,
        },
      });

      expect(packagePipeline(state)).toEqual(mockPipelineInfo);
    });

    it('should return null when build_info does not exist', () => {
      setupState();

      expect(packagePipeline(state)).toBe(null);
    });
  });

  describe('packageTypeDisplay', () => {
    describe.each`
      packageEntity              | expectedResult
      ${conanPackage}            | ${'Conan'}
      ${packageWithoutBuildInfo} | ${'Maven'}
      ${npmPackage}              | ${'NPM'}
      ${nugetPackage}            | ${'NuGet'}
      ${pypiPackage}             | ${'PyPI'}
    `(`package type`, ({ packageEntity, expectedResult }) => {
      beforeEach(() => setupState({ packageEntity }));

      it(`${packageEntity.package_type} should show as ${expectedResult}`, () => {
        expect(packageTypeDisplay(state)).toBe(expectedResult);
      });
    });
  });

  describe('packageIcon', () => {
    describe('nuget packages', () => {
      it('should return nuget package icon', () => {
        setupState({ packageEntity: nugetPackage });

        expect(packageIcon(state)).toBe(nugetPackage.nuget_metadatum.icon_url);
      });

      it('should return null when nuget package does not have an icon', () => {
        setupState({ packageEntity: { ...nugetPackage, nuget_metadatum: {} } });

        expect(packageIcon(state)).toBe(null);
      });
    });

    it('should not find icons for other package types', () => {
      setupState({ packageEntity: npmPackage });

      expect(packageIcon(state)).toBe(null);
    });
  });

  describe('conan string getters', () => {
    it('gets the correct conanInstallationCommand', () => {
      setupState({ packageEntity: conanPackage });

      expect(conanInstallationCommand(state)).toBe(conanInstallationCommandStr);
    });

    it('gets the correct conanSetupCommand', () => {
      setupState({ packageEntity: conanPackage });

      expect(conanSetupCommand(state)).toBe(conanSetupCommandStr);
    });
  });

  describe('maven string getters', () => {
    it('gets the correct mavenInstallationXml', () => {
      setupState();

      expect(mavenInstallationXml(state)).toBe(mavenInstallationXmlBlock);
    });

    it('gets the correct mavenInstallationCommand', () => {
      setupState();

      expect(mavenInstallationCommand(state)).toBe(mavenCommandStr);
    });

    it('gets the correct mavenSetupXml', () => {
      setupState();

      expect(mavenSetupXml(state)).toBe(mavenSetupXmlBlock);
    });
  });

  describe('npm string getters', () => {
    it('gets the correct npmInstallationCommand for NPM', () => {
      setupState({ packageEntity: npmPackage });

      expect(npmInstallationCommand(state)(NpmManager.NPM)).toBe(npmInstallStr);
    });

    it('gets the correct npmSetupCommand for NPM', () => {
      setupState({ packageEntity: npmPackage });

      expect(npmSetupCommand(state)(NpmManager.NPM)).toBe(npmSetupStr);
    });

    it('gets the correct npmInstallationCommand for Yarn', () => {
      setupState({ packageEntity: npmPackage });

      expect(npmInstallationCommand(state)(NpmManager.YARN)).toBe(yarnInstallStr);
    });

    it('gets the correct npmSetupCommand for Yarn', () => {
      setupState({ packageEntity: npmPackage });

      expect(npmSetupCommand(state)(NpmManager.YARN)).toBe(yarnSetupStr);
    });
  });

  describe('nuget string getters', () => {
    it('gets the correct nugetInstallationCommand', () => {
      setupState({ packageEntity: nugetPackage });

      expect(nugetInstallationCommand(state)).toBe(nugetInstallationCommandStr);
    });

    it('gets the correct nugetSetupCommand', () => {
      setupState({ packageEntity: nugetPackage });

      expect(nugetSetupCommand(state)).toBe(nugetSetupCommandStr);
    });
  });

  describe('pypi string getters', () => {
    it('gets the correct pypiPipCommand', () => {
      setupState({ packageEntity: pypiPackage });

      expect(pypiPipCommand(state)).toBe(pypiPipCommandStr);
    });

    it('gets the correct pypiSetupCommand', () => {
      setupState({ pypiSetupPath: 'foo' });

      expect(pypiSetupCommand(state)).toBe(pypiSetupCommandStr);
    });
  });

  describe('composer string getters', () => {
    it('gets the correct composerRegistryInclude command', () => {
      setupState({ composerPath: 'foo', composerConfigRepositoryName: 'gitlab.com/123' });

      expect(composerRegistryInclude(state)).toBe(composerRegistryIncludeStr);
    });

    it('gets the correct composerPackageInclude command', () => {
      setupState();

      expect(composerPackageInclude(state)).toBe(composerPackageIncludeStr);
    });
  });

  describe('check if group', () => {
    it('is set', () => {
      setupState({ groupListUrl: '/groups/composer/-/packages' });

      expect(groupExists(state)).toBe(true);
    });

    it('is not set', () => {
      setupState({ groupListUrl: '' });

      expect(groupExists(state)).toBe(false);
    });
  });
});