summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/sidebar/copyable_field_spec.js
blob: b99b1a66b79f85cf2d0c934a9d2ebceae05ad0b2 (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
import { GlLoadingIcon } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
import CopyableField from '~/vue_shared/components/sidebar/copyable_field.vue';

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

  const defaultProps = {
    value: 'Gl-1',
    name: 'Reference',
  };

  const createComponent = (propsData = defaultProps) => {
    wrapper = shallowMount(CopyableField, {
      propsData,
    });
  };

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

  const findClipboardButton = () => wrapper.findComponent(ClipboardButton);
  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);

  describe('template', () => {
    describe('when `isLoading` prop is `false`', () => {
      beforeEach(() => {
        createComponent();
      });

      it('renders copyable field', () => {
        expect(wrapper.text()).toContain('Reference: Gl-1');
      });

      it('renders ClipboardButton with correct props', () => {
        const clipboardButton = findClipboardButton();

        expect(clipboardButton.exists()).toBe(true);
        expect(clipboardButton.props('title')).toBe(`Copy ${defaultProps.name}`);
        expect(clipboardButton.props('text')).toBe(defaultProps.value);
      });

      it('does not render loading icon', () => {
        expect(findLoadingIcon().exists()).toBe(false);
      });
    });

    describe('when `isLoading` prop is `true`', () => {
      beforeEach(() => {
        createComponent({ ...defaultProps, isLoading: true });
      });

      it('renders loading icon', () => {
        expect(findLoadingIcon().exists()).toBe(true);
        expect(findLoadingIcon().props('label')).toBe('Loading Reference');
      });

      it('does not render clipboard button', () => {
        expect(findClipboardButton().exists()).toBe(false);
      });
    });

    describe('with `clipboardTooltipText` prop', () => {
      it('sets ClipboardButton `title` prop to `clipboardTooltipText` value', () => {
        const mockClipboardTooltipText = 'Copy my custom value';
        createComponent({ ...defaultProps, clipboardTooltipText: mockClipboardTooltipText });

        expect(findClipboardButton().props('title')).toBe(mockClipboardTooltipText);
      });
    });
  });
});