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

registry_breadcrumb_spec.js « components « explorer « registry « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fb0b98ba00435ad5483a816633d33ed5991f1896 (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
import { shallowMount } from '@vue/test-utils';

import component from '~/registry/explorer/components/registry_breadcrumb.vue';

describe('Registry Breadcrumb', () => {
  let wrapper;
  const nameGenerator = jest.fn();

  const crumb = {
    className: 'foo bar',
    tagName: 'div',
    innerHTML: 'baz',
    querySelector: jest.fn(),
    children: [
      {
        tagName: 'a',
        className: 'foo',
      },
    ],
  };

  const querySelectorReturnValue = {
    classList: ['js-divider'],
    tagName: 'svg',
    innerHTML: 'foo',
  };

  const crumbs = [crumb, { ...crumb, innerHTML: 'foo' }, { ...crumb, className: 'baz' }];

  const routes = [
    { name: 'foo', meta: { nameGenerator, root: true } },
    { name: 'baz', meta: { nameGenerator } },
  ];

  const findDivider = () => wrapper.find('.js-divider');
  const findRootRoute = () => wrapper.find({ ref: 'rootRouteLink' });
  const findChildRoute = () => wrapper.find({ ref: 'childRouteLink' });
  const findLastCrumb = () => wrapper.find({ ref: 'lastCrumb' });

  const mountComponent = $route => {
    wrapper = shallowMount(component, {
      propsData: {
        crumbs,
      },
      stubs: {
        'router-link': { name: 'router-link', template: '<a><slot></slot></a>', props: ['to'] },
      },
      mocks: {
        $route,
        $router: {
          options: {
            routes,
          },
        },
      },
    });
  };

  beforeEach(() => {
    nameGenerator.mockClear();
    crumb.querySelector = jest.fn();
  });

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  describe('when is rootRoute', () => {
    beforeEach(() => {
      mountComponent(routes[0]);
    });

    it('renders', () => {
      expect(wrapper.element).toMatchSnapshot();
    });

    it('contains a router-link for the child route', () => {
      expect(findChildRoute().exists()).toBe(true);
    });

    it('the link text is calculated by nameGenerator', () => {
      expect(nameGenerator).toHaveBeenCalledTimes(1);
    });
  });

  describe('when is not rootRoute', () => {
    beforeEach(() => {
      crumb.querySelector.mockReturnValue(querySelectorReturnValue);
      mountComponent(routes[1]);
    });

    it('renders a divider', () => {
      expect(findDivider().exists()).toBe(true);
    });

    it('contains a router-link for the root route', () => {
      expect(findRootRoute().exists()).toBe(true);
    });

    it('contains a router-link for the child route', () => {
      expect(findChildRoute().exists()).toBe(true);
    });

    it('the link text is calculated by nameGenerator', () => {
      expect(nameGenerator).toHaveBeenCalledTimes(2);
    });
  });

  describe('last crumb', () => {
    const lastChildren = crumb.children[0];
    beforeEach(() => {
      nameGenerator.mockReturnValue('foo');
      mountComponent(routes[0]);
    });

    it('has the same tag as the last children of the crumbs', () => {
      expect(findLastCrumb().element.tagName).toBe(lastChildren.tagName.toUpperCase());
    });

    it('has the same classes as the last children of the crumbs', () => {
      expect(
        findLastCrumb()
          .classes()
          .join(' '),
      ).toEqual(lastChildren.className);
    });

    it('has a link to the current route', () => {
      expect(findChildRoute().props('to')).toEqual({ to: routes[0].name });
    });

    it('the link has the correct text', () => {
      expect(findChildRoute().text()).toEqual('foo');
    });
  });
});