summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/file_finder/item_spec.js
blob: b69c33055c1da5e66e0ca83edf37c31377747950 (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
126
127
import Vue, { nextTick } from 'vue';
import createComponent from 'helpers/vue_mount_component_helper';
import { file } from 'jest/ide/helpers';
import ItemComponent from '~/vue_shared/components/file_finder/item.vue';

describe('File finder item spec', () => {
  const Component = Vue.extend(ItemComponent);
  let vm;
  let localFile;

  beforeEach(() => {
    localFile = {
      ...file(),
      name: 'test file',
      path: 'test/file',
    };

    vm = createComponent(Component, {
      file: localFile,
      focused: true,
      searchText: '',
      index: 0,
    });
  });

  afterEach(() => {
    vm.$destroy();
  });

  it('renders file name & path', () => {
    expect(vm.$el.textContent).toContain('test file');
    expect(vm.$el.textContent).toContain('test/file');
  });

  describe('focused', () => {
    it('adds is-focused class', () => {
      expect(vm.$el.classList).toContain('is-focused');
    });

    it('does not have is-focused class when not focused', async () => {
      vm.focused = false;

      await nextTick();
      expect(vm.$el.classList).not.toContain('is-focused');
    });
  });

  describe('changed file icon', () => {
    it('does not render when not a changed or temp file', () => {
      expect(vm.$el.querySelector('.diff-changed-stats')).toBe(null);
    });

    it('renders when a changed file', async () => {
      vm.file.changed = true;

      await nextTick();
      expect(vm.$el.querySelector('.diff-changed-stats')).not.toBe(null);
    });

    it('renders when a temp file', async () => {
      vm.file.tempFile = true;

      await nextTick();
      expect(vm.$el.querySelector('.diff-changed-stats')).not.toBe(null);
    });
  });

  it('emits event when clicked', () => {
    jest.spyOn(vm, '$emit').mockImplementation(() => {});

    vm.$el.click();

    expect(vm.$emit).toHaveBeenCalledWith('click', vm.file);
  });

  describe('path', () => {
    let el;

    beforeEach(async () => {
      vm.searchText = 'file';

      el = vm.$el.querySelector('.diff-changed-file-path');

      nextTick();
    });

    it('highlights text', () => {
      expect(el.querySelectorAll('.highlighted').length).toBe(4);
    });

    it('adds ellipsis to long text', async () => {
      vm.file.path = new Array(70)
        .fill()
        .map((_, i) => `${i}-`)
        .join('');

      await nextTick();
      expect(el.textContent).toBe(`...${vm.file.path.substr(vm.file.path.length - 60)}`);
    });
  });

  describe('name', () => {
    let el;

    beforeEach(async () => {
      vm.searchText = 'file';

      el = vm.$el.querySelector('.diff-changed-file-name');

      await nextTick();
    });

    it('highlights text', () => {
      expect(el.querySelectorAll('.highlighted').length).toBe(4);
    });

    it('does not add ellipsis to long text', async () => {
      vm.file.name = new Array(70)
        .fill()
        .map((_, i) => `${i}-`)
        .join('');

      await nextTick();
      expect(el.textContent).not.toBe(`...${vm.file.name.substr(vm.file.name.length - 60)}`);
    });
  });
});