summaryrefslogtreecommitdiff
path: root/spec/javascripts/repo/components/repo_sidebar_spec.js
blob: 35d2b37ac2a8199a7ab21b5a3c741b0ca8ec3dc3 (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
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';

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

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

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

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

  it('renders a sidebar', () => {
    RepoStore.files = [{
      id: 0,
    }];
    RepoStore.openedFiles = [];
    RepoStore.isRoot = false;

    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).toEqual('Name');
    expect(thead.querySelector('.last-commit').textContent).toEqual('Last commit');
    expect(thead.querySelector('.last-update').textContent).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')).toBeFalsy();
    expect(vm.$el.querySelector('tbody .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 isRoot', () => {
    RepoStore.files = [{
      id: 0,
    }];
    RepoStore.isRoot = true;
    vm = createComponent();

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

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

        vm.fileClicked(file1);

        expect(Helper.getContent).toHaveBeenCalledWith(file1);
      });

      it('should not fetch data for already opened files', () => {
        const file = {
          id: 42,
          url: 'foo',
        };

        spyOn(Helper, 'getFileFromPath').and.returnValue(file);
        spyOn(RepoStore, 'setActiveFiles');
        vm = createComponent();
        vm.fileClicked(file);

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

      it('should hide files in directory if already open', () => {
        spyOn(RepoStore, 'removeChildFilesOfTree').and.callThrough();
        const file1 = {
          id: 0,
          type: 'tree',
          url: '',
          opened: true,
        };
        RepoStore.files = [file1];
        RepoStore.isRoot = true;
        vm = createComponent();

        vm.fileClicked(file1);

        expect(RepoStore.removeChildFilesOfTree).toHaveBeenCalledWith(file1);
      });
    });

    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', () => {
      const file1 = {
        id: 1,
        url: 'file1',
      };
      const file2 = {
        id: 2,
        url: 'file2',
      };
      RepoStore.files = [file1, file2];
      RepoStore.openedFiles = [file1, file2];
      RepoStore.isRoot = true;

      vm = createComponent();
      vm.fileClicked(file1);

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

        vm.fileClicked(file2);
        expect(Helper.getContent).toHaveBeenCalledWith(file2);
        Helper.getContent.calls.reset();

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

        expect(Helper.getContent.calls.mostRecent().args[0].url).toContain(file1.url);

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