summaryrefslogtreecommitdiff
path: root/spec/frontend/ide/components/repo_tab_spec.js
blob: 95d52e8f7a98490090b8fb67b140eb81c647a170 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { GlTab } from '@gitlab/ui';
import { mount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import { stubComponent } from 'helpers/stub_component';
import RepoTab from '~/ide/components/repo_tab.vue';
import { createRouter } from '~/ide/ide_router';
import { createStore } from '~/ide/stores';
import { file } from '../helpers';

const localVue = createLocalVue();
localVue.use(Vuex);

const GlTabStub = stubComponent(GlTab, {
  template: '<li><slot name="title" /></li>',
});

describe('RepoTab', () => {
  let wrapper;
  let store;
  let router;

  const findTab = () => wrapper.find(GlTabStub);

  function createComponent(propsData) {
    wrapper = mount(RepoTab, {
      localVue,
      store,
      propsData,
      stubs: {
        GlTab: GlTabStub,
      },
    });
  }

  beforeEach(() => {
    store = createStore();
    router = createRouter(store);
    jest.spyOn(router, 'push').mockImplementation(() => {});
  });

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

  it('renders a close link and a name link', () => {
    createComponent({
      tab: file(),
    });
    wrapper.vm.$store.state.openFiles.push(wrapper.vm.tab);
    const close = wrapper.find('.multi-file-tab-close');
    const name = wrapper.find(`[title]`);

    expect(close.html()).toContain('#close');
    expect(name.text().trim()).toEqual(wrapper.vm.tab.name);
  });

  it('does not call openPendingTab when tab is active', async () => {
    createComponent({
      tab: {
        ...file(),
        pending: true,
        active: true,
      },
    });

    jest.spyOn(wrapper.vm, 'openPendingTab').mockImplementation(() => {});

    await findTab().vm.$emit('click');

    expect(wrapper.vm.openPendingTab).not.toHaveBeenCalled();
  });

  it('fires clickFile when the link is clicked', () => {
    createComponent({
      tab: file(),
    });

    jest.spyOn(wrapper.vm, 'clickFile').mockImplementation(() => {});

    findTab().vm.$emit('click');

    expect(wrapper.vm.clickFile).toHaveBeenCalledWith(wrapper.vm.tab);
  });

  it('calls closeFile when clicking close button', () => {
    createComponent({
      tab: file(),
    });

    jest.spyOn(wrapper.vm, 'closeFile').mockImplementation(() => {});

    wrapper.find('.multi-file-tab-close').trigger('click');

    expect(wrapper.vm.closeFile).toHaveBeenCalledWith(wrapper.vm.tab);
  });

  it('changes icon on hover', async () => {
    const tab = file();
    tab.changed = true;
    createComponent({
      tab,
    });

    await findTab().vm.$emit('mouseover');

    expect(wrapper.find('.file-modified').exists()).toBe(false);

    await findTab().vm.$emit('mouseout');

    expect(wrapper.find('.file-modified').exists()).toBe(true);
  });

  it.each`
    tabProps             | closeLabel
    ${{}}                | ${'Close foo.txt'}
    ${{ changed: true }} | ${'foo.txt changed'}
  `('close button has label ($closeLabel) with tab ($tabProps)', ({ tabProps, closeLabel }) => {
    const tab = { ...file('foo.txt'), ...tabProps };

    createComponent({ tab });

    expect(wrapper.find('button').attributes('aria-label')).toBe(closeLabel);
  });

  describe('locked file', () => {
    let f;

    beforeEach(() => {
      f = file('locked file');
      f.file_lock = {
        user: {
          name: 'testuser',
          updated_at: new Date(),
        },
      };

      createComponent({
        tab: f,
      });
    });

    it('renders lock icon', () => {
      expect(wrapper.find('.file-status-icon')).not.toBeNull();
    });

    it('renders a tooltip', () => {
      expect(wrapper.find('span:nth-child(2)').attributes('title')).toBe('Locked by testuser');
    });
  });

  describe('methods', () => {
    describe('closeTab', () => {
      it('closes tab if file has changed', async () => {
        const tab = file();
        tab.changed = true;
        tab.opened = true;
        createComponent({
          tab,
        });
        wrapper.vm.$store.state.openFiles.push(tab);
        wrapper.vm.$store.state.changedFiles.push(tab);
        wrapper.vm.$store.state.entries[tab.path] = tab;
        wrapper.vm.$store.dispatch('setFileActive', tab.path);

        await wrapper.find('.multi-file-tab-close').trigger('click');

        expect(tab.opened).toBeFalsy();
        expect(wrapper.vm.$store.state.changedFiles).toHaveLength(1);
      });

      it('closes tab when clicking close btn', async () => {
        const tab = file('lose');
        tab.opened = true;
        createComponent({
          tab,
        });
        wrapper.vm.$store.state.openFiles.push(tab);
        wrapper.vm.$store.state.entries[tab.path] = tab;
        wrapper.vm.$store.dispatch('setFileActive', tab.path);

        await wrapper.find('.multi-file-tab-close').trigger('click');

        expect(tab.opened).toBeFalsy();
      });
    });
  });
});