summaryrefslogtreecommitdiff
path: root/spec/frontend/blob/components/blob_header_filepath_spec.js
blob: 8c32cba1ba407d78012493642584f42b9e437c82 (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
import { GlBadge } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import BlobHeaderFilepath from '~/blob/components/blob_header_filepath.vue';
import { numberToHumanSize } from '~/lib/utils/number_utils';
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
import { Blob as MockBlob } from './mock_data';

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

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

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

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

  const findBadge = () => wrapper.findComponent(GlBadge);

  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.findComponent(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('a lot');
    });

    it('renders LFS badge if LFS if enabled', () => {
      createComponent({ storedExternally: true, externalStorage: 'lfs' });
      expect(findBadge().text()).toBe('LFS');
    });

    it('renders a slot and prepends its contents to the existing one', () => {
      const slotContent = 'Foo Bar';
      createComponent(
        {},
        {
          scopedSlots: {
            'filepath-prepend': `<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}\``);
    });
  });
});