Commit c0dca9d2 authored by Phil Hughes's avatar Phil Hughes

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

Remove setImmediate usage in specs

See merge request gitlab-org/gitlab!80367
parents a91a7f94 95f5d578
import { getByRole } from '@testing-library/dom'; import { getByRole } from '@testing-library/dom';
import Vue, { nextTick } from 'vue'; import Vue, { nextTick } from 'vue';
import mountComponent from 'helpers/vue_mount_component_helper'; import mountComponent from 'helpers/vue_mount_component_helper';
import waitForPromises from 'helpers/wait_for_promises';
import { OPEN_REVERT_MODAL, OPEN_CHERRY_PICK_MODAL } from '~/projects/commit/constants'; import { OPEN_REVERT_MODAL, OPEN_CHERRY_PICK_MODAL } from '~/projects/commit/constants';
import modalEventHub from '~/projects/commit/event_hub'; import modalEventHub from '~/projects/commit/event_hub';
import mergedComponent from '~/vue_merge_request_widget/components/states/mr_widget_merged.vue'; import mergedComponent from '~/vue_merge_request_widget/components/states/mr_widget_merged.vue';
...@@ -127,7 +128,7 @@ describe('MRWidgetMerged', () => { ...@@ -127,7 +128,7 @@ describe('MRWidgetMerged', () => {
describe('methods', () => { describe('methods', () => {
describe('removeSourceBranch', () => { describe('removeSourceBranch', () => {
it('should set flag and call service then request main component to update the widget', (done) => { it('should set flag and call service then request main component to update the widget', async () => {
jest.spyOn(vm.service, 'removeSourceBranch').mockReturnValue( jest.spyOn(vm.service, 'removeSourceBranch').mockReturnValue(
new Promise((resolve) => { new Promise((resolve) => {
resolve({ resolve({
...@@ -139,14 +140,14 @@ describe('MRWidgetMerged', () => { ...@@ -139,14 +140,14 @@ describe('MRWidgetMerged', () => {
); );
vm.removeSourceBranch(); vm.removeSourceBranch();
setImmediate(() => {
await waitForPromises();
const args = eventHub.$emit.mock.calls[0]; const args = eventHub.$emit.mock.calls[0];
expect(vm.isMakingRequest).toEqual(true); expect(vm.isMakingRequest).toEqual(true);
expect(args[0]).toEqual('MRWidgetUpdateRequested'); expect(args[0]).toEqual('MRWidgetUpdateRequested');
expect(args[1]).not.toThrow(); expect(args[1]).not.toThrow();
done();
});
}); });
}); });
}); });
......
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue'; import { nextTick } from 'vue';
import { GlSprintf } from '@gitlab/ui'; import { GlSprintf } from '@gitlab/ui';
import waitForPromises from 'helpers/wait_for_promises';
import simplePoll from '~/lib/utils/simple_poll'; import simplePoll from '~/lib/utils/simple_poll';
import CommitEdit from '~/vue_merge_request_widget/components/states/commit_edit.vue'; import CommitEdit from '~/vue_merge_request_widget/components/states/commit_edit.vue';
import CommitMessageDropdown from '~/vue_merge_request_widget/components/states/commit_message_dropdown.vue'; import CommitMessageDropdown from '~/vue_merge_request_widget/components/states/commit_message_dropdown.vue';
...@@ -261,29 +262,27 @@ describe('ReadyToMerge', () => { ...@@ -261,29 +262,27 @@ describe('ReadyToMerge', () => {
describe('methods', () => { describe('methods', () => {
describe('handleMergeButtonClick', () => { describe('handleMergeButtonClick', () => {
const returnPromise = (status) => const response = (status) => ({
new Promise((resolve) => {
resolve({
data: { data: {
status, status,
}, },
}); });
});
it('should handle merge when pipeline succeeds', (done) => { it('should handle merge when pipeline succeeds', async () => {
createComponent(); createComponent();
jest.spyOn(eventHub, '$emit').mockImplementation(() => {}); jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
jest jest
.spyOn(wrapper.vm.service, 'merge') .spyOn(wrapper.vm.service, 'merge')
.mockReturnValue(returnPromise('merge_when_pipeline_succeeds')); .mockResolvedValue(response('merge_when_pipeline_succeeds'));
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details // setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
wrapper.setData({ removeSourceBranch: false }); wrapper.setData({ removeSourceBranch: false });
wrapper.vm.handleMergeButtonClick(true); wrapper.vm.handleMergeButtonClick(true);
setImmediate(() => { await waitForPromises();
expect(wrapper.vm.isMakingRequest).toBeTruthy(); expect(wrapper.vm.isMakingRequest).toBeTruthy();
expect(eventHub.$emit).toHaveBeenCalledWith('MRWidgetUpdateRequested'); expect(eventHub.$emit).toHaveBeenCalledWith('MRWidgetUpdateRequested');
expect(eventHub.$emit).toHaveBeenCalledWith('StateMachineValueChanged', { expect(eventHub.$emit).toHaveBeenCalledWith('StateMachineValueChanged', {
...@@ -300,18 +299,17 @@ describe('ReadyToMerge', () => { ...@@ -300,18 +299,17 @@ describe('ReadyToMerge', () => {
auto_merge_strategy: 'merge_when_pipeline_succeeds', auto_merge_strategy: 'merge_when_pipeline_succeeds',
}), }),
); );
done();
});
}); });
it('should handle merge failed', (done) => { it('should handle merge failed', async () => {
createComponent(); createComponent();
jest.spyOn(eventHub, '$emit').mockImplementation(() => {}); jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
jest.spyOn(wrapper.vm.service, 'merge').mockReturnValue(returnPromise('failed')); jest.spyOn(wrapper.vm.service, 'merge').mockResolvedValue(response('failed'));
wrapper.vm.handleMergeButtonClick(false, true); wrapper.vm.handleMergeButtonClick(false, true);
setImmediate(() => { await waitForPromises();
expect(wrapper.vm.isMakingRequest).toBeTruthy(); expect(wrapper.vm.isMakingRequest).toBeTruthy();
expect(eventHub.$emit).toHaveBeenCalledWith('FailedToMerge', undefined); expect(eventHub.$emit).toHaveBeenCalledWith('FailedToMerge', undefined);
...@@ -319,15 +317,13 @@ describe('ReadyToMerge', () => { ...@@ -319,15 +317,13 @@ describe('ReadyToMerge', () => {
expect(params.should_remove_source_branch).toBeTruthy(); expect(params.should_remove_source_branch).toBeTruthy();
expect(params.auto_merge_strategy).toBeUndefined(); expect(params.auto_merge_strategy).toBeUndefined();
done();
});
}); });
it('should handle merge action accepted case', (done) => { it('should handle merge action accepted case', async () => {
createComponent(); createComponent();
jest.spyOn(eventHub, '$emit').mockImplementation(() => {}); jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
jest.spyOn(wrapper.vm.service, 'merge').mockReturnValue(returnPromise('success')); jest.spyOn(wrapper.vm.service, 'merge').mockResolvedValue(response('success'));
jest.spyOn(wrapper.vm.mr, 'transitionStateMachine'); jest.spyOn(wrapper.vm.mr, 'transitionStateMachine');
wrapper.vm.handleMergeButtonClick(); wrapper.vm.handleMergeButtonClick();
...@@ -335,7 +331,8 @@ describe('ReadyToMerge', () => { ...@@ -335,7 +331,8 @@ describe('ReadyToMerge', () => {
transition: 'start-merge', transition: 'start-merge',
}); });
setImmediate(() => { await waitForPromises();
expect(wrapper.vm.isMakingRequest).toBeTruthy(); expect(wrapper.vm.isMakingRequest).toBeTruthy();
expect(wrapper.vm.mr.transitionStateMachine).toHaveBeenCalledWith({ expect(wrapper.vm.mr.transitionStateMachine).toHaveBeenCalledWith({
transition: 'start-merge', transition: 'start-merge',
...@@ -345,8 +342,6 @@ describe('ReadyToMerge', () => { ...@@ -345,8 +342,6 @@ describe('ReadyToMerge', () => {
expect(params.should_remove_source_branch).toBeTruthy(); expect(params.should_remove_source_branch).toBeTruthy();
expect(params.auto_merge_strategy).toBeUndefined(); expect(params.auto_merge_strategy).toBeUndefined();
done();
});
}); });
}); });
...@@ -364,20 +359,17 @@ describe('ReadyToMerge', () => { ...@@ -364,20 +359,17 @@ describe('ReadyToMerge', () => {
}); });
describe('handleRemoveBranchPolling', () => { describe('handleRemoveBranchPolling', () => {
const returnPromise = (state) => const response = (state) => ({
new Promise((resolve) => {
resolve({
data: { data: {
source_branch_exists: state, source_branch_exists: state,
}, },
}); });
});
it('should call start and stop polling when MR merged', (done) => { it('should call start and stop polling when MR merged', async () => {
createComponent(); createComponent();
jest.spyOn(eventHub, '$emit').mockImplementation(() => {}); jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
jest.spyOn(wrapper.vm.service, 'poll').mockReturnValue(returnPromise(false)); jest.spyOn(wrapper.vm.service, 'poll').mockResolvedValue(response(false));
let cpc = false; // continuePollingCalled let cpc = false; // continuePollingCalled
let spc = false; // stopPollingCalled let spc = false; // stopPollingCalled
...@@ -390,7 +382,9 @@ describe('ReadyToMerge', () => { ...@@ -390,7 +382,9 @@ describe('ReadyToMerge', () => {
spc = true; spc = true;
}, },
); );
setImmediate(() => {
await waitForPromises();
expect(wrapper.vm.service.poll).toHaveBeenCalled(); expect(wrapper.vm.service.poll).toHaveBeenCalled();
const args = eventHub.$emit.mock.calls[0]; const args = eventHub.$emit.mock.calls[0];
...@@ -403,15 +397,12 @@ describe('ReadyToMerge', () => { ...@@ -403,15 +397,12 @@ describe('ReadyToMerge', () => {
expect(cpc).toBeFalsy(); expect(cpc).toBeFalsy();
expect(spc).toBeTruthy(); expect(spc).toBeTruthy();
done();
});
}); });
it('should continue polling until MR is merged', (done) => { it('should continue polling until MR is merged', async () => {
createComponent(); createComponent();
jest.spyOn(wrapper.vm.service, 'poll').mockReturnValue(returnPromise(true)); jest.spyOn(wrapper.vm.service, 'poll').mockResolvedValue(response(true));
let cpc = false; // continuePollingCalled let cpc = false; // continuePollingCalled
let spc = false; // stopPollingCalled let spc = false; // stopPollingCalled
...@@ -424,12 +415,11 @@ describe('ReadyToMerge', () => { ...@@ -424,12 +415,11 @@ describe('ReadyToMerge', () => {
spc = true; spc = true;
}, },
); );
setImmediate(() => {
await waitForPromises();
expect(cpc).toBeTruthy(); expect(cpc).toBeTruthy();
expect(spc).toBeFalsy(); expect(spc).toBeFalsy();
done();
});
}); });
}); });
}); });
......
import Vue, { nextTick } from 'vue'; import Vue, { nextTick } from 'vue';
import waitForPromises from 'helpers/wait_for_promises';
import WorkInProgress from '~/vue_merge_request_widget/components/states/work_in_progress.vue'; import WorkInProgress from '~/vue_merge_request_widget/components/states/work_in_progress.vue';
import toast from '~/vue_shared/plugins/global_toast'; import toast from '~/vue_shared/plugins/global_toast';
import eventHub from '~/vue_merge_request_widget/event_hub'; import eventHub from '~/vue_merge_request_widget/event_hub';
...@@ -47,7 +48,7 @@ describe('Wip', () => { ...@@ -47,7 +48,7 @@ describe('Wip', () => {
}; };
describe('handleRemoveDraft', () => { describe('handleRemoveDraft', () => {
it('should make a request to service and handle response', (done) => { it('should make a request to service and handle response', async () => {
const vm = createComponent(); const vm = createComponent();
jest.spyOn(eventHub, '$emit').mockImplementation(() => {}); jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
...@@ -60,12 +61,12 @@ describe('Wip', () => { ...@@ -60,12 +61,12 @@ describe('Wip', () => {
); );
vm.handleRemoveDraft(); vm.handleRemoveDraft();
setImmediate(() => {
await waitForPromises();
expect(vm.isMakingRequest).toBeTruthy(); expect(vm.isMakingRequest).toBeTruthy();
expect(eventHub.$emit).toHaveBeenCalledWith('UpdateWidgetData', mrObj); expect(eventHub.$emit).toHaveBeenCalledWith('UpdateWidgetData', mrObj);
expect(toast).toHaveBeenCalledWith('Marked as ready. Merging is now allowed.'); expect(toast).toHaveBeenCalledWith('Marked as ready. Merging is now allowed.');
done();
});
}); });
}); });
}); });
...@@ -91,13 +92,12 @@ describe('Wip', () => { ...@@ -91,13 +92,12 @@ describe('Wip', () => {
); );
}); });
it('should not show removeWIP button is user cannot update MR', (done) => { it('should not show removeWIP button is user cannot update MR', async () => {
vm.mr.removeWIPPath = ''; vm.mr.removeWIPPath = '';
nextTick(() => { await nextTick();
expect(el.querySelector('.js-remove-draft')).toEqual(null); expect(el.querySelector('.js-remove-draft')).toEqual(null);
done();
});
}); });
}); });
}); });
...@@ -176,17 +176,18 @@ describe('AlertManagementStatus', () => { ...@@ -176,17 +176,18 @@ describe('AlertManagementStatus', () => {
jest.spyOn(Tracking, 'event'); jest.spyOn(Tracking, 'event');
}); });
it('should not track alert status updates when the tracking options do not exist', () => { it('should not track alert status updates when the tracking options do not exist', async () => {
mountComponent({}); mountComponent({});
Tracking.event.mockClear(); Tracking.event.mockClear();
jest.spyOn(wrapper.vm.$apollo, 'mutate').mockResolvedValue({}); jest.spyOn(wrapper.vm.$apollo, 'mutate').mockResolvedValue({});
findFirstStatusOption().vm.$emit('click'); findFirstStatusOption().vm.$emit('click');
setImmediate(() => {
await nextTick();
expect(Tracking.event).not.toHaveBeenCalled(); expect(Tracking.event).not.toHaveBeenCalled();
}); });
});
it('should track alert status updates when the tracking options exist', () => { it('should track alert status updates when the tracking options exist', async () => {
const trackAlertStatusUpdateOptions = { const trackAlertStatusUpdateOptions = {
category: 'Alert Management', category: 'Alert Management',
action: 'update_alert_status', action: 'update_alert_status',
...@@ -196,11 +197,12 @@ describe('AlertManagementStatus', () => { ...@@ -196,11 +197,12 @@ describe('AlertManagementStatus', () => {
Tracking.event.mockClear(); Tracking.event.mockClear();
jest.spyOn(wrapper.vm.$apollo, 'mutate').mockResolvedValue({}); jest.spyOn(wrapper.vm.$apollo, 'mutate').mockResolvedValue({});
findFirstStatusOption().vm.$emit('click'); findFirstStatusOption().vm.$emit('click');
await nextTick();
const status = findFirstStatusOption().text(); const status = findFirstStatusOption().text();
setImmediate(() => {
const { category, action, label } = trackAlertStatusUpdateOptions; const { category, action, label } = trackAlertStatusUpdateOptions;
expect(Tracking.event).toHaveBeenCalledWith(category, action, { label, property: status }); expect(Tracking.event).toHaveBeenCalledWith(category, action, { label, property: status });
}); });
}); });
});
}); });
import Vue from 'vue'; import Vue, { nextTick } from 'vue';
import mountComponent from 'helpers/vue_mount_component_helper'; import mountComponent from 'helpers/vue_mount_component_helper';
import { GREEN_BOX_IMAGE_URL, RED_BOX_IMAGE_URL } from 'spec/test_constants'; import { GREEN_BOX_IMAGE_URL, RED_BOX_IMAGE_URL } from 'spec/test_constants';
...@@ -26,14 +26,15 @@ describe('DiffViewer', () => { ...@@ -26,14 +26,15 @@ describe('DiffViewer', () => {
vm.$destroy(); vm.$destroy();
}); });
it('renders image diff', (done) => { it('renders image diff', async () => {
window.gon = { window.gon = {
relative_url_root: '', relative_url_root: '',
}; };
createComponent({ ...requiredProps, projectPath: '' }); createComponent({ ...requiredProps, projectPath: '' });
setImmediate(() => { await nextTick();
expect(vm.$el.querySelector('.deleted img').getAttribute('src')).toBe( expect(vm.$el.querySelector('.deleted img').getAttribute('src')).toBe(
`//-/raw/DEF/${RED_BOX_IMAGE_URL}`, `//-/raw/DEF/${RED_BOX_IMAGE_URL}`,
); );
...@@ -41,12 +42,9 @@ describe('DiffViewer', () => { ...@@ -41,12 +42,9 @@ describe('DiffViewer', () => {
expect(vm.$el.querySelector('.added img').getAttribute('src')).toBe( expect(vm.$el.querySelector('.added img').getAttribute('src')).toBe(
`//-/raw/ABC/${GREEN_BOX_IMAGE_URL}`, `//-/raw/ABC/${GREEN_BOX_IMAGE_URL}`,
); );
done();
});
}); });
it('renders fallback download diff display', (done) => { it('renders fallback download diff display', async () => {
createComponent({ createComponent({
...requiredProps, ...requiredProps,
diffViewerMode: 'added', diffViewerMode: 'added',
...@@ -54,10 +52,9 @@ describe('DiffViewer', () => { ...@@ -54,10 +52,9 @@ describe('DiffViewer', () => {
oldPath: 'testold.abc', oldPath: 'testold.abc',
}); });
setImmediate(() => { await nextTick();
expect(vm.$el.querySelector('.deleted .file-info').textContent.trim()).toContain(
'testold.abc', expect(vm.$el.querySelector('.deleted .file-info').textContent.trim()).toContain('testold.abc');
);
expect(vm.$el.querySelector('.deleted .btn.btn-default').textContent.trim()).toContain( expect(vm.$el.querySelector('.deleted .btn.btn-default').textContent.trim()).toContain(
'Download', 'Download',
...@@ -67,9 +64,6 @@ describe('DiffViewer', () => { ...@@ -67,9 +64,6 @@ describe('DiffViewer', () => {
expect(vm.$el.querySelector('.added .btn.btn-default').textContent.trim()).toContain( expect(vm.$el.querySelector('.added .btn.btn-default').textContent.trim()).toContain(
'Download', 'Download',
); );
done();
});
}); });
describe('renamed file', () => { describe('renamed file', () => {
......
...@@ -75,33 +75,29 @@ describe('ImageDiffViewer', () => { ...@@ -75,33 +75,29 @@ describe('ImageDiffViewer', () => {
expect(metaInfoElements[1]).toHaveText('1.00 KiB'); expect(metaInfoElements[1]).toHaveText('1.00 KiB');
}); });
it('renders image diff for new', (done) => { it('renders image diff for new', async () => {
createComponent({ ...allProps, diffMode: 'new', oldPath: '' }); createComponent({ ...allProps, diffMode: 'new', oldPath: '' });
setImmediate(() => { await nextTick();
const metaInfoElement = vm.$el.querySelector('.image-info'); const metaInfoElement = vm.$el.querySelector('.image-info');
expect(vm.$el.querySelector('.added img').getAttribute('src')).toBe(GREEN_BOX_IMAGE_URL); expect(vm.$el.querySelector('.added img').getAttribute('src')).toBe(GREEN_BOX_IMAGE_URL);
expect(metaInfoElement).toHaveText('1.00 KiB'); expect(metaInfoElement).toHaveText('1.00 KiB');
done();
});
}); });
it('renders image diff for deleted', (done) => { it('renders image diff for deleted', async () => {
createComponent({ ...allProps, diffMode: 'deleted', newPath: '' }); createComponent({ ...allProps, diffMode: 'deleted', newPath: '' });
setImmediate(() => { await nextTick();
const metaInfoElement = vm.$el.querySelector('.image-info'); const metaInfoElement = vm.$el.querySelector('.image-info');
expect(vm.$el.querySelector('.deleted img').getAttribute('src')).toBe(RED_BOX_IMAGE_URL); expect(vm.$el.querySelector('.deleted img').getAttribute('src')).toBe(RED_BOX_IMAGE_URL);
expect(metaInfoElement).toHaveText('2.00 KiB'); expect(metaInfoElement).toHaveText('2.00 KiB');
done();
});
}); });
it('renders image diff for renamed', (done) => { it('renders image diff for renamed', async () => {
vm = new Vue({ vm = new Vue({
components: { components: {
imageDiffViewer, imageDiffViewer,
...@@ -127,25 +123,21 @@ describe('ImageDiffViewer', () => { ...@@ -127,25 +123,21 @@ describe('ImageDiffViewer', () => {
`), `),
}).$mount(); }).$mount();
setImmediate(() => { await nextTick();
const metaInfoElement = vm.$el.querySelector('.image-info'); const metaInfoElement = vm.$el.querySelector('.image-info');
expect(vm.$el.querySelector('img').getAttribute('src')).toBe(GREEN_BOX_IMAGE_URL); expect(vm.$el.querySelector('img').getAttribute('src')).toBe(GREEN_BOX_IMAGE_URL);
expect(vm.$el.querySelector('.overlay')).not.toBe(null); expect(vm.$el.querySelector('.overlay')).not.toBe(null);
expect(metaInfoElement).toHaveText('2.00 KiB'); expect(metaInfoElement).toHaveText('2.00 KiB');
done();
});
}); });
describe('swipeMode', () => { describe('swipeMode', () => {
beforeEach((done) => { beforeEach(() => {
createComponent({ ...requiredProps }); createComponent({ ...requiredProps });
setImmediate(() => { return nextTick();
done();
});
}); });
it('switches to Swipe Mode', async () => { it('switches to Swipe Mode', async () => {
...@@ -157,12 +149,10 @@ describe('ImageDiffViewer', () => { ...@@ -157,12 +149,10 @@ describe('ImageDiffViewer', () => {
}); });
describe('onionSkin', () => { describe('onionSkin', () => {
beforeEach((done) => { beforeEach(() => {
createComponent({ ...requiredProps }); createComponent({ ...requiredProps });
setImmediate(() => { return nextTick();
done();
});
}); });
it('switches to Onion Skin Mode', async () => { it('switches to Onion Skin Mode', async () => {
......
import Mousetrap from 'mousetrap'; import Mousetrap from 'mousetrap';
import Vue, { nextTick } from 'vue'; import Vue, { nextTick } from 'vue';
import waitForPromises from 'helpers/wait_for_promises';
import { file } from 'jest/ide/helpers'; import { file } from 'jest/ide/helpers';
import { UP_KEY_CODE, DOWN_KEY_CODE, ENTER_KEY_CODE, ESC_KEY_CODE } from '~/lib/utils/keycodes'; import { UP_KEY_CODE, DOWN_KEY_CODE, ENTER_KEY_CODE, ESC_KEY_CODE } from '~/lib/utils/keycodes';
import FindFileComponent from '~/vue_shared/components/file_finder/index.vue'; import FindFileComponent from '~/vue_shared/components/file_finder/index.vue';
...@@ -31,7 +30,7 @@ describe('File finder item spec', () => { ...@@ -31,7 +30,7 @@ describe('File finder item spec', () => {
}); });
describe('with entries', () => { describe('with entries', () => {
beforeEach((done) => { beforeEach(() => {
createComponent({ createComponent({
files: [ files: [
{ {
...@@ -48,7 +47,7 @@ describe('File finder item spec', () => { ...@@ -48,7 +47,7 @@ describe('File finder item spec', () => {
], ],
}); });
setImmediate(done); return nextTick();
}); });
it('renders list of blobs', () => { it('renders list of blobs', () => {
...@@ -57,68 +56,48 @@ describe('File finder item spec', () => { ...@@ -57,68 +56,48 @@ describe('File finder item spec', () => {
expect(vm.$el.textContent).not.toContain('folder'); expect(vm.$el.textContent).not.toContain('folder');
}); });
it('filters entries', (done) => { it('filters entries', async () => {
vm.searchText = 'index'; vm.searchText = 'index';
setImmediate(() => { await nextTick();
expect(vm.$el.textContent).toContain('index.js'); expect(vm.$el.textContent).toContain('index.js');
expect(vm.$el.textContent).not.toContain('component.js'); expect(vm.$el.textContent).not.toContain('component.js');
done();
});
}); });
it('shows clear button when searchText is not empty', (done) => { it('shows clear button when searchText is not empty', async () => {
vm.searchText = 'index'; vm.searchText = 'index';
setImmediate(() => { await nextTick();
expect(vm.$el.querySelector('.dropdown-input').classList).toContain('has-value'); expect(vm.$el.querySelector('.dropdown-input').classList).toContain('has-value');
expect(vm.$el.querySelector('.dropdown-input-search').classList).toContain('hidden'); expect(vm.$el.querySelector('.dropdown-input-search').classList).toContain('hidden');
done();
});
}); });
it('clear button resets searchText', (done) => { it('clear button resets searchText', async () => {
vm.searchText = 'index'; vm.searchText = 'index';
waitForPromises()
.then(() => {
vm.clearSearchInput(); vm.clearSearchInput();
})
.then(waitForPromises)
.then(() => {
expect(vm.searchText).toBe(''); expect(vm.searchText).toBe('');
})
.then(done)
.catch(done.fail);
}); });
it('clear button focuses search input', (done) => { it('clear button focuses search input', async () => {
jest.spyOn(vm.$refs.searchInput, 'focus').mockImplementation(() => {}); jest.spyOn(vm.$refs.searchInput, 'focus').mockImplementation(() => {});
vm.searchText = 'index'; vm.searchText = 'index';
waitForPromises()
.then(() => {
vm.clearSearchInput(); vm.clearSearchInput();
})
.then(waitForPromises) await nextTick();
.then(() => {
expect(vm.$refs.searchInput.focus).toHaveBeenCalled(); expect(vm.$refs.searchInput.focus).toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
}); });
describe('listShowCount', () => { describe('listShowCount', () => {
it('returns 1 when no filtered entries exist', (done) => { it('returns 1 when no filtered entries exist', () => {
vm.searchText = 'testing 123'; vm.searchText = 'testing 123';
setImmediate(() => {
expect(vm.listShowCount).toBe(1); expect(vm.listShowCount).toBe(1);
done();
});
}); });
it('returns entries length when not filtered', () => { it('returns entries length when not filtered', () => {
...@@ -131,26 +110,18 @@ describe('File finder item spec', () => { ...@@ -131,26 +110,18 @@ describe('File finder item spec', () => {
expect(vm.listHeight).toBe(55); expect(vm.listHeight).toBe(55);
}); });
it('returns 33 when entries dont exist', (done) => { it('returns 33 when entries dont exist', () => {
vm.searchText = 'testing 123'; vm.searchText = 'testing 123';
setImmediate(() => {
expect(vm.listHeight).toBe(33); expect(vm.listHeight).toBe(33);
done();
});
}); });
}); });
describe('filteredBlobsLength', () => { describe('filteredBlobsLength', () => {
it('returns length of filtered blobs', (done) => { it('returns length of filtered blobs', () => {
vm.searchText = 'index'; vm.searchText = 'index';
setImmediate(() => {
expect(vm.filteredBlobsLength).toBe(1); expect(vm.filteredBlobsLength).toBe(1);
done();
});
}); });
}); });
...@@ -158,7 +129,7 @@ describe('File finder item spec', () => { ...@@ -158,7 +129,7 @@ describe('File finder item spec', () => {
it('renders less DOM nodes if not visible by utilizing v-if', async () => { it('renders less DOM nodes if not visible by utilizing v-if', async () => {
vm.visible = false; vm.visible = false;
await waitForPromises(); await nextTick();
expect(vm.$el).toBeInstanceOf(Comment); expect(vm.$el).toBeInstanceOf(Comment);
}); });
...@@ -166,33 +137,24 @@ describe('File finder item spec', () => { ...@@ -166,33 +137,24 @@ describe('File finder item spec', () => {
describe('watches', () => { describe('watches', () => {
describe('searchText', () => { describe('searchText', () => {
it('resets focusedIndex when updated', (done) => { it('resets focusedIndex when updated', async () => {
vm.focusedIndex = 1; vm.focusedIndex = 1;
vm.searchText = 'test'; vm.searchText = 'test';
setImmediate(() => { await nextTick();
expect(vm.focusedIndex).toBe(0);
done(); expect(vm.focusedIndex).toBe(0);
});
}); });
}); });
describe('visible', () => { describe('visible', () => {
it('resets searchText when changed to false', (done) => { it('resets searchText when changed to false', async () => {
vm.searchText = 'test'; vm.searchText = 'test';
vm.visible = true;
waitForPromises()
.then(() => {
vm.visible = false; vm.visible = false;
})
.then(waitForPromises) await nextTick();
.then(() => {
expect(vm.searchText).toBe(''); expect(vm.searchText).toBe('');
})
.then(done)
.catch(done.fail);
}); });
}); });
}); });
...@@ -216,7 +178,7 @@ describe('File finder item spec', () => { ...@@ -216,7 +178,7 @@ describe('File finder item spec', () => {
}); });
describe('onKeyup', () => { describe('onKeyup', () => {
it('opens file on enter key', (done) => { it('opens file on enter key', async () => {
const event = new CustomEvent('keyup'); const event = new CustomEvent('keyup');
event.keyCode = ENTER_KEY_CODE; event.keyCode = ENTER_KEY_CODE;
...@@ -224,14 +186,12 @@ describe('File finder item spec', () => { ...@@ -224,14 +186,12 @@ describe('File finder item spec', () => {
vm.$refs.searchInput.dispatchEvent(event); vm.$refs.searchInput.dispatchEvent(event);
setImmediate(() => { await nextTick();
expect(vm.openFile).toHaveBeenCalledWith(vm.files[0]);
done(); expect(vm.openFile).toHaveBeenCalledWith(vm.files[0]);
});
}); });
it('closes file finder on esc key', (done) => { it('closes file finder on esc key', async () => {
const event = new CustomEvent('keyup'); const event = new CustomEvent('keyup');
event.keyCode = ESC_KEY_CODE; event.keyCode = ESC_KEY_CODE;
...@@ -239,11 +199,9 @@ describe('File finder item spec', () => { ...@@ -239,11 +199,9 @@ describe('File finder item spec', () => {
vm.$refs.searchInput.dispatchEvent(event); vm.$refs.searchInput.dispatchEvent(event);
setImmediate(() => { await nextTick();
expect(vm.$emit).toHaveBeenCalledWith('toggle', false);
done(); expect(vm.$emit).toHaveBeenCalledWith('toggle', false);
});
}); });
}); });
......
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