summaryrefslogtreecommitdiff
path: root/spec/javascripts/repo/components/repo_sidebar_spec.js
blob: 148f275e03df0f993093eacb496ef93d8a83d19e (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
import Vue from 'vue';
import Helper from '~/repo/helpers/repo_helper';
import RepoService from '~/repo/services/repo_service';
import RepoStore from '~/repo/stores/repo_store';
import repoSidebar from '~/repo/components/repo_sidebar.vue';
import { file } from '../mock_data';

describe('RepoSidebar', () => {
  let vm;

  function createComponent() {
    const RepoSidebar = Vue.extend(repoSidebar);

    return new RepoSidebar().$mount();
  }

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

    RepoStore.files = [];
    RepoStore.openedFiles = [];
  });

  it('renders a sidebar', () => {
    RepoStore.files = [file()];
    RepoStore.openedFiles = [];
    RepoStore.isRoot = true;

    vm = createComponent();
    const thead = vm.$el.querySelector('thead');
    const tbody = vm.$el.querySelector('tbody');

    expect(vm.$el.id).toEqual('sidebar');
    expect(vm.$el.classList.contains('sidebar-mini')).toBeFalsy();
    expect(thead.querySelector('.name').textContent.trim()).toEqual('Name');
    expect(thead.querySelector('.last-commit').textContent.trim()).toEqual('Last commit');
    expect(thead.querySelector('.last-update').textContent.trim()).toEqual('Last update');
    expect(tbody.querySelector('.repo-file-options')).toBeFalsy();
    expect(tbody.querySelector('.prev-directory')).toBeFalsy();
    expect(tbody.querySelector('.loading-file')).toBeFalsy();
    expect(tbody.querySelector('.file')).toBeTruthy();
  });

  it('does not render a thead, renders repo-file-options and sets sidebar-mini class if isMini', () => {
    RepoStore.openedFiles = [{
      id: 0,
    }];
    vm = createComponent();

    expect(vm.$el.classList.contains('sidebar-mini')).toBeTruthy();
    expect(vm.$el.querySelector('thead')).toBeTruthy();
    expect(vm.$el.querySelector('thead .repo-file-options')).toBeTruthy();
  });

  it('renders 5 loading files if tree is loading and not hasFiles', () => {
    RepoStore.loading.tree = true;
    RepoStore.files = [];
    vm = createComponent();

    expect(vm.$el.querySelectorAll('tbody .loading-file').length).toEqual(5);
  });

  it('renders a prev directory if is not root', () => {
    RepoStore.files = [file()];
    RepoStore.isRoot = false;
    RepoStore.loading.tree = false;
    vm = createComponent();

    expect(vm.$el.querySelector('tbody .prev-directory')).toBeTruthy();
  });

  describe('flattendFiles', () => {
    it('returns a flattend array of files', () => {
      const f = file();
      f.files.push(file('testing 123'));
      const files = [f, file()];
      vm = createComponent();
      vm.files = files;

      expect(vm.flattendFiles.length).toBe(3);
      expect(vm.flattendFiles[1].name).toBe('testing 123');
    });
  });

  describe('methods', () => {
    describe('fileClicked', () => {
      it('should fetch data for new file', () => {
        spyOn(Helper, 'getContent').and.callThrough();
        RepoStore.files = [file()];
        RepoStore.isRoot = true;
        vm = createComponent();

        vm.fileClicked(RepoStore.files[0]);

        expect(Helper.getContent).toHaveBeenCalledWith(RepoStore.files[0]);
      });

      it('should not fetch data for already opened files', () => {
        const f = file();
        spyOn(Helper, 'getFileFromPath').and.returnValue(f);
        spyOn(RepoStore, 'setActiveFiles');
        vm = createComponent();
        vm.fileClicked(f);

        expect(RepoStore.setActiveFiles).toHaveBeenCalledWith(f);
      });

      it('should hide files in directory if already open', () => {
        spyOn(Helper, 'setDirectoryToClosed').and.callThrough();
        const f = file();
        f.opened = true;
        f.type = 'tree';
        RepoStore.files = [f];
        vm = createComponent();

        vm.fileClicked(RepoStore.files[0]);

        expect(Helper.setDirectoryToClosed).toHaveBeenCalledWith(RepoStore.files[0]);
      });

      describe('submodule', () => {
        it('opens submodule project URL', () => {
          spyOn(gl.utils, 'visitUrl');

          const f = file();
          f.type = 'submodule';

          vm = createComponent();

          vm.fileClicked(f);

          expect(gl.utils.visitUrl).toHaveBeenCalledWith('url');
        });
      });
    });

    describe('goToPreviousDirectoryClicked', () => {
      it('should hide files in directory if already open', () => {
        const prevUrl = 'foo/bar';
        vm = createComponent();

        vm.goToPreviousDirectoryClicked(prevUrl);

        expect(RepoService.url).toEqual(prevUrl);
      });
    });

    describe('back button', () => {
      beforeEach(() => {
        const f = file();
        const file2 = Object.assign({}, file());
        file2.url = 'test';
        RepoStore.files = [f, file2];
        RepoStore.openedFiles = [];
        RepoStore.isRoot = true;

        vm = createComponent();
      });

      it('render previous file when using back button', () => {
        spyOn(Helper, 'getContent').and.callThrough();

        vm.fileClicked(RepoStore.files[1]);
        expect(Helper.getContent).toHaveBeenCalledWith(RepoStore.files[1]);

        history.pushState({
          key: Math.random(),
        }, '', RepoStore.files[1].url);
        const popEvent = document.createEvent('Event');
        popEvent.initEvent('popstate', true, true);
        window.dispatchEvent(popEvent);

        expect(Helper.getContent.calls.mostRecent().args[0].url).toContain(RepoStore.files[1].url);

        window.history.pushState({}, null, '/');
      });
    });
  });
});