summaryrefslogtreecommitdiff
path: root/spec/frontend/packages/shared/utils_spec.js
blob: 1fe90a4827f30d417251502f97d31abca09fef1a (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
import {
  packageTypeToTrackCategory,
  beautifyPath,
  getPackageTypeLabel,
  getCommitLink,
} from '~/packages/shared/utils';
import { PackageType, TrackingCategories } from '~/packages/shared/constants';
import { packageList } from '../mock_data';

describe('Packages shared utils', () => {
  describe('packageTypeToTrackCategory', () => {
    it('prepend UI to package category', () => {
      expect(packageTypeToTrackCategory()).toMatchInlineSnapshot(`"UI::undefined"`);
    });

    it.each(Object.keys(PackageType))('returns a correct category string for %s', packageKey => {
      const packageName = PackageType[packageKey];
      expect(packageTypeToTrackCategory(packageName)).toBe(
        `UI::${TrackingCategories[packageName]}`,
      );
    });
  });

  describe('beautifyPath', () => {
    it('returns a string with spaces around /', () => {
      expect(beautifyPath('foo/bar')).toBe('foo / bar');
    });
    it('does not fail for empty string', () => {
      expect(beautifyPath()).toBe('');
    });
  });

  describe('getPackageTypeLabel', () => {
    describe.each`
      packageType   | expectedResult
      ${'conan'}    | ${'Conan'}
      ${'maven'}    | ${'Maven'}
      ${'npm'}      | ${'NPM'}
      ${'nuget'}    | ${'NuGet'}
      ${'pypi'}     | ${'PyPi'}
      ${'composer'} | ${'Composer'}
      ${'foo'}      | ${null}
    `(`package type`, ({ packageType, expectedResult }) => {
      it(`${packageType} should show as ${expectedResult}`, () => {
        expect(getPackageTypeLabel(packageType)).toBe(expectedResult);
      });
    });
  });

  describe('getCommitLink', () => {
    it('returns a relative link when isGroup is false', () => {
      const link = getCommitLink(packageList[0], false);

      expect(link).toContain('../commit');
    });

    describe('when isGroup is true', () => {
      it('returns an absolute link matching project path', () => {
        const mavenPackage = packageList[0];
        const link = getCommitLink(mavenPackage, true);

        expect(link).toContain(`/${mavenPackage.project_path}/commit`);
      });
    });
  });
});