summaryrefslogtreecommitdiff
path: root/spec/frontend/registry/explorer/utils_spec.js
blob: 7a5d6958a093b92f59f0dc324f125d3b34b44190 (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
import { pathGenerator } from '~/registry/explorer/utils';

describe('Utils', () => {
  describe('pathGenerator', () => {
    const imageDetails = {
      path: 'foo/bar/baz',
      name: 'baz',
      id: 1,
    };

    beforeEach(() => {
      window.gon.relative_url_root = null;
    });

    it('returns the fetch url when no ending is passed', () => {
      expect(pathGenerator(imageDetails)).toBe('/foo/bar/registry/repository/1/tags?format=json');
    });

    it('returns the url with an ending when is passed', () => {
      expect(pathGenerator(imageDetails, '/foo')).toBe('/foo/bar/registry/repository/1/tags/foo');
    });

    describe.each`
      path                     | name         | result
      ${'foo/foo'}             | ${''}        | ${'/foo/foo/registry/repository/1/tags?format=json'}
      ${'foo/foo/foo'}         | ${'foo'}     | ${'/foo/foo/registry/repository/1/tags?format=json'}
      ${'baz/foo/foo/foo'}     | ${'foo'}     | ${'/baz/foo/foo/registry/repository/1/tags?format=json'}
      ${'baz/foo/foo/foo'}     | ${'foo'}     | ${'/baz/foo/foo/registry/repository/1/tags?format=json'}
      ${'foo/foo/baz/foo/foo'} | ${'foo/foo'} | ${'/foo/foo/baz/registry/repository/1/tags?format=json'}
      ${'foo/foo/baz/foo/bar'} | ${'foo/bar'} | ${'/foo/foo/baz/registry/repository/1/tags?format=json'}
      ${'baz/foo/foo'}         | ${'foo'}     | ${'/baz/foo/registry/repository/1/tags?format=json'}
      ${'baz/foo/bar'}         | ${'foo'}     | ${'/baz/foo/bar/registry/repository/1/tags?format=json'}
    `('when path is $path and name is $name', ({ name, path, result }) => {
      it('returns the correct value', () => {
        expect(pathGenerator({ id: 1, name, path })).toBe(result);
      });

      it('produces a correct relative url', () => {
        window.gon.relative_url_root = '/gitlab';
        expect(pathGenerator({ id: 1, name, path })).toBe(`/gitlab${result}`);
      });
    });

    it('returns the url unchanged when imageDetails have no name', () => {
      const imageDetailsWithoutName = {
        path: 'foo/bar/baz',
        name: '',
        id: 1,
      };

      expect(pathGenerator(imageDetailsWithoutName)).toBe(
        '/foo/bar/baz/registry/repository/1/tags?format=json',
      );
    });
  });
});