app_spec.js 16.4 KB
Newer Older
1 2
import Vue from 'vue';

3
import * as utils from '~/lib/utils/url_utility';
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
import appComponent from '~/groups/components/app.vue';
import groupFolderComponent from '~/groups/components/group_folder.vue';
import groupItemComponent from '~/groups/components/group_item.vue';
import eventHub from '~/groups/event_hub';
import GroupsStore from '~/groups/store/groups_store';
import GroupsService from '~/groups/service/groups_service';

import {
  mockEndpoint, mockGroups, mockSearchedGroups,
  mockRawPageInfo, mockParentGroupItem, mockRawChildren,
  mockChildren, mockPageInfo,
} from '../mock_data';

const createComponent = (hideProjects = false) => {
  const Component = Vue.extend(appComponent);
  const store = new GroupsStore(false);
  const service = new GroupsService(mockEndpoint);

  return new Component({
    propsData: {
      store,
      service,
      hideProjects,
    },
  });
};

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

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

  beforeEach((done) => {
    Vue.component('group-folder', groupFolderComponent);
    Vue.component('group-item', groupItemComponent);

    vm = createComponent();

    Vue.nextTick(() => {
      done();
    });
  });

  describe('computed', () => {
    beforeEach(() => {
      vm.$mount();
    });

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

    describe('groups', () => {
      it('should return list of groups from store', () => {
        spyOn(vm.store, 'getGroups');

        const groups = vm.groups;
        expect(vm.store.getGroups).toHaveBeenCalled();
        expect(groups).not.toBeDefined();
      });
    });

    describe('pageInfo', () => {
      it('should return pagination info from store', () => {
        spyOn(vm.store, 'getPaginationInfo');

        const pageInfo = vm.pageInfo;
        expect(vm.store.getPaginationInfo).toHaveBeenCalled();
        expect(pageInfo).not.toBeDefined();
      });
    });
  });

  describe('methods', () => {
    beforeEach(() => {
      vm.$mount();
    });

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

    describe('fetchGroups', () => {
      it('should call `getGroups` with all the params provided', (done) => {
        spyOn(vm.service, 'getGroups').and.returnValue(returnServicePromise(mockGroups));

        vm.fetchGroups({
          parentId: 1,
          page: 2,
          filterGroupsBy: 'git',
          sortBy: 'created_desc',
105
          archived: true,
106 107
        });
        setTimeout(() => {
108
          expect(vm.service.getGroups).toHaveBeenCalledWith(1, 2, 'git', 'created_desc', true);
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
          done();
        }, 0);
      });

      it('should set headers to store for building pagination info when called with `updatePagination`', (done) => {
        spyOn(vm.service, 'getGroups').and.returnValue(returnServicePromise({ headers: mockRawPageInfo }));
        spyOn(vm, 'updatePagination');

        vm.fetchGroups({ updatePagination: true });
        setTimeout(() => {
          expect(vm.service.getGroups).toHaveBeenCalled();
          expect(vm.updatePagination).toHaveBeenCalled();
          done();
        }, 0);
      });

      it('should show flash error when request fails', (done) => {
        spyOn(vm.service, 'getGroups').and.returnValue(returnServicePromise(null, true));
        spyOn($, 'scrollTo');
        spyOn(window, 'Flash');

        vm.fetchGroups({});
        setTimeout(() => {
          expect(vm.isLoading).toBeFalsy();
          expect($.scrollTo).toHaveBeenCalledWith(0);
          expect(window.Flash).toHaveBeenCalledWith('An error occurred. Please try again.');
          done();
        }, 0);
      });
    });

    describe('fetchAllGroups', () => {
      it('should fetch default set of groups', (done) => {
        spyOn(vm, 'fetchGroups').and.returnValue(returnServicePromise(mockGroups));
        spyOn(vm, 'updatePagination').and.callThrough();
        spyOn(vm, 'updateGroups').and.callThrough();

        vm.fetchAllGroups();
        expect(vm.isLoading).toBeTruthy();
        expect(vm.fetchGroups).toHaveBeenCalled();
        setTimeout(() => {
          expect(vm.isLoading).toBeFalsy();
          expect(vm.updateGroups).toHaveBeenCalled();
          done();
        }, 0);
      });

      it('should fetch matching set of groups when app is loaded with search query', (done) => {
        spyOn(vm, 'fetchGroups').and.returnValue(returnServicePromise(mockSearchedGroups));
        spyOn(vm, 'updateGroups').and.callThrough();

        vm.fetchAllGroups();
        expect(vm.fetchGroups).toHaveBeenCalledWith({
          page: null,
          filterGroupsBy: null,
          sortBy: null,
          updatePagination: true,
166
          archived: null,
167 168 169 170 171 172 173 174 175 176 177 178
        });
        setTimeout(() => {
          expect(vm.updateGroups).toHaveBeenCalled();
          done();
        }, 0);
      });
    });

    describe('fetchPage', () => {
      it('should fetch groups for provided page details and update window state', (done) => {
        spyOn(vm, 'fetchGroups').and.returnValue(returnServicePromise(mockGroups));
        spyOn(vm, 'updateGroups').and.callThrough();
179
        spyOn(utils, 'mergeUrlParams').and.callThrough();
180 181 182
        spyOn(window.history, 'replaceState');
        spyOn($, 'scrollTo');

183
        vm.fetchPage(2, null, null, true);
184 185 186 187 188 189
        expect(vm.isLoading).toBeTruthy();
        expect(vm.fetchGroups).toHaveBeenCalledWith({
          page: 2,
          filterGroupsBy: null,
          sortBy: null,
          updatePagination: true,
190
          archived: true,
191 192 193 194
        });
        setTimeout(() => {
          expect(vm.isLoading).toBeFalsy();
          expect($.scrollTo).toHaveBeenCalledWith(0);
195
          expect(utils.mergeUrlParams).toHaveBeenCalledWith({ page: 2 }, jasmine.any(String));
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
          expect(window.history.replaceState).toHaveBeenCalledWith({
            page: jasmine.any(String),
          }, jasmine.any(String), jasmine.any(String));
          expect(vm.updateGroups).toHaveBeenCalled();
          done();
        }, 0);
      });
    });

    describe('toggleChildren', () => {
      let groupItem;

      beforeEach(() => {
        groupItem = Object.assign({}, mockParentGroupItem);
        groupItem.isOpen = false;
        groupItem.isChildrenLoading = false;
      });

      it('should fetch children of given group and expand it if group is collapsed and children are not loaded', (done) => {
        spyOn(vm, 'fetchGroups').and.returnValue(returnServicePromise(mockRawChildren));
        spyOn(vm.store, 'setGroupChildren');

        vm.toggleChildren(groupItem);
        expect(groupItem.isChildrenLoading).toBeTruthy();
        expect(vm.fetchGroups).toHaveBeenCalledWith({
          parentId: groupItem.id,
        });
        setTimeout(() => {
          expect(vm.store.setGroupChildren).toHaveBeenCalled();
          done();
        }, 0);
      });

      it('should skip network request while expanding group if children are already loaded', () => {
        spyOn(vm, 'fetchGroups');
        groupItem.children = mockRawChildren;

        vm.toggleChildren(groupItem);
        expect(vm.fetchGroups).not.toHaveBeenCalled();
        expect(groupItem.isOpen).toBeTruthy();
      });

      it('should collapse group if it is already expanded', () => {
        spyOn(vm, 'fetchGroups');
        groupItem.isOpen = true;

        vm.toggleChildren(groupItem);
        expect(vm.fetchGroups).not.toHaveBeenCalled();
        expect(groupItem.isOpen).toBeFalsy();
      });

      it('should set `isChildrenLoading` back to `false` if load request fails', (done) => {
        spyOn(vm, 'fetchGroups').and.returnValue(returnServicePromise({}, true));

        vm.toggleChildren(groupItem);
        expect(groupItem.isChildrenLoading).toBeTruthy();
        setTimeout(() => {
          expect(groupItem.isChildrenLoading).toBeFalsy();
          done();
        }, 0);
      });
    });

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
    describe('showLeaveGroupModal', () => {
      it('caches candidate group (as props) which is to be left', () => {
        const group = Object.assign({}, mockParentGroupItem);
        expect(vm.targetGroup).toBe(null);
        expect(vm.targetParentGroup).toBe(null);
        vm.showLeaveGroupModal(group, mockParentGroupItem);
        expect(vm.targetGroup).not.toBe(null);
        expect(vm.targetParentGroup).not.toBe(null);
      });

      it('updates props which show modal confirmation dialog', () => {
        const group = Object.assign({}, mockParentGroupItem);
        expect(vm.showModal).toBeFalsy();
        expect(vm.groupLeaveConfirmationMessage).toBe('');
        vm.showLeaveGroupModal(group, mockParentGroupItem);
        expect(vm.showModal).toBeTruthy();
        expect(vm.groupLeaveConfirmationMessage).toBe(`Are you sure you want to leave the "${group.fullName}" group?`);
      });
    });

    describe('hideLeaveGroupModal', () => {
      it('hides modal confirmation which is shown before leaving the group', () => {
        const group = Object.assign({}, mockParentGroupItem);
        vm.showLeaveGroupModal(group, mockParentGroupItem);
        expect(vm.showModal).toBeTruthy();
        vm.hideLeaveGroupModal();
        expect(vm.showModal).toBeFalsy();
      });
    });

289 290 291 292 293 294 295 296 297
    describe('leaveGroup', () => {
      let groupItem;
      let childGroupItem;

      beforeEach(() => {
        groupItem = Object.assign({}, mockParentGroupItem);
        groupItem.children = mockChildren;
        childGroupItem = groupItem.children[0];
        groupItem.isChildrenLoading = false;
298 299
        vm.targetGroup = childGroupItem;
        vm.targetParentGroup = groupItem;
300 301
      });

302
      it('hides modal confirmation leave group and remove group item from tree', (done) => {
303 304 305 306 307 308
        const notice = `You left the "${childGroupItem.fullName}" group.`;
        spyOn(vm.service, 'leaveGroup').and.returnValue(returnServicePromise({ notice }));
        spyOn(vm.store, 'removeGroup').and.callThrough();
        spyOn(window, 'Flash');
        spyOn($, 'scrollTo');

309 310 311 312
        vm.leaveGroup();
        expect(vm.showModal).toBeFalsy();
        expect(vm.targetGroup.isBeingRemoved).toBeTruthy();
        expect(vm.service.leaveGroup).toHaveBeenCalledWith(vm.targetGroup.leavePath);
313 314
        setTimeout(() => {
          expect($.scrollTo).toHaveBeenCalledWith(0);
315
          expect(vm.store.removeGroup).toHaveBeenCalledWith(vm.targetGroup, vm.targetParentGroup);
316 317 318 319 320 321 322 323 324 325 326
          expect(window.Flash).toHaveBeenCalledWith(notice, 'notice');
          done();
        }, 0);
      });

      it('should show error flash message if request failed to leave group', (done) => {
        const message = 'An error occurred. Please try again.';
        spyOn(vm.service, 'leaveGroup').and.returnValue(returnServicePromise({ status: 500 }, true));
        spyOn(vm.store, 'removeGroup').and.callThrough();
        spyOn(window, 'Flash');

327 328
        vm.leaveGroup();
        expect(vm.targetGroup.isBeingRemoved).toBeTruthy();
329 330 331 332
        expect(vm.service.leaveGroup).toHaveBeenCalledWith(childGroupItem.leavePath);
        setTimeout(() => {
          expect(vm.store.removeGroup).not.toHaveBeenCalled();
          expect(window.Flash).toHaveBeenCalledWith(message);
333
          expect(vm.targetGroup.isBeingRemoved).toBeFalsy();
334 335 336 337 338 339 340 341 342 343 344
          done();
        }, 0);
      });

      it('should show appropriate error flash message if request forbids to leave group', (done) => {
        const message = 'Failed to leave the group. Please make sure you are not the only owner.';
        spyOn(vm.service, 'leaveGroup').and.returnValue(returnServicePromise({ status: 403 }, true));
        spyOn(vm.store, 'removeGroup').and.callThrough();
        spyOn(window, 'Flash');

        vm.leaveGroup(childGroupItem, groupItem);
345
        expect(vm.targetGroup.isBeingRemoved).toBeTruthy();
346 347 348 349
        expect(vm.service.leaveGroup).toHaveBeenCalledWith(childGroupItem.leavePath);
        setTimeout(() => {
          expect(vm.store.removeGroup).not.toHaveBeenCalled();
          expect(window.Flash).toHaveBeenCalledWith(message);
350
          expect(vm.targetGroup.isBeingRemoved).toBeFalsy();
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
          done();
        }, 0);
      });
    });

    describe('updatePagination', () => {
      it('should set pagination info to store from provided headers', () => {
        spyOn(vm.store, 'setPaginationInfo');

        vm.updatePagination(mockRawPageInfo);
        expect(vm.store.setPaginationInfo).toHaveBeenCalledWith(mockRawPageInfo);
      });
    });

    describe('updateGroups', () => {
      it('should call setGroups on store if method was called directly', () => {
        spyOn(vm.store, 'setGroups');

        vm.updateGroups(mockGroups);
        expect(vm.store.setGroups).toHaveBeenCalledWith(mockGroups);
      });

      it('should call setSearchedGroups on store if method was called with fromSearch param', () => {
        spyOn(vm.store, 'setSearchedGroups');

        vm.updateGroups(mockGroups, true);
        expect(vm.store.setSearchedGroups).toHaveBeenCalledWith(mockGroups);
      });

      it('should set `isSearchEmpty` prop based on groups count', () => {
        vm.updateGroups(mockGroups);
        expect(vm.isSearchEmpty).toBeFalsy();

        vm.updateGroups([]);
        expect(vm.isSearchEmpty).toBeTruthy();
      });
    });
  });

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

      const newVm = createComponent();
      newVm.$mount();

      Vue.nextTick(() => {
        expect(eventHub.$on).toHaveBeenCalledWith('fetchPage', jasmine.any(Function));
        expect(eventHub.$on).toHaveBeenCalledWith('toggleChildren', jasmine.any(Function));
400
        expect(eventHub.$on).toHaveBeenCalledWith('showLeaveGroupModal', jasmine.any(Function));
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
        expect(eventHub.$on).toHaveBeenCalledWith('updatePagination', jasmine.any(Function));
        expect(eventHub.$on).toHaveBeenCalledWith('updateGroups', jasmine.any(Function));
        newVm.$destroy();
        done();
      });
    });

    it('should initialize `searchEmptyMessage` prop with correct string when `hideProjects` is `false`', (done) => {
      const newVm = createComponent();
      newVm.$mount();
      Vue.nextTick(() => {
        expect(newVm.searchEmptyMessage).toBe('Sorry, no groups or projects matched your search');
        newVm.$destroy();
        done();
      });
    });

    it('should initialize `searchEmptyMessage` prop with correct string when `hideProjects` is `true`', (done) => {
      const newVm = createComponent(true);
      newVm.$mount();
      Vue.nextTick(() => {
        expect(newVm.searchEmptyMessage).toBe('Sorry, no groups matched your search');
        newVm.$destroy();
        done();
      });
    });
  });

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

      const newVm = createComponent();
      newVm.$mount();
      newVm.$destroy();

      Vue.nextTick(() => {
        expect(eventHub.$off).toHaveBeenCalledWith('fetchPage', jasmine.any(Function));
        expect(eventHub.$off).toHaveBeenCalledWith('toggleChildren', jasmine.any(Function));
440
        expect(eventHub.$off).toHaveBeenCalledWith('showLeaveGroupModal', jasmine.any(Function));
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
        expect(eventHub.$off).toHaveBeenCalledWith('updatePagination', jasmine.any(Function));
        expect(eventHub.$off).toHaveBeenCalledWith('updateGroups', jasmine.any(Function));
        done();
      });
    });
  });

  describe('template', () => {
    beforeEach(() => {
      vm.$mount();
    });

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

    it('should render loading icon', (done) => {
      vm.isLoading = true;
      Vue.nextTick(() => {
        expect(vm.$el.querySelector('.loading-animation')).toBeDefined();
        expect(vm.$el.querySelector('i.fa').getAttribute('aria-label')).toBe('Loading groups');
        done();
      });
    });

    it('should render groups tree', (done) => {
Phil Hughes's avatar
Phil Hughes committed
467
      vm.store.state.groups = [mockParentGroupItem];
468
      vm.isLoading = false;
Phil Hughes's avatar
Phil Hughes committed
469
      vm.store.state.pageInfo = mockPageInfo;
470 471 472 473 474
      Vue.nextTick(() => {
        expect(vm.$el.querySelector('.groups-list-tree-container')).toBeDefined();
        done();
      });
    });
475 476 477 478 479 480 481 482 483

    it('renders modal confirmation dialog', () => {
      vm.groupLeaveConfirmationMessage = 'Are you sure you want to leave the "foo" group?';
      vm.showModal = true;
      const modalDialogEl = vm.$el.querySelector('.modal');
      expect(modalDialogEl).not.toBe(null);
      expect(modalDialogEl.querySelector('.modal-title').innerText.trim()).toBe('Are you sure?');
      expect(modalDialogEl.querySelector('.btn.btn-warning').innerText.trim()).toBe('Leave');
    });
484 485
  });
});