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

snippet_blob_view_spec.js « components « snippets « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05ff64c22960401a997de789f371e81fd03d2b75 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { shallowMount } from '@vue/test-utils';
import {
  Blob as BlobMock,
  SimpleViewerMock,
  RichViewerMock,
  RichBlobContentMock,
  SimpleBlobContentMock,
} from 'jest/blob/components/mock_data';
import GetBlobContent from 'shared_queries/snippet/snippet_blob_content.query.graphql';
import BlobContent from '~/blob/components/blob_content.vue';
import BlobHeader from '~/blob/components/blob_header.vue';
import {
  BLOB_RENDER_EVENT_LOAD,
  BLOB_RENDER_EVENT_SHOW_SOURCE,
  BLOB_RENDER_ERRORS,
} from '~/blob/components/constants';
import SnippetBlobView from '~/snippets/components/snippet_blob_view.vue';
import { VISIBILITY_LEVEL_PUBLIC_STRING } from '~/visibility_level/constants';
import { RichViewer, SimpleViewer } from '~/vue_shared/components/blob_viewers';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';

describe('Blob Embeddable', () => {
  let wrapper;
  let requestHandlers;

  const snippet = {
    id: 'gid://foo.bar/snippet',
    webUrl: 'https://foo.bar',
    visibilityLevel: VISIBILITY_LEVEL_PUBLIC_STRING,
  };
  const dataMock = {
    activeViewerType: SimpleViewerMock.type,
  };

  const mockDefaultHandler = ({ path, nodes } = { path: BlobMock.path }) => {
    const renderedNodes = nodes || [
      { __typename: 'Blob', path, richData: 'richData', plainData: 'plainData' },
    ];

    return jest.fn().mockResolvedValue({
      data: {
        snippets: {
          __typename: 'Snippet',
          id: '1',
          nodes: [
            {
              __typename: 'Snippet',
              id: '2',
              blobs: {
                __typename: 'Blob',
                hasUnretrievableBlobs: false,
                nodes: renderedNodes,
              },
            },
          ],
        },
      },
    });
  };

  const createMockApolloProvider = (handler) => {
    Vue.use(VueApollo);

    requestHandlers = handler;
    return createMockApollo([[GetBlobContent, requestHandlers]]);
  };

  function createComponent({
    snippetProps = {},
    data = dataMock,
    blob = BlobMock,
    handler = mockDefaultHandler(),
  } = {}) {
    wrapper = shallowMount(SnippetBlobView, {
      apolloProvider: createMockApolloProvider(handler),
      propsData: {
        snippet: {
          ...snippet,
          ...snippetProps,
        },
        blob,
      },
      data() {
        return {
          ...data,
        };
      },
      stubs: {
        BlobHeader,
        BlobContent,
      },
    });
  }

  const findBlobHeader = () => wrapper.findComponent(BlobHeader);
  const findBlobContent = () => wrapper.findComponent(BlobContent);
  const findSimpleViewer = () => wrapper.findComponent(SimpleViewer);
  const findRichViewer = () => wrapper.findComponent(RichViewer);

  describe('rendering', () => {
    it('renders correct components', () => {
      createComponent();
      expect(findBlobHeader().exists()).toBe(true);
      expect(findBlobContent().exists()).toBe(true);
    });

    it('sets simple viewer correctly', async () => {
      createComponent();
      await waitForPromises();

      expect(findSimpleViewer().exists()).toBe(true);
    });

    it('sets rich viewer correctly', async () => {
      const data = { ...dataMock, activeViewerType: RichViewerMock.type };
      createComponent({
        data,
      });
      await waitForPromises();
      expect(findRichViewer().exists()).toBe(true);
    });

    it('correctly switches viewer type', async () => {
      createComponent();
      await waitForPromises();

      expect(findSimpleViewer().exists()).toBe(true);

      findBlobContent().vm.$emit(BLOB_RENDER_EVENT_SHOW_SOURCE, RichViewerMock.type);
      await waitForPromises();

      expect(findRichViewer().exists()).toBe(true);

      findBlobContent().vm.$emit(BLOB_RENDER_EVENT_SHOW_SOURCE, SimpleViewerMock.type);
      await waitForPromises();

      expect(findSimpleViewer().exists()).toBe(true);
    });

    it('passes information about render error down to blob header', () => {
      createComponent({
        blob: {
          ...BlobMock,
          simpleViewer: {
            ...SimpleViewerMock,
            renderError: BLOB_RENDER_ERRORS.REASONS.COLLAPSED.id,
          },
        },
      });

      expect(findBlobHeader().props('hasRenderError')).toBe(true);
    });

    describe('bob content in multi-file scenario', () => {
      const SimpleBlobContentMock2 = {
        ...SimpleBlobContentMock,
        plainData: 'Another Plain Foo',
      };
      const RichBlobContentMock2 = {
        ...SimpleBlobContentMock,
        richData: 'Another Rich Foo',
      };

      const MixedSimpleBlobContentMock = {
        ...SimpleBlobContentMock,
        richData: '<h1>Rich</h1>',
      };

      const MixedRichBlobContentMock = {
        ...RichBlobContentMock,
        plainData: 'Plain',
      };

      it.each`
        snippetBlobs                                         | description                                  | currentBlob              | expectedContent                    | activeViewerType
        ${[SimpleBlobContentMock]}                           | ${'one existing textual blob'}               | ${SimpleBlobContentMock} | ${SimpleBlobContentMock.plainData} | ${SimpleViewerMock.type}
        ${[RichBlobContentMock]}                             | ${'one existing rich blob'}                  | ${RichBlobContentMock}   | ${RichBlobContentMock.richData}    | ${RichViewerMock.type}
        ${[SimpleBlobContentMock, MixedRichBlobContentMock]} | ${'mixed blobs with current textual blob'}   | ${SimpleBlobContentMock} | ${SimpleBlobContentMock.plainData} | ${SimpleViewerMock.type}
        ${[MixedSimpleBlobContentMock, RichBlobContentMock]} | ${'mixed blobs with current rich blob'}      | ${RichBlobContentMock}   | ${RichBlobContentMock.richData}    | ${RichViewerMock.type}
        ${[SimpleBlobContentMock, SimpleBlobContentMock2]}   | ${'textual blobs with current textual blob'} | ${SimpleBlobContentMock} | ${SimpleBlobContentMock.plainData} | ${SimpleViewerMock.type}
        ${[RichBlobContentMock, RichBlobContentMock2]}       | ${'rich blobs with current rich blob'}       | ${RichBlobContentMock}   | ${RichBlobContentMock.richData}    | ${RichViewerMock.type}
      `(
        'renders correct content for $description',
        async ({ snippetBlobs, currentBlob, expectedContent, activeViewerType }) => {
          createComponent({
            handler: mockDefaultHandler({ path: currentBlob.path, nodes: snippetBlobs }),
            data: { activeViewerType },
            blob: {
              ...BlobMock,
              path: currentBlob.path,
            },
          });
          await waitForPromises();

          expect(findBlobContent().props('content')).toBe(expectedContent);
        },
      );
    });

    describe('URLS with hash', () => {
      afterEach(() => {
        window.location.hash = '';
      });

      describe('if hash starts with #LC', () => {
        beforeEach(() => {
          window.location.hash = '#LC2';
        });

        it('renders simple viewer by default', async () => {
          createComponent({
            data: {},
          });
          await waitForPromises();

          expect(findBlobHeader().props('activeViewerType')).toBe(SimpleViewerMock.type);
          expect(findSimpleViewer().exists()).toBe(true);
        });

        describe('switchViewer()', () => {
          it('switches to the passed viewer', async () => {
            createComponent();
            await waitForPromises();

            findBlobContent().vm.$emit(BLOB_RENDER_EVENT_SHOW_SOURCE, RichViewerMock.type);
            await waitForPromises();

            expect(findBlobHeader().props('activeViewerType')).toBe(RichViewerMock.type);
            expect(findRichViewer().exists()).toBe(true);

            findBlobContent().vm.$emit(BLOB_RENDER_EVENT_SHOW_SOURCE, SimpleViewerMock.type);
            await waitForPromises();

            expect(findBlobHeader().props('activeViewerType')).toBe(SimpleViewerMock.type);
            expect(findSimpleViewer().exists()).toBe(true);
          });
        });
      });

      describe('if hash starts with anything else', () => {
        beforeEach(() => {
          window.location.hash = '#last-headline';
        });

        it('renders rich viewer by default', async () => {
          createComponent({
            data: {},
          });
          await waitForPromises();

          expect(findBlobHeader().props('activeViewerType')).toBe(RichViewerMock.type);
          expect(findRichViewer().exists()).toBe(true);
        });

        describe('switchViewer()', () => {
          it('switches to the passed viewer', async () => {
            createComponent();
            await waitForPromises();

            findBlobContent().vm.$emit(BLOB_RENDER_EVENT_SHOW_SOURCE, SimpleViewerMock.type);
            await waitForPromises();

            expect(findBlobHeader().props('activeViewerType')).toBe(SimpleViewerMock.type);
            expect(findSimpleViewer().exists()).toBe(true);

            findBlobContent().vm.$emit(BLOB_RENDER_EVENT_SHOW_SOURCE, RichViewerMock.type);
            await waitForPromises();

            expect(findBlobHeader().props('activeViewerType')).toBe(RichViewerMock.type);
            expect(findRichViewer().exists()).toBe(true);
          });
        });
      });
    });
  });

  describe('functionality', () => {
    describe('render error', () => {
      it('correctly sets blob on the blob-content-error component', () => {
        createComponent();
        expect(findBlobContent().props('blob')).toEqual(BlobMock);
      });

      it(`refetches blob content on ${BLOB_RENDER_EVENT_LOAD} event`, async () => {
        createComponent();
        await waitForPromises();

        expect(requestHandlers).toHaveBeenCalledTimes(1);

        findBlobContent().vm.$emit(BLOB_RENDER_EVENT_LOAD);
        await waitForPromises();

        expect(requestHandlers).toHaveBeenCalledTimes(2);
      });

      it(`sets '${SimpleViewerMock.type}' as active on ${BLOB_RENDER_EVENT_SHOW_SOURCE} event`, () => {
        createComponent({
          data: {
            activeViewerType: RichViewerMock.type,
          },
        });

        findBlobContent().vm.$emit(BLOB_RENDER_EVENT_SHOW_SOURCE);
        expect(wrapper.vm.activeViewerType).toEqual(SimpleViewerMock.type);
      });
    });
  });
});