summaryrefslogtreecommitdiff
path: root/spec/javascripts/projects_dropdown/components/app_spec.js
blob: 2054fef790b7b7c2f1391560df244ee286d4c6f0 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import Vue from 'vue';

import bp from '~/breakpoints';
import appComponent from '~/projects_dropdown/components/app.vue';
import eventHub from '~/projects_dropdown/event_hub';
import ProjectsStore from '~/projects_dropdown/store/projects_store';
import ProjectsService from '~/projects_dropdown/service/projects_service';

import mountComponent from 'spec/helpers/vue_mount_component_helper';
import { currentSession, mockProject, mockRawProject } from '../mock_data';

const createComponent = () => {
  gon.api_version = currentSession.apiVersion;
  const Component = Vue.extend(appComponent);
  const store = new ProjectsStore();
  const service = new ProjectsService(currentSession.username);

  return mountComponent(Component, {
    store,
    service,
    currentUserName: currentSession.username,
    currentProject: currentSession.project,
  });
};

const returnServicePromise = (data, failed) => new Promise((resolve, reject) => {
  if (failed) {
    reject(data);
  } else {
    resolve({
      json() {
        return data;
      },
    });
  }
});

describe('AppComponent', () => {
  describe('computed', () => {
    let vm;

    beforeEach(() => {
      vm = createComponent();
    });

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

    describe('frequentProjects', () => {
      it('should return list of frequently accessed projects from store', () => {
        expect(vm.frequentProjects).toBeDefined();
        expect(vm.frequentProjects.length).toBe(0);

        vm.store.setFrequentProjects([mockProject]);
        expect(vm.frequentProjects).toBeDefined();
        expect(vm.frequentProjects.length).toBe(1);
      });
    });

    describe('searchProjects', () => {
      it('should return list of frequently accessed projects from store', () => {
        expect(vm.searchProjects).toBeDefined();
        expect(vm.searchProjects.length).toBe(0);

        vm.store.setSearchedProjects([mockRawProject]);
        expect(vm.searchProjects).toBeDefined();
        expect(vm.searchProjects.length).toBe(1);
      });
    });
  });

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

    beforeEach(() => {
      vm = createComponent();
    });

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

    describe('toggleFrequentProjectsList', () => {
      it('should toggle props which control visibility of Frequent Projects list from state passed', () => {
        vm.toggleFrequentProjectsList(true);
        expect(vm.isLoadingProjects).toBeFalsy();
        expect(vm.isSearchListVisible).toBeFalsy();
        expect(vm.isFrequentsListVisible).toBeTruthy();

        vm.toggleFrequentProjectsList(false);
        expect(vm.isLoadingProjects).toBeTruthy();
        expect(vm.isSearchListVisible).toBeTruthy();
        expect(vm.isFrequentsListVisible).toBeFalsy();
      });
    });

    describe('toggleSearchProjectsList', () => {
      it('should toggle props which control visibility of Searched Projects list from state passed', () => {
        vm.toggleSearchProjectsList(true);
        expect(vm.isLoadingProjects).toBeFalsy();
        expect(vm.isFrequentsListVisible).toBeFalsy();
        expect(vm.isSearchListVisible).toBeTruthy();

        vm.toggleSearchProjectsList(false);
        expect(vm.isLoadingProjects).toBeTruthy();
        expect(vm.isFrequentsListVisible).toBeTruthy();
        expect(vm.isSearchListVisible).toBeFalsy();
      });
    });

    describe('toggleLoader', () => {
      it('should toggle props which control visibility of list loading animation from state passed', () => {
        vm.toggleLoader(true);
        expect(vm.isFrequentsListVisible).toBeFalsy();
        expect(vm.isSearchListVisible).toBeFalsy();
        expect(vm.isLoadingProjects).toBeTruthy();

        vm.toggleLoader(false);
        expect(vm.isFrequentsListVisible).toBeTruthy();
        expect(vm.isSearchListVisible).toBeTruthy();
        expect(vm.isLoadingProjects).toBeFalsy();
      });
    });

    describe('fetchFrequentProjects', () => {
      it('should set props for loading animation to `true` while frequent projects list is being loaded', () => {
        spyOn(vm, 'toggleLoader');

        vm.fetchFrequentProjects();
        expect(vm.isLocalStorageFailed).toBeFalsy();
        expect(vm.toggleLoader).toHaveBeenCalledWith(true);
      });

      it('should set props for loading animation to `false` and props for frequent projects list to `true` once data is loaded', () => {
        const mockData = [mockProject];

        spyOn(vm.service, 'getFrequentProjects').and.returnValue(mockData);
        spyOn(vm.store, 'setFrequentProjects');
        spyOn(vm, 'toggleFrequentProjectsList');

        vm.fetchFrequentProjects();
        expect(vm.service.getFrequentProjects).toHaveBeenCalled();
        expect(vm.store.setFrequentProjects).toHaveBeenCalledWith(mockData);
        expect(vm.toggleFrequentProjectsList).toHaveBeenCalledWith(true);
      });

      it('should set props for failure message to `true` when method fails to fetch frequent projects list', () => {
        spyOn(vm.service, 'getFrequentProjects').and.returnValue(null);
        spyOn(vm.store, 'setFrequentProjects');
        spyOn(vm, 'toggleFrequentProjectsList');

        expect(vm.isLocalStorageFailed).toBeFalsy();

        vm.fetchFrequentProjects();
        expect(vm.service.getFrequentProjects).toHaveBeenCalled();
        expect(vm.store.setFrequentProjects).toHaveBeenCalledWith([]);
        expect(vm.toggleFrequentProjectsList).toHaveBeenCalledWith(true);
        expect(vm.isLocalStorageFailed).toBeTruthy();
      });

      it('should set props for search results list to `true` if search query was already made previously', () => {
        spyOn(bp, 'getBreakpointSize').and.returnValue('md');
        spyOn(vm.service, 'getFrequentProjects');
        spyOn(vm, 'toggleSearchProjectsList');

        vm.searchQuery = 'test';
        vm.fetchFrequentProjects();
        expect(vm.service.getFrequentProjects).not.toHaveBeenCalled();
        expect(vm.toggleSearchProjectsList).toHaveBeenCalledWith(true);
      });

      it('should set props for frequent projects list to `true` if search query was already made but screen size is less than 768px', () => {
        spyOn(bp, 'getBreakpointSize').and.returnValue('sm');
        spyOn(vm, 'toggleSearchProjectsList');
        spyOn(vm.service, 'getFrequentProjects');

        vm.searchQuery = 'test';
        vm.fetchFrequentProjects();
        expect(vm.service.getFrequentProjects).toHaveBeenCalled();
        expect(vm.toggleSearchProjectsList).not.toHaveBeenCalled();
      });
    });

    describe('fetchSearchedProjects', () => {
      const searchQuery = 'test';

      it('should perform search with provided search query', (done) => {
        const mockData = [mockRawProject];
        spyOn(vm, 'toggleLoader');
        spyOn(vm, 'toggleSearchProjectsList');
        spyOn(vm.service, 'getSearchedProjects').and.returnValue(returnServicePromise(mockData));
        spyOn(vm.store, 'setSearchedProjects');

        vm.fetchSearchedProjects(searchQuery);
        setTimeout(() => {
          expect(vm.searchQuery).toBe(searchQuery);
          expect(vm.toggleLoader).toHaveBeenCalledWith(true);
          expect(vm.service.getSearchedProjects).toHaveBeenCalledWith(searchQuery);
          expect(vm.toggleSearchProjectsList).toHaveBeenCalledWith(true);
          expect(vm.store.setSearchedProjects).toHaveBeenCalledWith(mockData);
          done();
        }, 0);
      });

      it('should update props for showing search failure', (done) => {
        spyOn(vm, 'toggleSearchProjectsList');
        spyOn(vm.service, 'getSearchedProjects').and.returnValue(returnServicePromise({}, true));

        vm.fetchSearchedProjects(searchQuery);
        setTimeout(() => {
          expect(vm.searchQuery).toBe(searchQuery);
          expect(vm.service.getSearchedProjects).toHaveBeenCalledWith(searchQuery);
          expect(vm.isSearchFailed).toBeTruthy();
          expect(vm.toggleSearchProjectsList).toHaveBeenCalledWith(true);
          done();
        }, 0);
      });
    });

    describe('logCurrentProjectAccess', () => {
      it('should log current project access via service', (done) => {
        spyOn(vm.service, 'logProjectAccess');

        vm.currentProject = mockProject;
        vm.logCurrentProjectAccess();

        setTimeout(() => {
          expect(vm.service.logProjectAccess).toHaveBeenCalledWith(mockProject);
          done();
        }, 1);
      });
    });

    describe('handleSearchClear', () => {
      it('should show frequent projects list when search input is cleared', () => {
        spyOn(vm.store, 'clearSearchedProjects');
        spyOn(vm, 'toggleFrequentProjectsList');

        vm.handleSearchClear();

        expect(vm.toggleFrequentProjectsList).toHaveBeenCalledWith(true);
        expect(vm.store.clearSearchedProjects).toHaveBeenCalled();
        expect(vm.searchQuery).toBe('');
      });
    });

    describe('handleSearchFailure', () => {
      it('should show failure message within dropdown', () => {
        spyOn(vm, 'toggleSearchProjectsList');

        vm.handleSearchFailure();
        expect(vm.toggleSearchProjectsList).toHaveBeenCalledWith(true);
        expect(vm.isSearchFailed).toBeTruthy();
      });
    });
  });

  describe('created', () => {
    it('should bind event listeners on eventHub', (done) => {
      spyOn(eventHub, '$on');

      createComponent().$mount();

      Vue.nextTick(() => {
        expect(eventHub.$on).toHaveBeenCalledWith('dropdownOpen', jasmine.any(Function));
        expect(eventHub.$on).toHaveBeenCalledWith('searchProjects', jasmine.any(Function));
        expect(eventHub.$on).toHaveBeenCalledWith('searchCleared', jasmine.any(Function));
        expect(eventHub.$on).toHaveBeenCalledWith('searchFailed', jasmine.any(Function));
        done();
      });
    });
  });

  describe('beforeDestroy', () => {
    it('should unbind event listeners on eventHub', (done) => {
      const vm = createComponent();
      spyOn(eventHub, '$off');

      vm.$mount();
      vm.$destroy();

      Vue.nextTick(() => {
        expect(eventHub.$off).toHaveBeenCalledWith('dropdownOpen', jasmine.any(Function));
        expect(eventHub.$off).toHaveBeenCalledWith('searchProjects', jasmine.any(Function));
        expect(eventHub.$off).toHaveBeenCalledWith('searchCleared', jasmine.any(Function));
        expect(eventHub.$off).toHaveBeenCalledWith('searchFailed', jasmine.any(Function));
        done();
      });
    });
  });

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

    beforeEach(() => {
      vm = createComponent();
    });

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

    it('should render search input', () => {
      expect(vm.$el.querySelector('.search-input-container')).toBeDefined();
    });

    it('should render loading animation', (done) => {
      vm.toggleLoader(true);
      Vue.nextTick(() => {
        const loadingEl = vm.$el.querySelector('.loading-animation');

        expect(loadingEl).toBeDefined();
        expect(loadingEl.classList.contains('prepend-top-20')).toBeTruthy();
        expect(loadingEl.querySelector('i').getAttribute('aria-label')).toBe('Loading projects');
        done();
      });
    });

    it('should render frequent projects list header', (done) => {
      vm.toggleFrequentProjectsList(true);
      Vue.nextTick(() => {
        const sectionHeaderEl = vm.$el.querySelector('.section-header');

        expect(sectionHeaderEl).toBeDefined();
        expect(sectionHeaderEl.innerText.trim()).toBe('Frequently visited');
        done();
      });
    });

    it('should render frequent projects list', (done) => {
      vm.toggleFrequentProjectsList(true);
      Vue.nextTick(() => {
        expect(vm.$el.querySelector('.projects-list-frequent-container')).toBeDefined();
        done();
      });
    });

    it('should render searched projects list', (done) => {
      vm.toggleSearchProjectsList(true);
      Vue.nextTick(() => {
        expect(vm.$el.querySelector('.section-header')).toBe(null);
        expect(vm.$el.querySelector('.projects-list-search-container')).toBeDefined();
        done();
      });
    });
  });
});