BigW Consortium Gitlab

list_spec.js 4.59 KB
Newer Older
1 2 3 4
/* eslint-disable comma-dangle */
/* global boardsMockInterceptor */
/* global BoardService */
/* global List */
5
/* global ListIssue */
6
/* global listObj */
7
/* global listObjDuplicate */
8

9 10
import Vue from 'vue';

11 12 13 14 15 16 17 18
import '~/lib/utils/url_utility';
import '~/boards/models/issue';
import '~/boards/models/label';
import '~/boards/models/list';
import '~/boards/models/assignee';
import '~/boards/services/board_service';
import '~/boards/stores/boards_store';
import './mock_data';
19 20 21 22 23

describe('List model', () => {
  let list;

  beforeEach(() => {
24
    Vue.http.interceptors.push(boardsMockInterceptor);
25
    gl.boardService = new BoardService('/test/issue-boards/board', '', '1');
26
    gl.issueBoards.BoardsStore.create();
27 28 29 30

    list = new List(listObj);
  });

31 32 33 34
  afterEach(() => {
    Vue.http.interceptors = _.without(Vue.http.interceptors, boardsMockInterceptor);
  });

35 36 37 38 39 40 41 42 43 44 45
  it('gets issues when created', (done) => {
    setTimeout(() => {
      expect(list.issues.length).toBe(1);
      done();
    }, 0);
  });

  it('saves list and returns ID', (done) => {
    list = new List({
      title: 'test',
      label: {
46
        id: _.random(10000),
47 48 49 50 51 52 53
        title: 'test',
        color: 'red'
      }
    });
    list.save();

    setTimeout(() => {
54
      expect(list.id).toBe(listObj.id);
55 56 57 58 59 60 61
      expect(list.type).toBe('label');
      expect(list.position).toBe(0);
      done();
    }, 0);
  });

  it('destroys the list', (done) => {
62
    gl.issueBoards.BoardsStore.addList(listObj);
63
    list = gl.issueBoards.BoardsStore.findList('id', listObj.id);
64
    expect(gl.issueBoards.BoardsStore.state.lists.length).toBe(1);
65 66 67
    list.destroy();

    setTimeout(() => {
68
      expect(gl.issueBoards.BoardsStore.state.lists.length).toBe(0);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      done();
    }, 0);
  });

  it('gets issue from list', (done) => {
    setTimeout(() => {
      const issue = list.findIssue(1);
      expect(issue).toBeDefined();
      done();
    }, 0);
  });

  it('removes issue', (done) => {
    setTimeout(() => {
      const issue = list.findIssue(1);
      expect(list.issues.length).toBe(1);
      list.removeIssue(issue);
      expect(list.issues.length).toBe(0);
      done();
    }, 0);
  });
90 91 92 93 94

  it('sends service request to update issue label', () => {
    const listDup = new List(listObjDuplicate);
    const issue = new ListIssue({
      title: 'Testing',
95
      iid: _.random(10000),
96
      confidential: false,
97 98
      labels: [list.label, listDup.label],
      assignees: [],
99 100 101 102 103 104 105
    });

    list.issues.push(issue);
    listDup.issues.push(issue);

    spyOn(gl.boardService, 'moveIssue').and.callThrough();

106
    listDup.updateIssueLabel(issue, list);
107

Phil Hughes committed
108 109
    expect(gl.boardService.moveIssue)
      .toHaveBeenCalledWith(issue.id, list.id, listDup.id, undefined, undefined);
110
  });
111 112 113 114 115 116 117

  describe('page number', () => {
    beforeEach(() => {
      spyOn(list, 'getIssues');
    });

    it('increase page number if current issue count is more than the page size', () => {
Phil Hughes committed
118
      for (let i = 0; i < 30; i += 1) {
119 120 121 122
        list.issues.push(new ListIssue({
          title: 'Testing',
          iid: _.random(10000) + i,
          confidential: false,
Clement Ho committed
123 124
          labels: [list.label],
          assignees: [],
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        }));
      }
      list.issuesSize = 50;

      expect(list.issues.length).toBe(30);

      list.nextPage();

      expect(list.page).toBe(2);
      expect(list.getIssues).toHaveBeenCalled();
    });

    it('does not increase page number if issue count is less than the page size', () => {
      list.issues.push(new ListIssue({
        title: 'Testing',
        iid: _.random(10000),
        confidential: false,
Clement Ho committed
142 143
        labels: [list.label],
        assignees: [],
144 145 146 147 148 149 150 151 152
      }));
      list.issuesSize = 2;

      list.nextPage();

      expect(list.page).toBe(1);
      expect(list.getIssues).toHaveBeenCalled();
    });
  });
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

  describe('newIssue', () => {
    beforeEach(() => {
      spyOn(gl.boardService, 'newIssue').and.returnValue(Promise.resolve({
        json() {
          return {
            iid: 42,
          };
        },
      }));
    });

    it('adds new issue to top of list', (done) => {
      list.issues.push(new ListIssue({
        title: 'Testing',
        iid: _.random(10000),
        confidential: false,
        labels: [list.label],
        assignees: [],
      }));
      const dummyIssue = new ListIssue({
        title: 'new issue',
        iid: _.random(10000),
        confidential: false,
        labels: [list.label],
        assignees: [],
      });

      list.newIssue(dummyIssue)
        .then(() => {
          expect(list.issues.length).toBe(2);
          expect(list.issues[0]).toBe(dummyIssue);
        })
        .then(done)
        .catch(done.fail);
    });
  });
190
});