Commit 95565a0b authored by Jacques Erasmus's avatar Jacques Erasmus

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

Remove setImmediate usage in specs

See merge request gitlab-org/gitlab!80372
parents a4ec91a7 c1a738ba
......@@ -5,6 +5,7 @@ import { nextTick } from 'vue';
import EpicHeader from 'ee/epic/components/epic_header.vue';
import { statusType } from 'ee/epic/constants';
import createStore from 'ee/epic/store';
import waitForPromises from 'helpers/wait_for_promises';
import TimeagoTooltip from '~/vue_shared/components/time_ago_tooltip.vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';
......@@ -157,7 +158,7 @@ describe('EpicHeaderComponent', () => {
store.state.author.isGitlabEmployee = true;
// Wait for dynamic imports to resolve
await new Promise(setImmediate);
await waitForPromises();
expect(wrapper.vm.$refs.gitlabTeamMemberBadge).not.toBeUndefined();
});
......
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
import waitForPromises from 'helpers/wait_for_promises';
import GitlabTeamMemberBadge from 'ee/vue_shared/components/user_avatar/badges/gitlab_team_member_badge.vue';
import NoteHeader from '~/notes/components/note_header.vue';
......@@ -37,13 +38,13 @@ describe('NoteHeader component', () => {
${{ author }} | ${false} | ${"doesn't render"} | ${'undefined'}
`(
'$message1 GitLab team member badge when `is_gitlab_employee` is $message2',
({ props, expected }) => {
async ({ props, expected }) => {
createComponent(props);
// Wait for dynamic imports to resolve
return new Promise(setImmediate).then(() => {
expect(wrapper.findComponent(GitlabTeamMemberBadge).exists()).toBe(expected);
});
await waitForPromises();
expect(wrapper.findComponent(GitlabTeamMemberBadge).exists()).toBe(expected);
},
);
});
import waitForPromises from 'helpers/wait_for_promises';
import initCopyAsGFM, { CopyAsGFM } from '~/behaviors/markdown/copy_as_gfm';
describe('CopyAsGFM', () => {
......@@ -81,49 +82,40 @@ describe('CopyAsGFM', () => {
stopPropagation() {},
};
CopyAsGFM.copyAsGFM(e, CopyAsGFM.transformGFMSelection);
return clipboardData;
return waitForPromises();
};
beforeAll((done) => {
beforeAll(() => {
initCopyAsGFM();
// Fake call to nodeToGfm so the import of lazy bundle happened
CopyAsGFM.nodeToGFM(document.createElement('div'))
.then(() => {
done();
})
.catch(done.fail);
return CopyAsGFM.nodeToGFM(document.createElement('div'));
});
beforeEach(() => jest.spyOn(clipboardData, 'setData'));
describe('list handling', () => {
it('uses correct gfm for unordered lists', (done) => {
it('uses correct gfm for unordered lists', async () => {
const selection = stubSelection('<li>List Item1</li><li>List Item2</li>\n', 'UL');
window.getSelection = jest.fn(() => selection);
simulateCopy();
await simulateCopy();
setImmediate(() => {
const expectedGFM = '* List Item1\n* List Item2';
const expectedGFM = '* List Item1\n* List Item2';
expect(clipboardData.setData).toHaveBeenCalledWith('text/x-gfm', expectedGFM);
done();
});
expect(clipboardData.setData).toHaveBeenCalledWith('text/x-gfm', expectedGFM);
});
it('uses correct gfm for ordered lists', (done) => {
it('uses correct gfm for ordered lists', async () => {
const selection = stubSelection('<li>List Item1</li><li>List Item2</li>\n', 'OL');
window.getSelection = jest.fn(() => selection);
simulateCopy();
await simulateCopy();
setImmediate(() => {
const expectedGFM = '1. List Item1\n1. List Item2';
const expectedGFM = '1. List Item1\n1. List Item2';
expect(clipboardData.setData).toHaveBeenCalledWith('text/x-gfm', expectedGFM);
done();
});
expect(clipboardData.setData).toHaveBeenCalledWith('text/x-gfm', expectedGFM);
});
});
});
......@@ -131,10 +123,9 @@ describe('CopyAsGFM', () => {
describe('CopyAsGFM.quoted', () => {
const sampleGFM = '* List 1\n* List 2\n\n`Some code`';
it('adds quote char `> ` to each line', (done) => {
it('adds quote char `> ` to each line', () => {
const expectedQuotedGFM = '> * List 1\n> * List 2\n> \n> `Some code`';
expect(CopyAsGFM.quoted(sampleGFM)).toEqual(expectedQuotedGFM);
done();
});
});
});
import $ from 'jquery';
import Mousetrap from 'mousetrap';
import waitForPromises from 'helpers/wait_for_promises';
import initCopyAsGFM, { CopyAsGFM } from '~/behaviors/markdown/copy_as_gfm';
import ShortcutsIssuable from '~/behaviors/shortcuts/shortcuts_issuable';
import { getSelectedFragment } from '~/lib/utils/common_utils';
......@@ -13,15 +14,11 @@ describe('ShortcutsIssuable', () => {
const snippetShowFixtureName = 'snippets/show.html';
const mrShowFixtureName = 'merge_requests/merge_request_of_current_user.html';
beforeAll((done) => {
beforeAll(() => {
initCopyAsGFM();
// Fake call to nodeToGfm so the import of lazy bundle happened
CopyAsGFM.nodeToGFM(document.createElement('div'))
.then(() => {
done();
})
.catch(done.fail);
return CopyAsGFM.nodeToGFM(document.createElement('div'));
});
describe('replyWithSelectedText', () => {
......@@ -79,22 +76,18 @@ describe('ShortcutsIssuable', () => {
stubSelection('<p>Selected text.</p>');
});
it('leaves existing input intact', (done) => {
it('leaves existing input intact', async () => {
$(FORM_SELECTOR).val('This text was already here.');
expect($(FORM_SELECTOR).val()).toBe('This text was already here.');
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect($(FORM_SELECTOR).val()).toBe(
'This text was already here.\n\n> Selected text.\n\n',
);
done();
});
await waitForPromises();
expect($(FORM_SELECTOR).val()).toBe('This text was already here.\n\n> Selected text.\n\n');
});
it('triggers `input`', (done) => {
it('triggers `input`', async () => {
let triggered = false;
$(FORM_SELECTOR).on('input', () => {
triggered = true;
......@@ -102,48 +95,40 @@ describe('ShortcutsIssuable', () => {
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect(triggered).toBe(true);
done();
});
await waitForPromises();
expect(triggered).toBe(true);
});
it('triggers `focus`', (done) => {
it('triggers `focus`', async () => {
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect(spy).toHaveBeenCalled();
done();
});
await waitForPromises();
expect(spy).toHaveBeenCalled();
});
});
describe('with a one-line selection', () => {
it('quotes the selection', (done) => {
it('quotes the selection', async () => {
stubSelection('<p>This text has been selected.</p>');
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect($(FORM_SELECTOR).val()).toBe('> This text has been selected.\n\n');
done();
});
await waitForPromises();
expect($(FORM_SELECTOR).val()).toBe('> This text has been selected.\n\n');
});
});
describe('with a multi-line selection', () => {
it('quotes the selected lines as a group', (done) => {
it('quotes the selected lines as a group', async () => {
stubSelection(
'<p>Selected line one.</p>\n<p>Selected line two.</p>\n<p>Selected line three.</p>',
);
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect($(FORM_SELECTOR).val()).toBe(
'> Selected line one.\n>\n> Selected line two.\n>\n> Selected line three.\n\n',
);
done();
});
await waitForPromises();
expect($(FORM_SELECTOR).val()).toBe(
'> Selected line one.\n>\n> Selected line two.\n>\n> Selected line three.\n\n',
);
});
});
......@@ -152,23 +137,19 @@ describe('ShortcutsIssuable', () => {
stubSelection('<p>Selected text.</p>', true);
});
it('does not add anything to the input', (done) => {
it('does not add anything to the input', async () => {
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect($(FORM_SELECTOR).val()).toBe('');
done();
});
await waitForPromises();
expect($(FORM_SELECTOR).val()).toBe('');
});
it('triggers `focus`', (done) => {
it('triggers `focus`', async () => {
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect(spy).toHaveBeenCalled();
done();
});
await waitForPromises();
expect(spy).toHaveBeenCalled();
});
});
......@@ -177,26 +158,22 @@ describe('ShortcutsIssuable', () => {
stubSelection('<div class="md">Selected text.</div><p>Invalid selected text.</p>', true);
});
it('only adds the valid part to the input', (done) => {
it('only adds the valid part to the input', async () => {
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect($(FORM_SELECTOR).val()).toBe('> Selected text.\n\n');
done();
});
await waitForPromises();
expect($(FORM_SELECTOR).val()).toBe('> Selected text.\n\n');
});
it('triggers `focus`', (done) => {
it('triggers `focus`', async () => {
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect(spy).toHaveBeenCalled();
done();
});
await waitForPromises();
expect(spy).toHaveBeenCalled();
});
it('triggers `input`', (done) => {
it('triggers `input`', async () => {
let triggered = false;
$(FORM_SELECTOR).on('input', () => {
triggered = true;
......@@ -204,10 +181,8 @@ describe('ShortcutsIssuable', () => {
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect(triggered).toBe(true);
done();
});
await waitForPromises();
expect(triggered).toBe(true);
});
});
......@@ -231,26 +206,22 @@ describe('ShortcutsIssuable', () => {
});
});
it('adds the quoted selection to the input', (done) => {
it('adds the quoted selection to the input', async () => {
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect($(FORM_SELECTOR).val()).toBe('> *Selected text.*\n\n');
done();
});
await waitForPromises();
expect($(FORM_SELECTOR).val()).toBe('> *Selected text.*\n\n');
});
it('triggers `focus`', (done) => {
it('triggers `focus`', async () => {
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect(spy).toHaveBeenCalled();
done();
});
await waitForPromises();
expect(spy).toHaveBeenCalled();
});
it('triggers `input`', (done) => {
it('triggers `input`', async () => {
let triggered = false;
$(FORM_SELECTOR).on('input', () => {
triggered = true;
......@@ -258,10 +229,8 @@ describe('ShortcutsIssuable', () => {
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect(triggered).toBe(true);
done();
});
await waitForPromises();
expect(triggered).toBe(true);
});
});
......@@ -285,36 +254,29 @@ describe('ShortcutsIssuable', () => {
});
});
it('does not add anything to the input', (done) => {
it('does not add anything to the input', async () => {
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect($(FORM_SELECTOR).val()).toBe('');
done();
});
await waitForPromises();
expect($(FORM_SELECTOR).val()).toBe('');
});
it('triggers `focus`', (done) => {
it('triggers `focus`', async () => {
const spy = jest.spyOn(document.querySelector(FORM_SELECTOR), 'focus');
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect(spy).toHaveBeenCalled();
done();
});
await waitForPromises();
expect(spy).toHaveBeenCalled();
});
});
describe('with a valid selection with no text content', () => {
it('returns the proper markdown', (done) => {
it('returns the proper markdown', async () => {
stubSelection('<img src="https://gitlab.com/logo.png" alt="logo" />');
ShortcutsIssuable.replyWithSelectedText(true);
setImmediate(() => {
expect($(FORM_SELECTOR).val()).toBe('> ![logo](https://gitlab.com/logo.png)\n\n');
done();
});
await waitForPromises();
expect($(FORM_SELECTOR).val()).toBe('> ![logo](https://gitlab.com/logo.png)\n\n');
});
});
});
......
......@@ -41,34 +41,30 @@ describe('Blob viewer', () => {
window.location.hash = '';
});
it('loads source file after switching views', (done) => {
it('loads source file after switching views', async () => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
setImmediate(() => {
expect(
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
.classList.contains('hidden'),
).toBeFalsy();
await axios.waitForAll();
done();
});
expect(
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
.classList.contains('hidden'),
).toBeFalsy();
});
it('loads source file when line number is in hash', (done) => {
it('loads source file when line number is in hash', async () => {
window.location.hash = '#L1';
new BlobViewer();
setImmediate(() => {
expect(
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
.classList.contains('hidden'),
).toBeFalsy();
await axios.waitForAll();
done();
});
expect(
document
.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]')
.classList.contains('hidden'),
).toBeFalsy();
});
it('doesnt reload file if already loaded', () => {
......@@ -123,24 +119,20 @@ describe('Blob viewer', () => {
expect(copyButton.blur).not.toHaveBeenCalled();
});
it('enables after switching to simple view', (done) => {
it('enables after switching to simple view', async () => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
setImmediate(() => {
expect(copyButton.classList.contains('disabled')).toBeFalsy();
await axios.waitForAll();
done();
});
expect(copyButton.classList.contains('disabled')).toBeFalsy();
});
it('updates tooltip after switching to simple view', (done) => {
it('updates tooltip after switching to simple view', async () => {
document.querySelector('.js-blob-viewer-switch-btn[data-viewer="simple"]').click();
setImmediate(() => {
expect(copyButtonTooltip.getAttribute('title')).toBe('Copy file contents');
await axios.waitForAll();
done();
});
expect(copyButtonTooltip.getAttribute('title')).toBe('Copy file contents');
});
});
......
import { nextTick } from 'vue';
import { TEST_HOST } from 'helpers/test_constants';
import initConfirmModal from '~/confirm_modal';
......@@ -50,7 +49,6 @@ describe('ConfirmModal', () => {
const findModal = () => document.querySelector('.gl-modal');
const findModalOkButton = (modal, variant) =>
modal.querySelector(`.modal-footer .btn-${variant}`);
const findModalCancelButton = (modal) => modal.querySelector('.modal-footer .btn-secondary');
const modalIsHidden = () => findModal() === null;
const serializeModal = (modal, buttonIndex) => {
......@@ -90,19 +88,6 @@ describe('ConfirmModal', () => {
expect(findModal()).not.toBe(null);
expect(modalIsHidden()).toBe(false);
});
describe('Cancel Button', () => {
beforeEach(async () => {
findModalCancelButton(findModal()).click();
await nextTick();
});
it('closes the modal', () => {
setImmediate(() => {
expect(modalIsHidden()).toBe(true);
});
});
});
});
});
......
......@@ -544,10 +544,8 @@ describe('DiffsStoreActions', () => {
[{ type: types.SET_DIFF_VIEW_TYPE, payload: INLINE_DIFF_VIEW_TYPE }],
[],
() => {
setImmediate(() => {
expect(Cookies.get('diff_view')).toEqual(INLINE_DIFF_VIEW_TYPE);
done();
});
expect(Cookies.get('diff_view')).toEqual(INLINE_DIFF_VIEW_TYPE);
done();
},
);
});
......@@ -562,10 +560,8 @@ describe('DiffsStoreActions', () => {
[{ type: types.SET_DIFF_VIEW_TYPE, payload: PARALLEL_DIFF_VIEW_TYPE }],
[],
() => {
setImmediate(() => {
expect(Cookies.get(DIFF_VIEW_COOKIE_NAME)).toEqual(PARALLEL_DIFF_VIEW_TYPE);
done();
});
expect(Cookies.get(DIFF_VIEW_COOKIE_NAME)).toEqual(PARALLEL_DIFF_VIEW_TYPE);
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