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

context_switcher_spec.js « components « super_sidebar « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 538e87cf843afe818024e0f24b49ab8f4ce72da3 (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
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import { GlSearchBoxByType } from '@gitlab/ui';
import * as Sentry from '@sentry/browser';
import { s__ } from '~/locale';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import ContextSwitcher from '~/super_sidebar/components/context_switcher.vue';
import ProjectsList from '~/super_sidebar/components/projects_list.vue';
import GroupsList from '~/super_sidebar/components/groups_list.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import searchUserProjectsAndGroupsQuery from '~/super_sidebar/graphql/queries/search_user_groups_and_projects.query.graphql';
import { trackContextAccess, formatContextSwitcherItems } from '~/super_sidebar/utils';
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
import waitForPromises from 'helpers/wait_for_promises';
import { stubComponent } from 'helpers/stub_component';
import { searchUserProjectsAndGroupsResponseMock } from '../mock_data';

jest.mock('~/super_sidebar/utils', () => ({
  getStorageKeyFor: jest.requireActual('~/super_sidebar/utils').getStorageKeyFor,
  getTopFrequentItems: jest.requireActual('~/super_sidebar/utils').getTopFrequentItems,
  formatContextSwitcherItems: jest.requireActual('~/super_sidebar/utils')
    .formatContextSwitcherItems,
  trackContextAccess: jest.fn(),
}));

const username = 'root';
const projectsPath = 'projectsPath';
const groupsPath = 'groupsPath';

Vue.use(VueApollo);

describe('ContextSwitcher component', () => {
  let wrapper;
  let mockApollo;

  const findSearchBox = () => wrapper.findComponent(GlSearchBoxByType);
  const findProjectsList = () => wrapper.findComponent(ProjectsList);
  const findGroupsList = () => wrapper.findComponent(GroupsList);

  const triggerSearchQuery = async () => {
    findSearchBox().vm.$emit('input', 'foo');
    await nextTick();
    jest.advanceTimersByTime(DEFAULT_DEBOUNCE_AND_THROTTLE_MS);
    return waitForPromises();
  };

  const searchUserProjectsAndGroupsHandlerSuccess = jest
    .fn()
    .mockResolvedValue(searchUserProjectsAndGroupsResponseMock);

  const createWrapper = ({ props = {}, requestHandlers = {} } = {}) => {
    mockApollo = createMockApollo([
      [
        searchUserProjectsAndGroupsQuery,
        requestHandlers.searchUserProjectsAndGroupsQueryHandler ??
          searchUserProjectsAndGroupsHandlerSuccess,
      ],
    ]);

    wrapper = shallowMountExtended(ContextSwitcher, {
      apolloProvider: mockApollo,
      propsData: {
        username,
        projectsPath,
        groupsPath,
        ...props,
      },
      stubs: {
        GlSearchBoxByType: stubComponent(GlSearchBoxByType, {
          props: ['placeholder'],
        }),
        ProjectsList: stubComponent(ProjectsList, {
          props: ['username', 'viewAllLink', 'isSearch', 'searchResults'],
        }),
        GroupsList: stubComponent(GroupsList, {
          props: ['username', 'viewAllLink', 'isSearch', 'searchResults'],
        }),
      },
    });
  };

  describe('default', () => {
    beforeEach(() => {
      createWrapper();
    });

    it('passes the placeholder to the search box', () => {
      expect(findSearchBox().props('placeholder')).toBe(
        s__('Navigation|Search for projects or groups'),
      );
    });

    it('passes the correct props the frequent projects list', () => {
      expect(findProjectsList().props()).toEqual({
        username,
        viewAllLink: projectsPath,
        isSearch: false,
        searchResults: [],
      });
    });

    it('passes the correct props the frequent groups list', () => {
      expect(findGroupsList().props()).toEqual({
        username,
        viewAllLink: groupsPath,
        isSearch: false,
        searchResults: [],
      });
    });

    it('does not trigger the search query on mount', () => {
      expect(searchUserProjectsAndGroupsHandlerSuccess).not.toHaveBeenCalled();
    });
  });

  describe('item access tracking', () => {
    it('does not track anything if not within a trackable context', () => {
      createWrapper();

      expect(trackContextAccess).not.toHaveBeenCalled();
    });

    it('tracks item access if within a trackable context', () => {
      const currentContext = { namespace: 'groups' };
      createWrapper({
        props: {
          currentContext,
        },
      });

      expect(trackContextAccess).toHaveBeenCalledWith(username, currentContext);
    });
  });

  describe('on search', () => {
    beforeEach(() => {
      createWrapper();
      return triggerSearchQuery();
    });

    it('triggers the search query on search', () => {
      expect(searchUserProjectsAndGroupsHandlerSuccess).toHaveBeenCalled();
    });

    it('passes the projects to the frequent projects list', () => {
      expect(findProjectsList().props('isSearch')).toBe(true);
      expect(findProjectsList().props('searchResults')).toEqual(
        formatContextSwitcherItems(searchUserProjectsAndGroupsResponseMock.data.projects.nodes),
      );
    });

    it('passes the groups to the frequent groups list', () => {
      expect(findGroupsList().props('isSearch')).toBe(true);
      expect(findGroupsList().props('searchResults')).toEqual(
        formatContextSwitcherItems(searchUserProjectsAndGroupsResponseMock.data.user.groups.nodes),
      );
    });
  });

  describe('when search query does not match any items', () => {
    beforeEach(() => {
      createWrapper({
        requestHandlers: {
          searchUserProjectsAndGroupsQueryHandler: jest.fn().mockResolvedValue({
            data: {
              projects: {
                nodes: [],
              },
              user: {
                id: '1',
                groups: {
                  nodes: [],
                },
              },
            },
          }),
        },
      });
      return triggerSearchQuery();
    });

    it('passes empty results to the lists', () => {
      expect(findProjectsList().props('isSearch')).toBe(true);
      expect(findProjectsList().props('searchResults')).toEqual([]);
      expect(findGroupsList().props('isSearch')).toBe(true);
      expect(findGroupsList().props('searchResults')).toEqual([]);
    });
  });

  describe('when search query fails', () => {
    beforeEach(() => {
      jest.spyOn(Sentry, 'captureException');
    });

    it('captures exception if response is formatted incorrectly', async () => {
      createWrapper({
        requestHandlers: {
          searchUserProjectsAndGroupsQueryHandler: jest.fn().mockResolvedValue({
            data: {},
          }),
        },
      });
      await triggerSearchQuery();

      expect(Sentry.captureException).toHaveBeenCalled();
    });

    it('captures exception if query fails', async () => {
      createWrapper({
        requestHandlers: {
          searchUserProjectsAndGroupsQueryHandler: jest.fn().mockRejectedValue(),
        },
      });
      await triggerSearchQuery();

      expect(Sentry.captureException).toHaveBeenCalled();
    });
  });
});