boards_selector_spec.js 5.44 KB
Newer Older
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
import Vue from 'vue';
import BoardsSelector from '~/boards/components/boards_selector.vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper';
import { TEST_HOST } from 'spec/test_constants';
import boardsStore from '~/boards/stores/boards_store';

const throttleDuration = 1;

function boardGenerator(n) {
  return new Array(n).fill().map((board, id) => {
    const name = `board${id}`;

    return {
      id,
      name,
    };
  });
}

describe('BoardsSelector', () => {
  let vm;
  let allBoardsResponse;
  let recentBoardsResponse;
  let fillSearchBox;
  const boards = boardGenerator(20);
  const recentBoards = boardGenerator(5);

  beforeEach(done => {
    setFixtures('<div class="js-boards-selector"></div>');
    window.gl = window.gl || {};

    boardsStore.setEndpoints({
      boardsEndpoint: '',
      recentBoardsEndpoint: '',
      listsEndpoint: '',
      bulkUpdatePath: '',
      boardId: '',
    });

    allBoardsResponse = Promise.resolve({
      data: boards,
    });
    recentBoardsResponse = Promise.resolve({
      data: recentBoards,
    });

47 48
    spyOn(boardsStore, 'allBoards').and.returnValue(allBoardsResponse);
    spyOn(boardsStore, 'recentBoards').and.returnValue(recentBoardsResponse);
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

    const Component = Vue.extend(BoardsSelector);
    vm = mountComponent(
      Component,
      {
        throttleDuration,
        currentBoard: {
          id: 1,
          name: 'Development',
          milestone_id: null,
          weight: null,
          assignee_id: null,
          labels: [],
        },
        milestonePath: `${TEST_HOST}/milestone/path`,
        boardBaseUrl: `${TEST_HOST}/board/base/url`,
        hasMissingBoards: false,
        canAdminBoard: true,
        multipleIssueBoardsAvailable: true,
        labelsPath: `${TEST_HOST}/labels/path`,
        projectId: 42,
        groupId: 19,
        scopedIssueBoardFeatureEnabled: true,
        weights: [],
      },
      document.querySelector('.js-boards-selector'),
    );

77 78
    // Emits gl-dropdown show event to simulate the dropdown is opened at initialization time
    vm.$children[0].$emit('show');
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

    Promise.all([allBoardsResponse, recentBoardsResponse])
      .then(() => vm.$nextTick())
      .then(done)
      .catch(done.fail);

    fillSearchBox = filterTerm => {
      const { searchBox } = vm.$refs;
      const searchBoxInput = searchBox.$el.querySelector('input');
      searchBoxInput.value = filterTerm;
      searchBoxInput.dispatchEvent(new Event('input'));
    };
  });

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

  describe('filtering', () => {
    it('shows all boards without filtering', done => {
      vm.$nextTick()
        .then(() => {
          const dropdownItem = vm.$el.querySelectorAll('.js-dropdown-item');

          expect(dropdownItem.length).toBe(boards.length + recentBoards.length);
        })
        .then(done)
        .catch(done.fail);
    });

    it('shows only matching boards when filtering', done => {
      const filterTerm = 'board1';
      const expectedCount = boards.filter(board => board.name.includes(filterTerm)).length;

      fillSearchBox(filterTerm);

      vm.$nextTick()
        .then(() => {
          const dropdownItems = vm.$el.querySelectorAll('.js-dropdown-item');

          expect(dropdownItems.length).toBe(expectedCount);
        })
        .then(done)
        .catch(done.fail);
    });

    it('shows message if there are no matching boards', done => {
      fillSearchBox('does not exist');

      vm.$nextTick()
        .then(() => {
          const dropdownItems = vm.$el.querySelectorAll('.js-dropdown-item');

          expect(dropdownItems.length).toBe(0);
          expect(vm.$el).toContainText('No matching boards found');
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('recent boards section', () => {
    it('shows only when boards are greater than 10', done => {
      vm.$nextTick()
        .then(() => {
          const headerEls = vm.$el.querySelectorAll('.dropdown-bold-header');

          const expectedCount = 2; // Recent + All

          expect(expectedCount).toBe(headerEls.length);
        })
        .then(done)
        .catch(done.fail);
    });

    it('does not show when boards are less than 10', done => {
      spyOn(vm, 'initScrollFade');
      spyOn(vm, 'setScrollFade');

      vm.$nextTick()
        .then(() => {
          vm.boards = vm.boards.slice(0, 5);
        })
        .then(vm.$nextTick)
        .then(() => {
          const headerEls = vm.$el.querySelectorAll('.dropdown-bold-header');
          const expectedCount = 0;

          expect(expectedCount).toBe(headerEls.length);
        })
        .then(done)
        .catch(done.fail);
    });

    it('does not show when recentBoards api returns empty array', done => {
      vm.$nextTick()
        .then(() => {
          vm.recentBoards = [];
        })
        .then(vm.$nextTick)
        .then(() => {
          const headerEls = vm.$el.querySelectorAll('.dropdown-bold-header');
          const expectedCount = 0;

          expect(expectedCount).toBe(headerEls.length);
        })
        .then(done)
        .catch(done.fail);
    });

    it('does not show when search is active', done => {
      fillSearchBox('Random string');

      vm.$nextTick()
        .then(() => {
          const headerEls = vm.$el.querySelectorAll('.dropdown-bold-header');
          const expectedCount = 0;

          expect(expectedCount).toBe(headerEls.length);
        })
        .then(done)
        .catch(done.fail);
    });
  });
});