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

blob_header_filepath_spec.js « components « blob « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3a53208f3575e53edf9dcd494f95f19b8120e47f (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
import { shallowMount } from '@vue/test-utils';
import BlobHeaderFilepath from '~/blob/components/blob_header_filepath.vue';
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
import { Blob as MockBlob } from './mock_data';
import { numberToHumanSize } from '~/lib/utils/number_utils';

const mockHumanReadableSize = 'a lot';
jest.mock('~/lib/utils/number_utils', () => ({
  numberToHumanSize: jest.fn(() => mockHumanReadableSize),
}));

describe('Blob Header Filepath', () => {
  let wrapper;

  function createComponent(blobProps = {}, options = {}) {
    wrapper = shallowMount(BlobHeaderFilepath, {
      propsData: {
        blob: { ...MockBlob, ...blobProps },
      },
      ...options,
    });
  }

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

  describe('rendering', () => {
    it('matches the snapshot', () => {
      createComponent();
      expect(wrapper.element).toMatchSnapshot();
    });

    it('renders regular name', () => {
      createComponent();
      expect(
        wrapper
          .find('.js-blob-header-filepath')
          .text()
          .trim(),
      ).toBe(MockBlob.path);
    });

    it('does not fail if the name is empty', () => {
      const emptyPath = '';
      createComponent({ path: emptyPath });
      expect(wrapper.find('.js-blob-header-filepath').exists()).toBe(false);
    });

    it('renders copy-to-clipboard icon that copies path of the Blob', () => {
      createComponent();
      const btn = wrapper.find(ClipboardButton);
      expect(btn.exists()).toBe(true);
      expect(btn.vm.text).toBe(MockBlob.path);
    });

    it('renders filesize in a human-friendly format', () => {
      createComponent();
      expect(numberToHumanSize).toHaveBeenCalled();
      expect(wrapper.vm.blobSize).toBe(mockHumanReadableSize);
    });

    it('renders a slot and prepends its contents to the existing one', () => {
      const slotContent = 'Foo Bar';
      createComponent(
        {},
        {
          scopedSlots: {
            filepathPrepend: `<span>${slotContent}</span>`,
          },
        },
      );

      expect(wrapper.text()).toContain(slotContent);
      expect(
        wrapper
          .text()
          .trim()
          .substring(0, slotContent.length),
      ).toBe(slotContent);
    });
  });

  describe('functionality', () => {
    it('sets gfm value correctly on the clipboard-button', () => {
      createComponent();
      expect(wrapper.vm.gfmCopyText).toBe(`\`${MockBlob.path}\``);
    });
  });
});