board_new_issue_spec.js 4.9 KB
Newer Older
P
Phil Hughes 已提交
1 2
/* global BoardService */
/* global List */
3

P
Phil Hughes 已提交
4
import Vue from 'vue';
E
Eric Eastwood 已提交
5 6
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
7
import boardNewIssue from '~/boards/components/board_new_issue.vue';
8

9
import '~/boards/models/list';
E
Eric Eastwood 已提交
10
import { listObj, boardsMockInterceptor, mockBoardService } from './mock_data';
11

P
Phil Hughes 已提交
12
describe('Issue boards new issue form', () => {
13 14
  let vm;
  let list;
E
Eric Eastwood 已提交
15
  let mock;
16
  let newIssueMock;
17
  const promiseReturn = {
E
Eric Eastwood 已提交
18 19
    data: {
      iid: 100,
20 21
    },
  };
S
Simon Knox 已提交
22

23
  const submitIssue = () => {
24 25 26 27 28
    const dummySubmitEvent = {
      preventDefault() {},
    };
    vm.$refs.submitButton = vm.$el.querySelector('.btn-success');
    return vm.submit(dummySubmitEvent);
29 30 31
  };

  beforeEach((done) => {
32 33
    setFixtures('<div class="test-container"></div>');

34 35
    const BoardNewIssueComp = Vue.extend(boardNewIssue);

E
Eric Eastwood 已提交
36 37 38
    mock = new MockAdapter(axios);
    mock.onAny().reply(boardsMockInterceptor);

S
Simon Knox 已提交
39
    gl.boardService = mockBoardService();
40 41 42
    gl.issueBoards.BoardsStore.create();
    gl.IssueBoardsApp = new Vue();

43 44 45 46 47 48 49 50 51
    list = new List(listObj);

    newIssueMock = Promise.resolve(promiseReturn);
    spyOn(list, 'newIssue').and.callFake(() => newIssueMock);

    vm = new BoardNewIssueComp({
      propsData: {
        list,
      },
52
    }).$mount(document.querySelector('.test-container'));
53 54 55 56

    Vue.nextTick()
      .then(done)
      .catch(done.fail);
57 58
  });

E
Eric Eastwood 已提交
59 60
  afterEach(() => {
    vm.$destroy();
61
    mock.restore();
E
Eric Eastwood 已提交
62
  });
63

64
  it('calls submit if submit button is clicked', (done) => {
65
    spyOn(vm, 'submit').and.callFake(e => e.preventDefault());
66 67 68 69 70 71 72 73 74 75 76
    vm.title = 'Testing Title';

    Vue.nextTick()
      .then(() => {
        vm.$el.querySelector('.btn-success').click();

        expect(vm.submit.calls.count()).toBe(1);
        expect(vm.$refs['submit-button']).toBe(vm.$el.querySelector('.btn-success'));
      })
      .then(done)
      .catch(done.fail);
77 78 79 80 81 82 83 84 85
  });

  it('disables submit button if title is empty', () => {
    expect(vm.$el.querySelector('.btn-success').disabled).toBe(true);
  });

  it('enables submit button if title is not empty', (done) => {
    vm.title = 'Testing Title';

86 87 88 89 90 91 92
    Vue.nextTick()
      .then(() => {
        expect(vm.$el.querySelector('.form-control').value).toBe('Testing Title');
        expect(vm.$el.querySelector('.btn-success').disabled).not.toBe(true);
      })
      .then(done)
      .catch(done.fail);
93 94 95 96 97
  });

  it('clears title after clicking cancel', (done) => {
    vm.$el.querySelector('.btn-default').click();

98 99 100 101 102 103
    Vue.nextTick()
      .then(() => {
        expect(vm.title).toBe('');
      })
      .then(done)
      .catch(done.fail);
104 105 106
  });

  it('does not create new issue if title is empty', (done) => {
107 108 109 110 111 112
    submitIssue()
      .then(() => {
        expect(list.newIssue).not.toHaveBeenCalled();
      })
      .then(done)
      .catch(done.fail);
113 114 115 116 117 118
  });

  describe('submit success', () => {
    it('creates new issue', (done) => {
      vm.title = 'submit title';

119 120 121 122 123 124 125
      Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(list.newIssue).toHaveBeenCalled();
        })
        .then(done)
        .catch(done.fail);
126 127 128
    });

    it('enables button after submit', (done) => {
P
Phil Hughes 已提交
129
      vm.title = 'submit issue';
130

131 132 133 134 135 136 137
      Vue.nextTick()
        .then(submitIssue)
        .then(() => {
          expect(vm.$el.querySelector('.btn-success').disabled).toBe(false);
        })
        .then(done)
        .catch(done.fail);
138 139 140 141 142
    });

    it('clears title after submit', (done) => {
      vm.title = 'submit issue';

143 144 145
      Vue.nextTick()
        .then(submitIssue)
        .then(() => {
S
Simon Knox 已提交
146
          expect(vm.title).toBe('');
147 148 149
        })
        .then(done)
        .catch(done.fail);
150 151 152
    });

    it('sets detail issue after submit', (done) => {
S
Simon Knox 已提交
153
      expect(gl.issueBoards.BoardsStore.detail.issue.title).toBe(undefined);
154 155
      vm.title = 'submit issue';

156 157 158
      Vue.nextTick()
        .then(submitIssue)
        .then(() => {
S
Simon Knox 已提交
159
          expect(gl.issueBoards.BoardsStore.detail.issue.title).toBe('submit issue');
160 161 162
        })
        .then(done)
        .catch(done.fail);
163 164 165 166 167
    });

    it('sets detail list after submit', (done) => {
      vm.title = 'submit issue';

168 169 170
      Vue.nextTick()
        .then(submitIssue)
        .then(() => {
S
Simon Knox 已提交
171
          expect(gl.issueBoards.BoardsStore.detail.list.id).toBe(list.id);
172 173 174
        })
        .then(done)
        .catch(done.fail);
175 176 177 178
    });
  });

  describe('submit error', () => {
179 180
    beforeEach(() => {
      newIssueMock = Promise.reject(new Error('My hovercraft is full of eels!'));
181
      vm.title = 'error';
182
    });
183

184 185 186 187
    it('removes issue', (done) => {
      Vue.nextTick()
        .then(submitIssue)
        .then(() => {
188
          expect(list.issues.length).toBe(1);
189 190 191
        })
        .then(done)
        .catch(done.fail);
192 193 194
    });

    it('shows error', (done) => {
195 196 197
      Vue.nextTick()
        .then(submitIssue)
        .then(() => {
198
          expect(vm.error).toBe(true);
199 200 201
        })
        .then(done)
        .catch(done.fail);
202 203 204
    });
  });
});