Commit 5d4dfc46 authored by Winnie Hellmann's avatar Winnie Hellmann

Fix timing in AssigneesListComponent tests

parent dac7b72e
...@@ -27,8 +27,11 @@ export default Vue.extend({ ...@@ -27,8 +27,11 @@ export default Vue.extend({
}, },
methods: { methods: {
loadAssignees() { loadAssignees() {
if (!this.store.state.assignees.length) { if (this.store.state.assignees.length) {
axios return Promise.resolve();
}
return axios
.get(this.listAssigneesPath) .get(this.listAssigneesPath)
.then(({ data }) => { .then(({ data }) => {
this.loading = false; this.loading = false;
...@@ -36,11 +39,8 @@ export default Vue.extend({ ...@@ -36,11 +39,8 @@ export default Vue.extend({
}) })
.catch(() => { .catch(() => {
this.loading = false; this.loading = false;
Flash( Flash(__('Something went wrong while fetching assignees list'));
__('Something went wrong while fetching assignees list'),
);
}); });
}
}, },
handleItemClick(assignee) { handleItemClick(assignee) {
if (!this.store.findList('title', assignee.name)) { if (!this.store.findList('title', assignee.name)) {
......
...@@ -7,33 +7,30 @@ import AssigneesListComponent from 'ee/boards/components/assignees_list/'; ...@@ -7,33 +7,30 @@ import AssigneesListComponent from 'ee/boards/components/assignees_list/';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import mountComponent from 'spec/helpers/vue_mount_component_helper';
import { mockAssigneesList } from 'spec/boards/mock_data'; import { mockAssigneesList } from 'spec/boards/mock_data';
import { TEST_HOST } from 'spec/test_constants';
const createComponent = () => mountComponent(AssigneesListComponent, {
listAssigneesPath: `${gl.TEST_HOST}/users.json`,
});
describe('AssigneesListComponent', () => { describe('AssigneesListComponent', () => {
const dummyEndpoint = `${TEST_HOST}/users.json`;
const createComponent = () =>
mountComponent(AssigneesListComponent, {
listAssigneesPath: dummyEndpoint,
});
let vm; let vm;
let mock; let mock;
let statusCode;
let response;
gl.issueBoards.BoardsStore.create(); gl.issueBoards.BoardsStore.create();
gl.issueBoards.BoardsStore.state.assignees = []; gl.issueBoards.BoardsStore.state.assignees = [];
beforeEach(() => { beforeEach(() => {
statusCode = 200;
response = mockAssigneesList;
mock = new MockAdapter(axios); mock = new MockAdapter(axios);
document.body.innerHTML += '<div class="flash-container"></div>'; setFixtures('<div class="flash-container"></div>');
mock.onGet(`${gl.TEST_HOST}/users.json`).reply(() => [statusCode, response]);
vm = createComponent(); vm = createComponent();
}); });
afterEach(() => { afterEach(() => {
document.querySelector('.flash-container').remove();
vm.$destroy(); vm.$destroy();
mock.restore(); mock.restore();
}); });
...@@ -47,35 +44,47 @@ describe('AssigneesListComponent', () => { ...@@ -47,35 +44,47 @@ describe('AssigneesListComponent', () => {
describe('methods', () => { describe('methods', () => {
describe('loadAssignees', () => { describe('loadAssignees', () => {
it('calls axios.get and sets response to store.state.assignees', (done) => { it('calls axios.get and sets response to store.state.assignees', done => {
mock.onGet(`${gl.TEST_HOST}/users.json`).reply(200, mockAssigneesList); mock.onGet(dummyEndpoint).reply(200, mockAssigneesList);
gl.issueBoards.BoardsStore.state.assignees = []; gl.issueBoards.BoardsStore.state.assignees = [];
vm.loadAssignees(); vm
setTimeout(() => { .loadAssignees()
.then(() => {
expect(vm.loading).toBe(false); expect(vm.loading).toBe(false);
expect(vm.store.state.assignees.length).toBe(mockAssigneesList.length); expect(vm.store.state.assignees.length).toBe(mockAssigneesList.length);
done(); })
}, 0); .then(done)
.catch(done.fail);
}); });
it('does not call axios.get when store.state.assignees is not empty', () => { it('does not call axios.get when store.state.assignees is not empty', done => {
spyOn(axios, 'get'); spyOn(axios, 'get');
gl.issueBoards.BoardsStore.state.assignees = mockAssigneesList; gl.issueBoards.BoardsStore.state.assignees = mockAssigneesList;
vm.loadAssignees();
vm
.loadAssignees()
.then(() => {
expect(axios.get).not.toHaveBeenCalled(); expect(axios.get).not.toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
}); });
it('calls axios.get and shows Flash error when request fails', (done) => { it('calls axios.get and shows Flash error when request fails', done => {
mock.onGet(`${gl.TEST_HOST}/users.json`).reply(500, {}); mock.onGet(dummyEndpoint).replyOnce(500, {});
gl.issueBoards.BoardsStore.state.assignees = []; gl.issueBoards.BoardsStore.state.assignees = [];
vm.loadAssignees(); vm
setTimeout(() => { .loadAssignees()
.then(() => {
expect(vm.loading).toBe(false); expect(vm.loading).toBe(false);
expect(document.querySelector('.flash-text').innerText.trim()).toBe('Something went wrong while fetching assignees list'); expect(document.querySelector('.flash-text').innerText.trim()).toBe(
done(); 'Something went wrong while fetching assignees list',
}, 0); );
})
.then(done)
.catch(done.fail);
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment