Commit cc888e99 authored by Martin Hanzel's avatar Martin Hanzel Committed by Kushal Pandya

Fix test false passes on unmocked requests

Update tests to always mock async method calls
with return values or stub them.
parent 66be6eb1
...@@ -13,7 +13,14 @@ import DropdownContents from 'ee/vue_shared/components/sidebar/epics_select/drop ...@@ -13,7 +13,14 @@ import DropdownContents from 'ee/vue_shared/components/sidebar/epics_select/drop
import createDefaultStore from 'ee/vue_shared/components/sidebar/epics_select/store'; import createDefaultStore from 'ee/vue_shared/components/sidebar/epics_select/store';
import { mockEpic1, mockEpic2, mockIssue, noneEpic } from '../mock_data'; import {
mockEpic1,
mockEpic2,
mockEpics,
mockAssignRemoveRes,
mockIssue,
noneEpic,
} from '../mock_data';
describe('EpicsSelect', () => { describe('EpicsSelect', () => {
describe('Base', () => { describe('Base', () => {
...@@ -43,269 +50,13 @@ describe('EpicsSelect', () => { ...@@ -43,269 +50,13 @@ describe('EpicsSelect', () => {
}); });
describe('methods', () => { describe('methods', () => {
/*
describe('fetchGroupEpics', () => {
it('should call `service.getGroupEpics` and set response to store on request success', done => {
jest.spyOn(wrapper.vm.service, 'getGroupEpics').mockResolvedValue({ data: mockEpics });
jest.spyOn(wrapper.vm.store, 'setEpics');
wrapper.vm
.fetchGroupEpics()
.then(() => {
expect(wrapper.vm.isEpicsLoading).toBe(false);
expect(wrapper.vm.store.setEpics).toHaveBeenCalledWith(mockEpics);
})
.then(done)
.catch(done.fail);
});
it('should call `service.getGroupEpics` and show flash error on request failure', done => {
jest.spyOn(wrapper.vm.service, 'getGroupEpics').mockRejectedValue();
jest.spyOn(wrapper.vm.store, 'setEpics');
wrapper.vm
.fetchGroupEpics()
.then(() => {
expect(wrapper.vm.isEpicsLoading).toBe(false);
expect(wrapper.vm.store.setEpics).not.toHaveBeenCalled();
expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(
errorMessage,
);
})
.then(done)
.catch(done.fail);
});
});
describe('handleSelectSuccess', () => {
const epic = { id: 15 };
const issue = { id: 10 };
it('should set selected Epic issue ID in store when `data.epic.id` & `data.issue.id` matches with seleced Epic ID & Issue ID respectively', done => {
const mockData = {
id: 22,
epic,
issue,
};
jest.spyOn(wrapper.vm.store, 'setSelectedEpicIssueId');
wrapper.setProps({
issueId: issue.id,
});
wrapper.vm.$nextTick(() => {
wrapper.vm.handleSelectSuccess({
data: mockData,
epic,
originalSelectedEpic: mockEpic1,
});
expect(wrapper.vm.isEpicSelectLoading).toBe(false);
expect(wrapper.vm.store.setSelectedEpicIssueId).toHaveBeenCalledWith(mockData.id);
done();
});
});
it('should set revert to original Epic in store when `data.epic.id` & `data.issue.id` do not match with seleced Epic ID & Issue ID respectively', done => {
const mockData = {
id: 22,
epic: { id: 11 },
issue: { id: 17 },
};
jest.spyOn(wrapper.vm.store, 'setSelectedEpic');
wrapper.setProps({
issueId: issue.id,
});
wrapper.vm.$nextTick(() => {
wrapper.vm.handleSelectSuccess({
data: mockData,
epic,
originalSelectedEpic: mockEpic1,
});
expect(wrapper.vm.isEpicSelectLoading).toBe(false);
expect(wrapper.vm.store.setSelectedEpic).toHaveBeenCalledWith(mockEpic1);
done();
});
});
});
describe('handleSelectFailure', () => {
it('should set originally selected epic back in the store', () => {
jest.spyOn(wrapper.vm.store, 'setSelectedEpic');
wrapper.vm.handleSelectFailure(errorMessage, mockEpic1);
expect(wrapper.vm.isEpicSelectLoading).toBe(false);
expect(wrapper.vm.store.setSelectedEpic).toHaveBeenCalledWith(mockEpic1);
});
it('should show flash error message', () => {
wrapper.vm.handleSelectFailure(errorMessage, mockEpic1);
expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(
errorMessage,
);
});
});
describe('assignIssueToEpic', () => {
it('should set `isEpicSelectLoading` to true while request is in progress', () => {
jest
.spyOn(wrapper.vm.service, 'assignIssueToEpic')
.mockResolvedValue({ data: mockAssignRemoveRes });
wrapper.vm.assignIssueToEpic(mockEpic1);
expect(wrapper.vm.isEpicSelectLoading).toBe(true);
});
it('should set selected Epic to the store while request is in progress', () => {
jest
.spyOn(wrapper.vm.service, 'assignIssueToEpic')
.mockResolvedValue({ data: mockAssignRemoveRes });
jest.spyOn(wrapper.vm.store, 'setSelectedEpic');
wrapper.vm.assignIssueToEpic(mockEpic1);
expect(wrapper.vm.store.setSelectedEpic).toHaveBeenCalledWith(mockEpic1);
});
it('should set call `service.assignIssueToEpic` with `issueId` & `epic`', () => {
jest
.spyOn(wrapper.vm.service, 'assignIssueToEpic')
.mockResolvedValue({ data: mockAssignRemoveRes });
wrapper.vm.assignIssueToEpic(mockEpic1);
expect(wrapper.vm.service.assignIssueToEpic).toHaveBeenCalledWith(
mockIssue.id,
mockEpic1,
);
});
it('should set call `handleSelectSuccess` request response, epic and originally selected epic on request success', done => {
jest
.spyOn(wrapper.vm.service, 'assignIssueToEpic')
.mockResolvedValue({ data: mockAssignRemoveRes });
jest.spyOn(wrapper.vm, 'handleSelectSuccess');
wrapper.vm
.assignIssueToEpic(mockEpic1)
.then(() => {
expect(wrapper.vm.handleSelectSuccess).toHaveBeenCalledWith(
expect.objectContaining({
data: mockAssignRemoveRes,
epic: mockEpic1,
originalSelectedEpic: mockEpic1,
}),
);
})
.then(done)
.catch(done.fail);
});
it('should set call `handleSelectFailure` with error message and originally selected epic on request failure', done => {
jest.spyOn(wrapper.vm.service, 'assignIssueToEpic').mockRejectedValue();
jest.spyOn(wrapper.vm, 'handleSelectFailure');
wrapper.vm
.assignIssueToEpic(mockEpic1)
.then(() => {
expect(wrapper.vm.handleSelectFailure).toHaveBeenCalledWith(
'Something went wrong while assigning issue to epic.',
mockEpic1,
);
})
.then(done)
.catch(done.fail);
});
});
describe('removeIssueFromEpic', () => {
it('should set `isEpicSelectLoading` to true while request is in progress', () => {
jest
.spyOn(wrapper.vm.service, 'removeIssueFromEpic')
.mockResolvedValue({ data: mockAssignRemoveRes });
wrapper.vm.removeIssueFromEpic(mockEpic1);
expect(wrapper.vm.isEpicSelectLoading).toBe(true);
});
it('should set `No Epic` to the store while request is in progress', () => {
jest
.spyOn(wrapper.vm.service, 'removeIssueFromEpic')
.mockResolvedValue({ data: mockAssignRemoveRes });
jest.spyOn(wrapper.vm.store, 'setSelectedEpic');
wrapper.vm.removeIssueFromEpic(mockEpic1);
expect(wrapper.vm.store.setSelectedEpic).toHaveBeenCalledWith(
expect.objectContaining({
...noneEpic,
}),
);
});
it('should set call `service.removeIssueFromEpic` with selected `epicIssueId` & `epic`', () => {
jest
.spyOn(wrapper.vm.service, 'removeIssueFromEpic')
.mockResolvedValue({ data: mockAssignRemoveRes });
wrapper.vm.removeIssueFromEpic(mockEpic1);
expect(wrapper.vm.service.removeIssueFromEpic).toHaveBeenCalledWith(
mockIssue.epic_issue_id,
mockEpic1,
);
});
it('should set call `handleSelectSuccess` request response, epic and originally selected epic on request success', done => {
jest
.spyOn(wrapper.vm.service, 'removeIssueFromEpic')
.mockResolvedValue({ data: mockAssignRemoveRes });
jest.spyOn(wrapper.vm, 'handleSelectSuccess');
wrapper.vm
.removeIssueFromEpic(mockEpic1)
.then(() => {
expect(wrapper.vm.handleSelectSuccess).toHaveBeenCalledWith(
expect.objectContaining({
data: mockAssignRemoveRes,
epic: mockEpic1,
originalSelectedEpic: mockEpic1,
}),
);
})
.then(done)
.catch(done.fail);
});
it('should set call `handleSelectFailure` with error message and originally selected epic on request failure', done => {
jest.spyOn(wrapper.vm.service, 'removeIssueFromEpic').mockRejectedValue();
jest.spyOn(wrapper.vm, 'handleSelectFailure');
wrapper.vm
.removeIssueFromEpic(mockEpic1)
.then(() => {
expect(wrapper.vm.handleSelectFailure).toHaveBeenCalledWith(
'Something went wrong while removing issue from epic.',
mockEpic1,
);
})
.then(done)
.catch(done.fail);
});
});
*/
describe('handleDropdownShown', () => { describe('handleDropdownShown', () => {
it('should call `fetchEpics` when `groupEpics` does not return any epics', done => { it('should call `fetchEpics` when `groupEpics` does not return any epics', done => {
jest.spyOn(wrapper.vm, 'fetchEpics'); jest.spyOn(wrapper.vm, 'fetchEpics').mockReturnValue(
Promise.resolve({
data: mockEpics,
}),
);
store.dispatch('receiveEpicsSuccess', []); store.dispatch('receiveEpicsSuccess', []);
...@@ -329,7 +80,11 @@ describe('EpicsSelect', () => { ...@@ -329,7 +80,11 @@ describe('EpicsSelect', () => {
describe('handleItemSelect', () => { describe('handleItemSelect', () => {
it('should call `removeIssueFromEpic` with selected epic when `epic` param represents `No Epic`', () => { it('should call `removeIssueFromEpic` with selected epic when `epic` param represents `No Epic`', () => {
jest.spyOn(wrapper.vm, 'removeIssueFromEpic'); jest.spyOn(wrapper.vm, 'removeIssueFromEpic').mockReturnValue(
Promise.resolve({
data: mockAssignRemoveRes,
}),
);
store.dispatch('setSelectedEpic', mockEpic1); store.dispatch('setSelectedEpic', mockEpic1);
wrapper.vm.handleItemSelect(noneEpic); wrapper.vm.handleItemSelect(noneEpic);
...@@ -338,7 +93,11 @@ describe('EpicsSelect', () => { ...@@ -338,7 +93,11 @@ describe('EpicsSelect', () => {
}); });
it('should call `assignIssueToEpic` with passed `epic` param when it does not represent `No Epic`', () => { it('should call `assignIssueToEpic` with passed `epic` param when it does not represent `No Epic`', () => {
jest.spyOn(wrapper.vm, 'assignIssueToEpic'); jest.spyOn(wrapper.vm, 'assignIssueToEpic').mockReturnValue(
Promise.resolve({
data: mockAssignRemoveRes,
}),
);
wrapper.vm.handleItemSelect(mockEpic2); wrapper.vm.handleItemSelect(mockEpic2);
......
...@@ -6,9 +6,10 @@ axios.isMock = true; ...@@ -6,9 +6,10 @@ axios.isMock = true;
axios.defaults.adapter = config => { axios.defaults.adapter = config => {
const message = const message =
`Unexpected unmocked request: ${JSON.stringify(config, null, 2)}\n` + `Unexpected unmocked request: ${JSON.stringify(config, null, 2)}\n` +
'Consider using the `axios-mock-adapter` in tests.'; 'Consider using the `axios-mock-adapter` module in tests.';
const error = new Error(message); const error = new Error(message);
error.config = config; error.config = config;
global.fail(error);
throw error; throw error;
}; };
......
/* eslint-disable global-require, promise/catch-or-return */ /* eslint-disable global-require */
import path from 'path'; import path from 'path';
...@@ -126,9 +126,8 @@ describe('mocks_helper.js', () => { ...@@ -126,9 +126,8 @@ describe('mocks_helper.js', () => {
it('survives jest.isolateModules()', done => { it('survives jest.isolateModules()', done => {
jest.isolateModules(() => { jest.isolateModules(() => {
const axios2 = require('~/lib/utils/axios_utils').default; const axios2 = require('~/lib/utils/axios_utils').default;
expect(axios2.get('http://gitlab.com')) expect(axios2.isMock).toBe(true);
.rejects.toThrow('Unexpected unmocked request') done();
.then(done);
}); });
}); });
......
...@@ -4,9 +4,11 @@ const $ = jest.requireActual('jquery'); ...@@ -4,9 +4,11 @@ const $ = jest.requireActual('jquery');
// Fail tests for unmocked requests // Fail tests for unmocked requests
$.ajax = () => { $.ajax = () => {
throw new Error( const err = new Error(
'Unexpected unmocked jQuery.ajax() call! Make sure to mock jQuery.ajax() in tests.', 'Unexpected unmocked jQuery.ajax() call! Make sure to mock jQuery.ajax() in tests.',
); );
global.fail(err);
throw err;
}; };
// jquery is not an ES6 module // jquery is not an ES6 module
......
...@@ -3,11 +3,22 @@ import axios from '~/lib/utils/axios_utils'; ...@@ -3,11 +3,22 @@ import axios from '~/lib/utils/axios_utils';
describe('Mock auto-injection', () => { describe('Mock auto-injection', () => {
describe('mocks', () => { describe('mocks', () => {
it('~/lib/utils/axios_utils', () => let failMock;
expect(axios.get('http://gitlab.com')).rejects.toThrow('Unexpected unmocked request')); beforeEach(() => {
failMock = jest.spyOn(global, 'fail').mockImplementation();
});
it('~/lib/utils/axios_utils', done => {
expect(axios.get('http://gitlab.com')).rejects.toThrow('Unexpected unmocked request');
setImmediate(() => {
expect(failMock).toHaveBeenCalledTimes(1);
done();
});
});
it('jQuery.ajax()', () => { it('jQuery.ajax()', () => {
expect($.ajax).toThrow('Unexpected unmocked'); expect($.ajax).toThrow('Unexpected unmocked');
expect(failMock).toHaveBeenCalledTimes(1);
}); });
}); });
}); });
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