summaryrefslogtreecommitdiff
path: root/spec/frontend/diffs/components/diff_file_row_spec.js
blob: 1d1c5fec293a7e5be39c75dcfdba50cc2da5ad18 (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
import { shallowMount } from '@vue/test-utils';
import DiffFileRow from '~/diffs/components/diff_file_row.vue';
import FileRowStats from '~/diffs/components/file_row_stats.vue';
import ChangedFileIcon from '~/vue_shared/components/changed_file_icon.vue';
import FileRow from '~/vue_shared/components/file_row.vue';

describe('Diff File Row component', () => {
  let wrapper;

  const createComponent = (props = {}) => {
    wrapper = shallowMount(DiffFileRow, {
      propsData: { ...props },
    });
  };

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

  it('renders file row component', () => {
    const sharedProps = {
      level: 4,
      file: {},
    };

    const diffFileRowProps = {
      hideFileStats: false,
    };

    createComponent({
      ...sharedProps,
      ...diffFileRowProps,
    });

    expect(wrapper.find(FileRow).props()).toEqual(
      expect.objectContaining({
        ...sharedProps,
      }),
    );
  });

  it('renders ChangedFileIcon component', () => {
    createComponent({
      level: 4,
      file: {},
      hideFileStats: false,
      showTooltip: true,
    });

    expect(wrapper.find(ChangedFileIcon).props()).toEqual(
      expect.objectContaining({
        file: {},
        size: 16,
        showTooltip: true,
      }),
    );
  });

  it.each`
    fileType  | isViewed | expected
    ${'blob'} | ${false} | ${'gl-font-weight-bold'}
    ${'blob'} | ${true}  | ${''}
    ${'tree'} | ${false} | ${''}
    ${'tree'} | ${true}  | ${''}
  `(
    'with (fileType="$fileType", isViewed=$isViewed), sets fileClasses="$expected"',
    ({ fileType, isViewed, expected }) => {
      createComponent({
        file: {
          type: fileType,
          fileHash: '#123456789',
        },
        level: 0,
        hideFileStats: false,
        viewedFiles: isViewed ? { '#123456789': true } : {},
      });
      expect(wrapper.find(FileRow).props('fileClasses')).toBe(expected);
    },
  );

  describe('FileRowStats components', () => {
    it.each`
      type      | hideFileStats | value    | desc
      ${'blob'} | ${false}      | ${true}  | ${'is shown if file type is blob'}
      ${'tree'} | ${false}      | ${false} | ${'is hidden if file is not blob'}
      ${'blob'} | ${true}       | ${false} | ${'is hidden if hideFileStats is true'}
    `('$desc', ({ type, value, hideFileStats }) => {
      createComponent({
        level: 4,
        file: {
          type,
        },
        hideFileStats,
      });
      expect(wrapper.find(FileRowStats).exists()).toEqual(value);
    });
  });

  it('adds is-active class when currentDiffFileId matches file_hash', () => {
    createComponent({
      level: 0,
      currentDiffFileId: '123',
      file: { fileHash: '123' },
      hideFileStats: false,
    });

    expect(wrapper.classes('is-active')).toBe(true);
  });
});