summaryrefslogtreecommitdiff
path: root/spec/frontend/snippets/components/show_spec.js
blob: e6162c6aad2c8c30d6f2504604f99ccf31b59222 (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
import { GlLoadingIcon } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { Blob, BinaryBlob } from 'jest/blob/components/mock_data';
import EmbedDropdown from '~/snippets/components/embed_dropdown.vue';
import SnippetApp from '~/snippets/components/show.vue';
import SnippetBlob from '~/snippets/components/snippet_blob_view.vue';
import SnippetHeader from '~/snippets/components/snippet_header.vue';
import SnippetTitle from '~/snippets/components/snippet_title.vue';
import {
  SNIPPET_VISIBILITY_INTERNAL,
  SNIPPET_VISIBILITY_PRIVATE,
  SNIPPET_VISIBILITY_PUBLIC,
} from '~/snippets/constants';
import CloneDropdownButton from '~/vue_shared/components/clone_dropdown.vue';

describe('Snippet view app', () => {
  let wrapper;
  const defaultProps = {
    snippetGid: 'gid://gitlab/PersonalSnippet/42',
  };
  const webUrl = 'http://foo.bar';
  const dummyHTTPUrl = webUrl;
  const dummySSHUrl = 'ssh://foo.bar';

  function createComponent({ props = defaultProps, data = {}, loading = false } = {}) {
    const $apollo = {
      queries: {
        snippet: {
          loading,
        },
      },
    };

    wrapper = shallowMount(SnippetApp, {
      mocks: { $apollo },
      propsData: {
        ...props,
      },
      data() {
        return data;
      },
    });
  }
  afterEach(() => {
    wrapper.destroy();
  });

  it('renders loader while the query is in flight', () => {
    createComponent({ loading: true });
    expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);
  });

  it('renders all simple components after the query is finished', () => {
    createComponent();
    expect(wrapper.find(SnippetHeader).exists()).toBe(true);
    expect(wrapper.find(SnippetTitle).exists()).toBe(true);
  });

  it('renders embed dropdown component if visibility allows', () => {
    createComponent({
      data: {
        snippet: {
          visibilityLevel: SNIPPET_VISIBILITY_PUBLIC,
          webUrl: 'http://foo.bar',
        },
      },
    });
    expect(wrapper.find(EmbedDropdown).exists()).toBe(true);
  });

  it('renders correct snippet-blob components', () => {
    createComponent({
      data: {
        blobs: [Blob, BinaryBlob],
      },
    });
    const blobs = wrapper.findAll(SnippetBlob);
    expect(blobs.length).toBe(2);
    expect(blobs.at(0).props('blob')).toEqual(Blob);
    expect(blobs.at(1).props('blob')).toEqual(BinaryBlob);
  });

  describe('Embed dropdown rendering', () => {
    it.each`
      visibilityLevel                | condition       | isRendered
      ${SNIPPET_VISIBILITY_INTERNAL} | ${'not render'} | ${false}
      ${SNIPPET_VISIBILITY_PRIVATE}  | ${'not render'} | ${false}
      ${'foo'}                       | ${'not render'} | ${false}
      ${SNIPPET_VISIBILITY_PUBLIC}   | ${'render'}     | ${true}
    `('does $condition embed-dropdown by default', ({ visibilityLevel, isRendered }) => {
      createComponent({
        data: {
          snippet: {
            visibilityLevel,
            webUrl,
          },
        },
      });
      expect(wrapper.find(EmbedDropdown).exists()).toBe(isRendered);
    });
  });

  describe('Clone button rendering', () => {
    it.each`
      httpUrlToRepo   | sshUrlToRepo   | shouldRender    | isRendered
      ${null}         | ${null}        | ${'Should not'} | ${false}
      ${null}         | ${dummySSHUrl} | ${'Should'}     | ${true}
      ${dummyHTTPUrl} | ${null}        | ${'Should'}     | ${true}
      ${dummyHTTPUrl} | ${dummySSHUrl} | ${'Should'}     | ${true}
    `(
      '$shouldRender render "Clone" button when `httpUrlToRepo` is $httpUrlToRepo and `sshUrlToRepo` is $sshUrlToRepo',
      ({ httpUrlToRepo, sshUrlToRepo, isRendered }) => {
        createComponent({
          data: {
            snippet: {
              sshUrlToRepo,
              httpUrlToRepo,
            },
          },
        });
        expect(wrapper.find(CloneDropdownButton).exists()).toBe(isRendered);
      },
    );
  });
});