Commit 7bb0d54a authored by Andrew Fontaine's avatar Andrew Fontaine

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

Remove setImmediate usage in specs

See merge request gitlab-org/gitlab!80369
parents b711569d 02509ff9
...@@ -8,13 +8,10 @@ describe('Mock auto-injection', () => { ...@@ -8,13 +8,10 @@ describe('Mock auto-injection', () => {
failMock = jest.spyOn(global, 'fail').mockImplementation(); failMock = jest.spyOn(global, 'fail').mockImplementation();
}); });
it('~/lib/utils/axios_utils', () => { it('~/lib/utils/axios_utils', async () => {
return Promise.all([ await expect(axios.get('http://gitlab.com')).rejects.toThrow('Unexpected unmocked request');
expect(axios.get('http://gitlab.com')).rejects.toThrow('Unexpected unmocked request'),
setImmediate(() => {
expect(failMock).toHaveBeenCalledTimes(1); expect(failMock).toHaveBeenCalledTimes(1);
}),
]);
}); });
it('jQuery.ajax()', () => { it('jQuery.ajax()', () => {
......
...@@ -25,12 +25,10 @@ describe('Code component', () => { ...@@ -25,12 +25,10 @@ describe('Code component', () => {
}; };
describe('without output', () => { describe('without output', () => {
beforeEach((done) => { beforeEach(() => {
vm = setupComponent(json.cells[0]); vm = setupComponent(json.cells[0]);
setImmediate(() => { return nextTick();
done();
});
}); });
it('does not render output prompt', () => { it('does not render output prompt', () => {
...@@ -39,12 +37,10 @@ describe('Code component', () => { ...@@ -39,12 +37,10 @@ describe('Code component', () => {
}); });
describe('with output', () => { describe('with output', () => {
beforeEach((done) => { beforeEach(() => {
vm = setupComponent(json.cells[2]); vm = setupComponent(json.cells[2]);
setImmediate(() => { return nextTick();
done();
});
}); });
it('does not render output prompt', () => { it('does not render output prompt', () => {
......
import Vue from 'vue'; import Vue, { nextTick } from 'vue';
import json from 'test_fixtures/blob/notebook/basic.json'; import json from 'test_fixtures/blob/notebook/basic.json';
import CodeComponent from '~/notebook/cells/output/index.vue'; import CodeComponent from '~/notebook/cells/output/index.vue';
...@@ -18,13 +18,11 @@ describe('Output component', () => { ...@@ -18,13 +18,11 @@ describe('Output component', () => {
}; };
describe('text output', () => { describe('text output', () => {
beforeEach((done) => { beforeEach(() => {
const textType = json.cells[2]; const textType = json.cells[2];
createComponent(textType.outputs[0]); createComponent(textType.outputs[0]);
setImmediate(() => { return nextTick();
done();
});
}); });
it('renders as plain text', () => { it('renders as plain text', () => {
...@@ -37,13 +35,11 @@ describe('Output component', () => { ...@@ -37,13 +35,11 @@ describe('Output component', () => {
}); });
describe('image output', () => { describe('image output', () => {
beforeEach((done) => { beforeEach(() => {
const imageType = json.cells[3]; const imageType = json.cells[3];
createComponent(imageType.outputs[0]); createComponent(imageType.outputs[0]);
setImmediate(() => { return nextTick();
done();
});
}); });
it('renders as an image', () => { it('renders as an image', () => {
...@@ -86,13 +82,11 @@ describe('Output component', () => { ...@@ -86,13 +82,11 @@ describe('Output component', () => {
}); });
describe('svg output', () => { describe('svg output', () => {
beforeEach((done) => { beforeEach(() => {
const svgType = json.cells[5]; const svgType = json.cells[5];
createComponent(svgType.outputs[0]); createComponent(svgType.outputs[0]);
setImmediate(() => { return nextTick();
done();
});
}); });
it('renders as an svg', () => { it('renders as an svg', () => {
...@@ -101,13 +95,11 @@ describe('Output component', () => { ...@@ -101,13 +95,11 @@ describe('Output component', () => {
}); });
describe('default to plain text', () => { describe('default to plain text', () => {
beforeEach((done) => { beforeEach(() => {
const unknownType = json.cells[6]; const unknownType = json.cells[6];
createComponent(unknownType.outputs[0]); createComponent(unknownType.outputs[0]);
setImmediate(() => { return nextTick();
done();
});
}); });
it('renders as plain text', () => { it('renders as plain text', () => {
...@@ -119,16 +111,14 @@ describe('Output component', () => { ...@@ -119,16 +111,14 @@ describe('Output component', () => {
expect(vm.$el.querySelector('.prompt span')).not.toBeNull(); expect(vm.$el.querySelector('.prompt span')).not.toBeNull();
}); });
it("renders as plain text when doesn't recognise other types", (done) => { it("renders as plain text when doesn't recognise other types", async () => {
const unknownType = json.cells[7]; const unknownType = json.cells[7];
createComponent(unknownType.outputs[0]); createComponent(unknownType.outputs[0]);
setImmediate(() => { await nextTick();
expect(vm.$el.querySelector('pre')).not.toBeNull(); expect(vm.$el.querySelector('pre')).not.toBeNull();
expect(vm.$el.textContent.trim()).toContain('testing'); expect(vm.$el.textContent.trim()).toContain('testing');
done();
});
}); });
}); });
}); });
import Vue from 'vue'; import Vue, { nextTick } from 'vue';
import PromptComponent from '~/notebook/cells/prompt.vue'; import PromptComponent from '~/notebook/cells/prompt.vue';
const Component = Vue.extend(PromptComponent); const Component = Vue.extend(PromptComponent);
...@@ -7,7 +7,7 @@ describe('Prompt component', () => { ...@@ -7,7 +7,7 @@ describe('Prompt component', () => {
let vm; let vm;
describe('input', () => { describe('input', () => {
beforeEach((done) => { beforeEach(() => {
vm = new Component({ vm = new Component({
propsData: { propsData: {
type: 'In', type: 'In',
...@@ -16,9 +16,7 @@ describe('Prompt component', () => { ...@@ -16,9 +16,7 @@ describe('Prompt component', () => {
}); });
vm.$mount(); vm.$mount();
setImmediate(() => { return nextTick();
done();
});
}); });
it('renders in label', () => { it('renders in label', () => {
...@@ -31,7 +29,7 @@ describe('Prompt component', () => { ...@@ -31,7 +29,7 @@ describe('Prompt component', () => {
}); });
describe('output', () => { describe('output', () => {
beforeEach((done) => { beforeEach(() => {
vm = new Component({ vm = new Component({
propsData: { propsData: {
type: 'Out', type: 'Out',
...@@ -40,9 +38,7 @@ describe('Prompt component', () => { ...@@ -40,9 +38,7 @@ describe('Prompt component', () => {
}); });
vm.$mount(); vm.$mount();
setImmediate(() => { return nextTick();
done();
});
}); });
it('renders in label', () => { it('renders in label', () => {
......
import { mount } from '@vue/test-utils'; import { mount } from '@vue/test-utils';
import Vue from 'vue'; import Vue, { nextTick } from 'vue';
import json from 'test_fixtures/blob/notebook/basic.json'; import json from 'test_fixtures/blob/notebook/basic.json';
import jsonWithWorksheet from 'test_fixtures/blob/notebook/worksheets.json'; import jsonWithWorksheet from 'test_fixtures/blob/notebook/worksheets.json';
import Notebook from '~/notebook/index.vue'; import Notebook from '~/notebook/index.vue';
...@@ -17,12 +17,10 @@ describe('Notebook component', () => { ...@@ -17,12 +17,10 @@ describe('Notebook component', () => {
} }
describe('without JSON', () => { describe('without JSON', () => {
beforeEach((done) => { beforeEach(() => {
vm = buildComponent({}); vm = buildComponent({});
setImmediate(() => { return nextTick();
done();
});
}); });
it('does not render', () => { it('does not render', () => {
...@@ -31,12 +29,10 @@ describe('Notebook component', () => { ...@@ -31,12 +29,10 @@ describe('Notebook component', () => {
}); });
describe('with JSON', () => { describe('with JSON', () => {
beforeEach((done) => { beforeEach(() => {
vm = buildComponent(json); vm = buildComponent(json);
setImmediate(() => { return nextTick();
done();
});
}); });
it('renders cells', () => { it('renders cells', () => {
...@@ -57,12 +53,10 @@ describe('Notebook component', () => { ...@@ -57,12 +53,10 @@ describe('Notebook component', () => {
}); });
describe('with worksheets', () => { describe('with worksheets', () => {
beforeEach((done) => { beforeEach(() => {
vm = buildComponent(jsonWithWorksheet); vm = buildComponent(jsonWithWorksheet);
setImmediate(() => { return nextTick();
done();
});
}); });
it('renders cells', () => { it('renders cells', () => {
......
...@@ -5,6 +5,7 @@ import $ from 'jquery'; ...@@ -5,6 +5,7 @@ import $ from 'jquery';
import '~/behaviors/markdown/render_gfm'; import '~/behaviors/markdown/render_gfm';
import { createSpyObj } from 'helpers/jest_helpers'; import { createSpyObj } from 'helpers/jest_helpers';
import { TEST_HOST } from 'helpers/test_constants'; import { TEST_HOST } from 'helpers/test_constants';
import waitForPromises from 'helpers/wait_for_promises';
import { setTestTimeoutOnce } from 'helpers/timeout'; import { setTestTimeoutOnce } from 'helpers/timeout';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import * as urlUtility from '~/lib/utils/url_utility'; import * as urlUtility from '~/lib/utils/url_utility';
...@@ -549,15 +550,14 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => { ...@@ -549,15 +550,14 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => {
expect($notesContainer.find('.note.being-posted').length).toBeGreaterThan(0); expect($notesContainer.find('.note.being-posted').length).toBeGreaterThan(0);
}); });
it('should remove placeholder note when new comment is done posting', (done) => { it('should remove placeholder note when new comment is done posting', async () => {
mockNotesPost(); mockNotesPost();
$('.js-comment-button').click(); $('.js-comment-button').click();
setImmediate(() => { await waitForPromises();
expect($notesContainer.find('.note.being-posted').length).toEqual(0); expect($notesContainer.find('.note.being-posted').length).toEqual(0);
done();
});
}); });
describe('postComment', () => { describe('postComment', () => {
...@@ -584,40 +584,37 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => { ...@@ -584,40 +584,37 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => {
}); });
}); });
it('should show actual note element when new comment is done posting', (done) => { it('should show actual note element when new comment is done posting', async () => {
mockNotesPost(); mockNotesPost();
$('.js-comment-button').click(); $('.js-comment-button').click();
setImmediate(() => { await waitForPromises();
expect($notesContainer.find(`#note_${note.id}`).length).toBeGreaterThan(0); expect($notesContainer.find(`#note_${note.id}`).length).toBeGreaterThan(0);
done();
});
}); });
it('should reset Form when new comment is done posting', (done) => { it('should reset Form when new comment is done posting', async () => {
mockNotesPost(); mockNotesPost();
$('.js-comment-button').click(); $('.js-comment-button').click();
setImmediate(() => { await waitForPromises();
expect($form.find('textarea.js-note-text').val()).toEqual(''); expect($form.find('textarea.js-note-text').val()).toEqual('');
done();
});
}); });
it('should show flash error message when new comment failed to be posted', (done) => { it('should show flash error message when new comment failed to be posted', async () => {
mockNotesPostError(); mockNotesPostError();
jest.spyOn(notes, 'addFlash'); jest.spyOn(notes, 'addFlash');
$('.js-comment-button').click(); $('.js-comment-button').click();
setImmediate(() => { await waitForPromises();
expect(notes.addFlash).toHaveBeenCalled(); expect(notes.addFlash).toHaveBeenCalled();
// JSDom doesn't support the :visible selector yet // JSDom doesn't support the :visible selector yet
expect(notes.flashContainer.style.display).not.toBe('none'); expect(notes.flashContainer.style.display).not.toBe('none');
done();
});
}); });
}); });
...@@ -657,16 +654,15 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => { ...@@ -657,16 +654,15 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => {
$form.find('textarea.js-note-text').val(sampleComment); $form.find('textarea.js-note-text').val(sampleComment);
}); });
it('should remove quick action placeholder when comment with quick actions is done posting', (done) => { it('should remove quick action placeholder when comment with quick actions is done posting', async () => {
jest.spyOn(gl.awardsHandler, 'addAwardToEmojiBar'); jest.spyOn(gl.awardsHandler, 'addAwardToEmojiBar');
$('.js-comment-button').click(); $('.js-comment-button').click();
expect($notesContainer.find('.note.being-posted').length).toEqual(1); // Placeholder shown expect($notesContainer.find('.note.being-posted').length).toEqual(1); // Placeholder shown
setImmediate(() => { await waitForPromises();
expect($notesContainer.find('.note.being-posted').length).toEqual(0); // Placeholder removed expect($notesContainer.find('.note.being-posted').length).toEqual(0); // Placeholder removed
done();
});
}); });
}); });
...@@ -692,16 +688,15 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => { ...@@ -692,16 +688,15 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => {
$form.find('textarea.js-note-text').val(sampleComment); $form.find('textarea.js-note-text').val(sampleComment);
}); });
it('should show message placeholder including lines starting with slash', (done) => { it('should show message placeholder including lines starting with slash', async () => {
$('.js-comment-button').click(); $('.js-comment-button').click();
expect($notesContainer.find('.note.being-posted').length).toEqual(1); // Placeholder shown expect($notesContainer.find('.note.being-posted').length).toEqual(1); // Placeholder shown
expect($notesContainer.find('.note-body p').text()).toEqual(sampleComment); // No quick action processing expect($notesContainer.find('.note-body p').text()).toEqual(sampleComment); // No quick action processing
setImmediate(() => { await waitForPromises();
expect($notesContainer.find('.note.being-posted').length).toEqual(0); // Placeholder removed expect($notesContainer.find('.note.being-posted').length).toEqual(0); // Placeholder removed
done();
});
}); });
}); });
...@@ -730,10 +725,11 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => { ...@@ -730,10 +725,11 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => {
$form.find('textarea.js-note-text').html(sampleComment); $form.find('textarea.js-note-text').html(sampleComment);
}); });
it('should not render a script tag', (done) => { it('should not render a script tag', async () => {
$('.js-comment-button').click(); $('.js-comment-button').click();
setImmediate(() => { await waitForPromises();
const $noteEl = $notesContainer.find(`#note_${note.id}`); const $noteEl = $notesContainer.find(`#note_${note.id}`);
$noteEl.find('.js-note-edit').click(); $noteEl.find('.js-note-edit').click();
$noteEl.find('textarea.js-note-text').html(updatedComment); $noteEl.find('textarea.js-note-text').html(updatedComment);
...@@ -744,9 +740,6 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => { ...@@ -744,9 +740,6 @@ describe.skip('Old Notes (~/deprecated_notes.js)', () => {
.find('.js-task-list-container'); .find('.js-task-list-container');
expect($updatedNoteEl.find('.note-text').text().trim()).toEqual(''); expect($updatedNoteEl.find('.note-text').text().trim()).toEqual('');
done();
});
}); });
}); });
......
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import $ from 'jquery'; import $ from 'jquery';
import { TEST_HOST } from 'helpers/test_constants'; import { TEST_HOST } from 'helpers/test_constants';
import waitForPromises from 'helpers/wait_for_promises';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { removeParams } from '~/lib/utils/url_utility'; import { removeParams } from '~/lib/utils/url_utility';
import Pager from '~/pager'; import Pager from '~/pager';
...@@ -64,51 +65,46 @@ describe('pager', () => { ...@@ -64,51 +65,46 @@ describe('pager', () => {
Pager.init(); Pager.init();
}); });
it('shows loader while loading next page', (done) => { it('shows loader while loading next page', async () => {
mockSuccess(); mockSuccess();
jest.spyOn(Pager.loading, 'show').mockImplementation(() => {}); jest.spyOn(Pager.loading, 'show').mockImplementation(() => {});
Pager.getOld(); Pager.getOld();
setImmediate(() => { await waitForPromises();
expect(Pager.loading.show).toHaveBeenCalled();
done(); expect(Pager.loading.show).toHaveBeenCalled();
});
}); });
it('hides loader on success', (done) => { it('hides loader on success', async () => {
mockSuccess(); mockSuccess();
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {}); jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {});
Pager.getOld(); Pager.getOld();
setImmediate(() => { await waitForPromises();
expect(Pager.loading.hide).toHaveBeenCalled();
done(); expect(Pager.loading.hide).toHaveBeenCalled();
});
}); });
it('hides loader on error', (done) => { it('hides loader on error', async () => {
mockError(); mockError();
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {}); jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {});
Pager.getOld(); Pager.getOld();
setImmediate(() => { await waitForPromises();
expect(Pager.loading.hide).toHaveBeenCalled();
done(); expect(Pager.loading.hide).toHaveBeenCalled();
});
}); });
it('sends request to url with offset and limit params', (done) => { it('sends request to url with offset and limit params', async () => {
Pager.offset = 100; Pager.offset = 100;
Pager.limit = 20; Pager.limit = 20;
Pager.getOld(); Pager.getOld();
setImmediate(() => { await waitForPromises();
const [url, params] = axios.get.mock.calls[0]; const [url, params] = axios.get.mock.calls[0];
expect(params).toEqual({ expect(params).toEqual({
...@@ -119,12 +115,9 @@ describe('pager', () => { ...@@ -119,12 +115,9 @@ describe('pager', () => {
}); });
expect(url).toBe('/some_list'); expect(url).toBe('/some_list');
done();
});
}); });
it('disables if return count is less than limit', (done) => { it('disables if return count is less than limit', async () => {
Pager.offset = 0; Pager.offset = 0;
Pager.limit = 20; Pager.limit = 20;
...@@ -132,12 +125,10 @@ describe('pager', () => { ...@@ -132,12 +125,10 @@ describe('pager', () => {
jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {}); jest.spyOn(Pager.loading, 'hide').mockImplementation(() => {});
Pager.getOld(); Pager.getOld();
setImmediate(() => { await waitForPromises();
expect(Pager.loading.hide).toHaveBeenCalled(); expect(Pager.loading.hide).toHaveBeenCalled();
expect(Pager.disable).toBe(true); expect(Pager.disable).toBe(true);
done();
});
}); });
describe('has data-href attribute from list element', () => { describe('has data-href attribute from list element', () => {
......
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