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

snippets_tab_spec.js « snippets « components « profile « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5992bb03e4d2c694baa313acf5a645f68157d5d7 (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
import { GlEmptyState, GlKeysetPagination } from '@gitlab/ui';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import { convertToGraphQLId } from '~/graphql_shared/utils';
import { TYPENAME_USER } from '~/graphql_shared/constants';
import { SNIPPET_MAX_LIST_COUNT } from '~/profile/constants';
import SnippetsTab from '~/profile/components/snippets/snippets_tab.vue';
import SnippetRow from '~/profile/components/snippets/snippet_row.vue';
import getUserSnippets from '~/profile/components/graphql/get_user_snippets.query.graphql';
import { isCurrentUser } from '~/lib/utils/common_utils';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import {
  MOCK_USER,
  MOCK_SNIPPETS_EMPTY_STATE,
  MOCK_USER_SNIPPETS_RES,
  MOCK_USER_SNIPPETS_PAGINATION_RES,
  MOCK_USER_SNIPPETS_EMPTY_RES,
  MOCK_NEW_SNIPPET_PATH,
} from 'jest/profile/mock_data';

jest.mock('~/lib/utils/common_utils');
jest.mock('~/helpers/help_page_helper', () => ({
  helpPagePath: jest.fn().mockImplementation(() => 'http://127.0.0.1:3000/help/user/snippets'),
}));

Vue.use(VueApollo);

describe('UserProfileSnippetsTab', () => {
  let wrapper;

  let queryHandlerMock = jest.fn().mockResolvedValue(MOCK_USER_SNIPPETS_RES);

  const createComponent = () => {
    const apolloProvider = createMockApollo([[getUserSnippets, queryHandlerMock]]);

    wrapper = shallowMountExtended(SnippetsTab, {
      apolloProvider,
      provide: {
        userId: MOCK_USER.id,
        snippetsEmptyState: MOCK_SNIPPETS_EMPTY_STATE,
        newSnippetPath: MOCK_NEW_SNIPPET_PATH,
      },
    });
  };

  const findSnippetRows = () => wrapper.findAllComponents(SnippetRow);
  const findGlEmptyState = () => wrapper.findComponent(GlEmptyState);
  const findGlKeysetPagination = () => wrapper.findComponent(GlKeysetPagination);

  describe('when user has no snippets', () => {
    beforeEach(async () => {
      queryHandlerMock = jest.fn().mockResolvedValue(MOCK_USER_SNIPPETS_EMPTY_RES);
      createComponent();

      await nextTick();
    });

    it('does not render snippet row', () => {
      expect(findSnippetRows().exists()).toBe(false);
    });

    describe('when user is the current user', () => {
      beforeEach(() => {
        isCurrentUser.mockImplementation(() => true);
        createComponent();
      });

      it('displays empty state with correct message', () => {
        expect(findGlEmptyState().props()).toMatchObject({
          svgPath: MOCK_SNIPPETS_EMPTY_STATE,
          title: SnippetsTab.i18n.currentUserEmptyStateTitle,
          description: SnippetsTab.i18n.emptyStateDescription,
          primaryButtonLink: MOCK_NEW_SNIPPET_PATH,
          primaryButtonText: SnippetsTab.i18n.newSnippet,
          secondaryButtonLink: 'http://127.0.0.1:3000/help/user/snippets',
          secondaryButtonText: SnippetsTab.i18n.learnMore,
        });
      });
    });

    describe('when user is a visitor', () => {
      beforeEach(() => {
        isCurrentUser.mockImplementation(() => false);
        createComponent();
      });

      it('displays empty state with correct message', () => {
        expect(findGlEmptyState().props()).toMatchObject({
          svgPath: MOCK_SNIPPETS_EMPTY_STATE,
          title: SnippetsTab.i18n.visitorEmptyStateTitle,
          description: null,
        });
      });
    });
  });

  describe('when snippets returns an error', () => {
    beforeEach(async () => {
      queryHandlerMock = jest.fn().mockRejectedValue({ errors: [] });
      createComponent();

      await nextTick();
    });

    it('does not render snippet row', () => {
      expect(findSnippetRows().exists()).toBe(false);
    });

    it('does render empty state with correct svg', () => {
      expect(findGlEmptyState().exists()).toBe(true);
      expect(findGlEmptyState().attributes('svgpath')).toBe(MOCK_SNIPPETS_EMPTY_STATE);
    });
  });

  describe('when snippets are returned', () => {
    beforeEach(async () => {
      queryHandlerMock = jest.fn().mockResolvedValue(MOCK_USER_SNIPPETS_RES);
      createComponent();

      await nextTick();
    });

    it('renders a snippet row for each snippet', () => {
      expect(findSnippetRows().exists()).toBe(true);
      expect(findSnippetRows().length).toBe(MOCK_USER_SNIPPETS_RES.data.user.snippets.nodes.length);
    });

    it('does not render empty state', () => {
      expect(findGlEmptyState().exists()).toBe(false);
    });

    it('adds bottom border when snippet is not last in list', () => {
      expect(findSnippetRows().at(0).classes('gl-border-b')).toBe(true);
    });

    it('does not add bottom border when snippet is last in list', () => {
      expect(
        findSnippetRows()
          .at(MOCK_USER_SNIPPETS_RES.data.user.snippets.nodes.length - 1)
          .classes('gl-border-b'),
      ).toBe(false);
    });
  });

  describe('Snippet Pagination', () => {
    describe('when user has one page of snippets', () => {
      beforeEach(async () => {
        queryHandlerMock = jest.fn().mockResolvedValue(MOCK_USER_SNIPPETS_RES);
        createComponent();

        await nextTick();
      });

      it('does not render pagination', () => {
        expect(findGlKeysetPagination().exists()).toBe(false);
      });
    });

    describe('when user has multiple pages of snippets', () => {
      beforeEach(async () => {
        queryHandlerMock = jest.fn().mockResolvedValue(MOCK_USER_SNIPPETS_PAGINATION_RES);
        createComponent();

        await nextTick();
      });

      it('does render pagination', () => {
        expect(findGlKeysetPagination().exists()).toBe(true);
      });

      it('when nextPage is clicked', async () => {
        findGlKeysetPagination().vm.$emit('next');

        await nextTick();

        expect(queryHandlerMock).toHaveBeenCalledWith({
          id: convertToGraphQLId(TYPENAME_USER, MOCK_USER.id),
          first: SNIPPET_MAX_LIST_COUNT,
          last: null,
          afterToken: MOCK_USER_SNIPPETS_RES.data.user.snippets.pageInfo.endCursor,
        });
      });

      it('when previousPage is clicked', async () => {
        findGlKeysetPagination().vm.$emit('prev');

        await nextTick();

        expect(queryHandlerMock).toHaveBeenCalledWith({
          id: convertToGraphQLId(TYPENAME_USER, MOCK_USER.id),
          first: null,
          last: SNIPPET_MAX_LIST_COUNT,
          beforeToken: MOCK_USER_SNIPPETS_RES.data.user.snippets.pageInfo.startCursor,
        });
      });
    });
  });
});