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

utils_spec.js « shared « packages_and_registries « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1dc6bb261def9ff2deeac29785520d330aceef07 (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
import {
  getQueryParams,
  keyValueToFilterToken,
  searchArrayToFilterTokens,
  extractFilterAndSorting,
  extractPageInfo,
  beautifyPath,
  getCommitLink,
} from '~/packages_and_registries/shared/utils';
import { FILTERED_SEARCH_TERM } from '~/vue_shared/components/filtered_search_bar/constants';

import { packageList } from 'jest/packages_and_registries/infrastructure_registry/components/mock_data';

describe('Packages And Registries shared utils', () => {
  describe('getQueryParams', () => {
    it('returns an object from a query string, with arrays', () => {
      const queryString = 'foo=bar&baz[]=1&baz[]=2';

      expect(getQueryParams(queryString)).toStrictEqual({ foo: 'bar', baz: ['1', '2'] });
    });
  });

  describe('keyValueToFilterToken', () => {
    it('returns an object in the correct form', () => {
      const type = 'myType';
      const data = 1;

      expect(keyValueToFilterToken(type, data)).toStrictEqual({ type, value: { data } });
    });
  });

  describe('searchArrayToFilterTokens', () => {
    it('returns an array of objects in the correct form', () => {
      const search = ['one', 'two'];

      expect(searchArrayToFilterTokens(search)).toStrictEqual([
        { type: FILTERED_SEARCH_TERM, value: { data: 'one' } },
        { type: FILTERED_SEARCH_TERM, value: { data: 'two' } },
      ]);
    });
  });
  describe('extractFilterAndSorting', () => {
    it.each`
      search     | type        | sort     | orderBy  | result
      ${['one']} | ${'myType'} | ${'asc'} | ${'foo'} | ${{ sorting: { sort: 'asc', orderBy: 'foo' }, filters: [{ type: 'type', value: { data: 'myType' } }, { type: FILTERED_SEARCH_TERM, value: { data: 'one' } }] }}
      ${['one']} | ${null}     | ${'asc'} | ${'foo'} | ${{ sorting: { sort: 'asc', orderBy: 'foo' }, filters: [{ type: FILTERED_SEARCH_TERM, value: { data: 'one' } }] }}
      ${[]}      | ${null}     | ${'asc'} | ${'foo'} | ${{ sorting: { sort: 'asc', orderBy: 'foo' }, filters: [] }}
      ${null}    | ${null}     | ${'asc'} | ${'foo'} | ${{ sorting: { sort: 'asc', orderBy: 'foo' }, filters: [] }}
      ${null}    | ${null}     | ${null}  | ${'foo'} | ${{ sorting: { orderBy: 'foo' }, filters: [] }}
      ${null}    | ${null}     | ${null}  | ${null}  | ${{ sorting: {}, filters: [] }}
    `(
      'returns sorting and filters objects in the correct form',
      ({ search, type, sort, orderBy, result }) => {
        const queryObject = {
          search,
          type,
          sort,
          orderBy,
        };
        expect(extractFilterAndSorting(queryObject)).toStrictEqual(result);
      },
    );
  });

  describe('extractPageInfo', () => {
    it.each`
      after    | before   | result
      ${null}  | ${null}  | ${{ after: null, before: null }}
      ${'123'} | ${null}  | ${{ after: '123', before: null }}
      ${null}  | ${'123'} | ${{ after: null, before: '123' }}
    `('returns pagination objects', ({ after, before, result }) => {
      const queryObject = {
        after,
        before,
      };
      expect(extractPageInfo(queryObject)).toStrictEqual(result);
    });
  });

  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('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`);
      });
    });
  });
});