Commit cc394e05 authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

Merge branch '344247-replace-setimmediate-in-tests-4' into 'master'

Remove setImmediate usage in specs

See merge request gitlab-org/gitlab!80370
parents 75980735 071d4de2
import Vue from 'vue'; import Vue, { nextTick } from 'vue';
import { createComponentWithStore } from 'helpers/vue_mount_component_helper'; import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import newDropdown from '~/ide/components/new_dropdown/index.vue'; import newDropdown from '~/ide/components/new_dropdown/index.vue';
import { createStore } from '~/ide/stores'; import { createStore } from '~/ide/stores';
...@@ -57,18 +57,16 @@ describe('new dropdown component', () => { ...@@ -57,18 +57,16 @@ describe('new dropdown component', () => {
}); });
describe('isOpen', () => { describe('isOpen', () => {
it('scrolls dropdown into view', (done) => { it('scrolls dropdown into view', async () => {
jest.spyOn(vm.$refs.dropdownMenu, 'scrollIntoView').mockImplementation(() => {}); jest.spyOn(vm.$refs.dropdownMenu, 'scrollIntoView').mockImplementation(() => {});
vm.isOpen = true; vm.isOpen = true;
setImmediate(() => { await nextTick();
expect(vm.$refs.dropdownMenu.scrollIntoView).toHaveBeenCalledWith({ expect(vm.$refs.dropdownMenu.scrollIntoView).toHaveBeenCalledWith({
block: 'nearest', block: 'nearest',
}); });
done();
});
}); });
}); });
......
...@@ -4,6 +4,7 @@ import Vue, { nextTick } from 'vue'; ...@@ -4,6 +4,7 @@ import Vue, { nextTick } from 'vue';
import { dispatch } from 'codesandbox-api'; import { dispatch } from 'codesandbox-api';
import smooshpack from 'smooshpack'; import smooshpack from 'smooshpack';
import Vuex from 'vuex'; import Vuex from 'vuex';
import waitForPromises from 'helpers/wait_for_promises';
import Clientside from '~/ide/components/preview/clientside.vue'; import Clientside from '~/ide/components/preview/clientside.vue';
import { PING_USAGE_PREVIEW_KEY, PING_USAGE_PREVIEW_SUCCESS_KEY } from '~/ide/constants'; import { PING_USAGE_PREVIEW_KEY, PING_USAGE_PREVIEW_SUCCESS_KEY } from '~/ide/constants';
import eventHub from '~/ide/eventhub'; import eventHub from '~/ide/eventhub';
...@@ -43,8 +44,6 @@ describe('IDE clientside preview', () => { ...@@ -43,8 +44,6 @@ describe('IDE clientside preview', () => {
}; };
const dispatchCodesandboxReady = () => dispatch({ type: 'done' }); const dispatchCodesandboxReady = () => dispatch({ type: 'done' });
const waitForCalls = () => new Promise(setImmediate);
const createComponent = ({ state, getters } = {}) => { const createComponent = ({ state, getters } = {}) => {
store = new Vuex.Store({ store = new Vuex.Store({
state: { state: {
...@@ -100,7 +99,7 @@ describe('IDE clientside preview', () => { ...@@ -100,7 +99,7 @@ describe('IDE clientside preview', () => {
beforeEach(() => { beforeEach(() => {
createComponent({ getters: { packageJson: dummyPackageJson } }); createComponent({ getters: { packageJson: dummyPackageJson } });
return waitForCalls(); return waitForPromises();
}); });
it('creates sandpack manager', () => { it('creates sandpack manager', () => {
...@@ -139,7 +138,7 @@ describe('IDE clientside preview', () => { ...@@ -139,7 +138,7 @@ describe('IDE clientside preview', () => {
state: { codesandboxBundlerUrl: TEST_BUNDLER_URL }, state: { codesandboxBundlerUrl: TEST_BUNDLER_URL },
}); });
return waitForCalls(); return waitForPromises();
}); });
it('creates sandpack manager with bundlerURL', () => { it('creates sandpack manager with bundlerURL', () => {
...@@ -154,7 +153,7 @@ describe('IDE clientside preview', () => { ...@@ -154,7 +153,7 @@ describe('IDE clientside preview', () => {
beforeEach(() => { beforeEach(() => {
createComponent({ getters: { packageJson: dummyPackageJson } }); createComponent({ getters: { packageJson: dummyPackageJson } });
return waitForCalls(); return waitForPromises();
}); });
it('creates sandpack manager', () => { it('creates sandpack manager', () => {
...@@ -340,7 +339,7 @@ describe('IDE clientside preview', () => { ...@@ -340,7 +339,7 @@ describe('IDE clientside preview', () => {
wrapper.setData({ sandpackReady: true }); wrapper.setData({ sandpackReady: true });
wrapper.vm.update(); wrapper.vm.update();
return waitForCalls().then(() => { return waitForPromises().then(() => {
expect(smooshpack.Manager).toHaveBeenCalled(); expect(smooshpack.Manager).toHaveBeenCalled();
}); });
}); });
......
...@@ -81,16 +81,13 @@ describe('Multi-file editor library model', () => { ...@@ -81,16 +81,13 @@ describe('Multi-file editor library model', () => {
}); });
describe('onChange', () => { describe('onChange', () => {
it('calls callback on change', (done) => { it('calls callback on change', () => {
const spy = jest.fn(); const spy = jest.fn();
model.onChange(spy); model.onChange(spy);
model.getModel().setValue('123'); model.getModel().setValue('123');
setImmediate(() => {
expect(spy).toHaveBeenCalledWith(model, expect.anything()); expect(spy).toHaveBeenCalledWith(model, expect.anything());
done();
});
}); });
}); });
......
...@@ -274,24 +274,17 @@ describe('Multi-file store actions', () => { ...@@ -274,24 +274,17 @@ describe('Multi-file store actions', () => {
}); });
describe('scrollToTab', () => { describe('scrollToTab', () => {
it('focuses the current active element', (done) => { it('focuses the current active element', () => {
document.body.innerHTML += document.body.innerHTML +=
'<div id="tabs"><div class="active"><div class="repo-tab"></div></div></div>'; '<div id="tabs"><div class="active"><div class="repo-tab"></div></div></div>';
const el = document.querySelector('.repo-tab'); const el = document.querySelector('.repo-tab');
jest.spyOn(el, 'focus').mockImplementation(); jest.spyOn(el, 'focus').mockImplementation();
store return store.dispatch('scrollToTab').then(() => {
.dispatch('scrollToTab')
.then(() => {
setImmediate(() => {
expect(el.focus).toHaveBeenCalled(); expect(el.focus).toHaveBeenCalled();
document.getElementById('tabs').remove(); document.getElementById('tabs').remove();
done();
}); });
})
.catch(done.fail);
}); });
}); });
......
...@@ -12,7 +12,7 @@ describe('RelatedMergeRequests', () => { ...@@ -12,7 +12,7 @@ describe('RelatedMergeRequests', () => {
let wrapper; let wrapper;
let mock; let mock;
beforeEach((done) => { beforeEach(() => {
// put the fixture in DOM as the component expects // put the fixture in DOM as the component expects
document.body.innerHTML = `<div id="js-issuable-app"></div>`; document.body.innerHTML = `<div id="js-issuable-app"></div>`;
document.getElementById('js-issuable-app').dataset.initial = JSON.stringify(mockData); document.getElementById('js-issuable-app').dataset.initial = JSON.stringify(mockData);
...@@ -29,7 +29,7 @@ describe('RelatedMergeRequests', () => { ...@@ -29,7 +29,7 @@ describe('RelatedMergeRequests', () => {
}, },
}); });
setImmediate(done); return axios.waitForAll();
}); });
afterEach(() => { afterEach(() => {
......
...@@ -394,8 +394,7 @@ describe('common_utils', () => { ...@@ -394,8 +394,7 @@ describe('common_utils', () => {
describe('backOff', () => { describe('backOff', () => {
beforeEach(() => { beforeEach(() => {
// shortcut our timeouts otherwise these tests will take a long time to finish jest.spyOn(window, 'setTimeout');
jest.spyOn(window, 'setTimeout').mockImplementation((cb) => setImmediate(cb, 0));
}); });
it('solves the promise from the callback', (done) => { it('solves the promise from the callback', (done) => {
...@@ -446,6 +445,7 @@ describe('common_utils', () => { ...@@ -446,6 +445,7 @@ describe('common_utils', () => {
if (numberOfCalls < 3) { if (numberOfCalls < 3) {
numberOfCalls += 1; numberOfCalls += 1;
next(); next();
jest.runOnlyPendingTimers();
} else { } else {
stop(resp); stop(resp);
} }
...@@ -464,7 +464,10 @@ describe('common_utils', () => { ...@@ -464,7 +464,10 @@ describe('common_utils', () => {
it('rejects the backOff promise after timing out', (done) => { it('rejects the backOff promise after timing out', (done) => {
commonUtils commonUtils
.backOff((next) => next(), 64000) .backOff((next) => {
next();
jest.runOnlyPendingTimers();
}, 64000)
.catch((errBackoffResp) => { .catch((errBackoffResp) => {
const timeouts = window.setTimeout.mock.calls.map(([, timeout]) => timeout); const timeouts = window.setTimeout.mock.calls.map(([, timeout]) => timeout);
......
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import $ from 'jquery'; import $ from 'jquery';
import { TEST_HOST } from 'spec/test_constants'; import { TEST_HOST } from 'spec/test_constants';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import MergeRequest from '~/merge_request'; import MergeRequest from '~/merge_request';
...@@ -27,31 +28,31 @@ describe('MergeRequest', () => { ...@@ -27,31 +28,31 @@ describe('MergeRequest', () => {
mock.restore(); mock.restore();
}); });
it('modifies the Markdown field', (done) => { it('modifies the Markdown field', async () => {
jest.spyOn($, 'ajax').mockImplementation(); jest.spyOn($, 'ajax').mockImplementation();
const changeEvent = document.createEvent('HTMLEvents'); const changeEvent = document.createEvent('HTMLEvents');
changeEvent.initEvent('change', true, true); changeEvent.initEvent('change', true, true);
$('input[type=checkbox]').first().attr('checked', true)[0].dispatchEvent(changeEvent); $('input[type=checkbox]').first().attr('checked', true)[0].dispatchEvent(changeEvent);
setImmediate(() => {
await waitForPromises();
expect($('.js-task-list-field').val()).toBe( expect($('.js-task-list-field').val()).toBe(
'- [x] Task List Item\n- [ ]\n- [ ] Task List Item 2\n', '- [x] Task List Item\n- [ ]\n- [ ] Task List Item 2\n',
); );
done();
});
}); });
it('ensure that task with only spaces does not get checked incorrectly', (done) => { it('ensure that task with only spaces does not get checked incorrectly', async () => {
// fixed in 'deckar01-task_list', '2.2.1' gem // fixed in 'deckar01-task_list', '2.2.1' gem
jest.spyOn($, 'ajax').mockImplementation(); jest.spyOn($, 'ajax').mockImplementation();
const changeEvent = document.createEvent('HTMLEvents'); const changeEvent = document.createEvent('HTMLEvents');
changeEvent.initEvent('change', true, true); changeEvent.initEvent('change', true, true);
$('input[type=checkbox]').last().attr('checked', true)[0].dispatchEvent(changeEvent); $('input[type=checkbox]').last().attr('checked', true)[0].dispatchEvent(changeEvent);
setImmediate(() => {
await waitForPromises();
expect($('.js-task-list-field').val()).toBe( expect($('.js-task-list-field').val()).toBe(
'- [ ] Task List Item\n- [ ]\n- [x] Task List Item 2\n', '- [ ] Task List Item\n- [ ]\n- [x] Task List Item 2\n',
); );
done();
});
}); });
describe('tasklist', () => { describe('tasklist', () => {
...@@ -60,13 +61,14 @@ describe('MergeRequest', () => { ...@@ -60,13 +61,14 @@ describe('MergeRequest', () => {
const index = 3; const index = 3;
const checked = true; const checked = true;
it('submits an ajax request on tasklist:changed', (done) => { it('submits an ajax request on tasklist:changed', async () => {
$('.js-task-list-field').trigger({ $('.js-task-list-field').trigger({
type: 'tasklist:changed', type: 'tasklist:changed',
detail: { lineNumber, lineSource, index, checked }, detail: { lineNumber, lineSource, index, checked },
}); });
setImmediate(() => { await waitForPromises();
expect(axios.patch).toHaveBeenCalledWith( expect(axios.patch).toHaveBeenCalledWith(
`${TEST_HOST}/frontend-fixtures/merge-requests-project/-/merge_requests/1.json`, `${TEST_HOST}/frontend-fixtures/merge-requests-project/-/merge_requests/1.json`,
{ {
...@@ -77,12 +79,9 @@ describe('MergeRequest', () => { ...@@ -77,12 +79,9 @@ describe('MergeRequest', () => {
}, },
}, },
); );
done();
});
}); });
it('shows an error notification when tasklist update failed', (done) => { it('shows an error notification when tasklist update failed', async () => {
mock mock
.onPatch(`${TEST_HOST}/frontend-fixtures/merge-requests-project/-/merge_requests/1.json`) .onPatch(`${TEST_HOST}/frontend-fixtures/merge-requests-project/-/merge_requests/1.json`)
.reply(409, {}); .reply(409, {});
...@@ -92,13 +91,11 @@ describe('MergeRequest', () => { ...@@ -92,13 +91,11 @@ describe('MergeRequest', () => {
detail: { lineNumber, lineSource, index, checked }, detail: { lineNumber, lineSource, index, checked },
}); });
setImmediate(() => { await waitForPromises();
expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe( expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(
'Someone edited this merge request at the same time you did. Please refresh the page to see changes.', 'Someone edited this merge request at the same time you did. Please refresh the page to see changes.',
); );
done();
});
}); });
}); });
}); });
......
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