board_list_spec.js 5.13 KB
Newer Older
1 2 3
/* global List */
/* global ListIssue */
import Vue from 'vue';
Eric Eastwood's avatar
Eric Eastwood committed
4 5
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
6
import Sortable from 'sortablejs';
7
import BoardList from '~/boards/components/board_list.vue';
8 9 10 11 12
import eventHub from '~/boards/eventhub';
import '~/boards/mixins/sortable_default_options';
import '~/boards/models/issue';
import '~/boards/models/list';
import '~/boards/stores/boards_store';
Eric Eastwood's avatar
Eric Eastwood committed
13
import { listObj, boardsMockInterceptor, mockBoardService } from './mock_data';
14 15 16 17

window.Sortable = Sortable;

describe('Board list component', () => {
Eric Eastwood's avatar
Eric Eastwood committed
18
  let mock;
19 20 21 22 23 24
  let component;

  beforeEach((done) => {
    const el = document.createElement('div');

    document.body.appendChild(el);
Eric Eastwood's avatar
Eric Eastwood committed
25 26
    mock = new MockAdapter(axios);
    mock.onAny().reply(boardsMockInterceptor);
Simon Knox's avatar
Simon Knox committed
27
    gl.boardService = mockBoardService();
28 29 30 31 32 33 34
    gl.issueBoards.BoardsStore.create();
    gl.IssueBoardsApp = new Vue();

    const BoardListComp = Vue.extend(BoardList);
    const list = new List(listObj);
    const issue = new ListIssue({
      title: 'Testing',
Simon Knox's avatar
Simon Knox committed
35
      id: 1,
36 37 38
      iid: 1,
      confidential: false,
      labels: [],
Clement Ho's avatar
Clement Ho committed
39
      assignees: [],
40 41 42
      project: {
        path: '/test',
      },
43 44 45 46 47 48 49 50 51 52 53
    });
    list.issuesSize = 1;
    list.issues.push(issue);

    component = new BoardListComp({
      el,
      propsData: {
        disabled: false,
        list,
        issues: list.issues,
        loading: false,
54 55
        groupId: 1,
        issueLinkBase: '/test/:project_path/issues',
56 57 58 59 60 61 62 63 64 65
        rootPath: '/',
      },
    }).$mount();

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

  afterEach(() => {
66
    mock.restore();
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  });

  it('renders component', () => {
    expect(
      component.$el.classList.contains('board-list-component'),
    ).toBe(true);
  });

  it('renders loading icon', (done) => {
    component.loading = true;

    Vue.nextTick(() => {
      expect(
        component.$el.querySelector('.board-list-loading'),
      ).not.toBeNull();

      done();
    });
  });

  it('renders issues', () => {
    expect(
Clement Ho's avatar
Clement Ho committed
89
      component.$el.querySelectorAll('.board-card').length,
90 91 92
    ).toBe(1);
  });

93 94
  it('renders link properly in issue', () => {
    expect(
Clement Ho's avatar
Clement Ho committed
95
      component.$el.querySelector('.board-card .board-card-title a').getAttribute('href'),
96 97 98
    ).not.toContain(':project_path');
  });

99 100
  it('sets data attribute with issue id', () => {
    expect(
Clement Ho's avatar
Clement Ho committed
101
      component.$el.querySelector('.board-card').getAttribute('data-issue-id'),
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
    ).toBe('1');
  });

  it('shows new issue form', (done) => {
    component.toggleForm();

    Vue.nextTick(() => {
      expect(
        component.$el.querySelector('.board-new-issue-form'),
      ).not.toBeNull();

      expect(
        component.$el.querySelector('.is-smaller'),
      ).not.toBeNull();

      done();
    });
  });

  it('shows new issue form after eventhub event', (done) => {
    eventHub.$emit(`hide-issue-form-${component.list.id}`);

    Vue.nextTick(() => {
      expect(
        component.$el.querySelector('.board-new-issue-form'),
      ).not.toBeNull();

      expect(
        component.$el.querySelector('.is-smaller'),
      ).not.toBeNull();

      done();
    });
  });

  it('does not show new issue form for closed list', (done) => {
    component.list.type = 'closed';
    component.toggleForm();

    Vue.nextTick(() => {
      expect(
        component.$el.querySelector('.board-new-issue-form'),
      ).toBeNull();

      done();
    });
  });

  it('shows count list item', (done) => {
    component.showCount = true;

    Vue.nextTick(() => {
      expect(
        component.$el.querySelector('.board-list-count'),
      ).not.toBeNull();

      expect(
        component.$el.querySelector('.board-list-count').textContent.trim(),
      ).toBe('Showing all issues');

      done();
    });
  });

166 167 168 169 170 171 172 173 174 175 176 177
  it('sets data attribute with invalid id', (done) => {
    component.showCount = true;

    Vue.nextTick(() => {
      expect(
        component.$el.querySelector('.board-list-count').getAttribute('data-issue-id'),
      ).toBe('-1');

      done();
    });
  });

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  it('shows how many more issues to load', (done) => {
    component.showCount = true;
    component.list.issuesSize = 20;

    Vue.nextTick(() => {
      expect(
        component.$el.querySelector('.board-list-count').textContent.trim(),
      ).toBe('Showing 1 of 20 issues');

      done();
    });
  });

  it('loads more issues after scrolling', (done) => {
    spyOn(component.list, 'nextPage');
    component.$refs.list.style.height = '100px';
    component.$refs.list.style.overflow = 'scroll';

Tim Zallmann's avatar
Tim Zallmann committed
196 197 198
    for (let i = 1; i < 20; i += 1) {
      const issue = Object.assign({}, component.list.issues[0]);
      issue.id += i;
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
      component.list.issues.push(issue);
    }

    Vue.nextTick(() => {
      component.$refs.list.scrollTop = 20000;

      setTimeout(() => {
        expect(component.list.nextPage).toHaveBeenCalled();

        done();
      });
    });
  });

  it('shows loading more spinner', (done) => {
    component.showCount = true;
    component.list.loadingMore = true;

    Vue.nextTick(() => {
      expect(
        component.$el.querySelector('.board-list-count .fa-spinner'),
      ).not.toBeNull();

      done();
    });
  });
});