Commit 461c8f20 authored by Andrew Fontaine's avatar Andrew Fontaine

Merge branch '194238-migrate-some-karma-specs-in-ide-commit-sidebar' into 'master'

Migrate some ide/components/commit_sidebar to Jest

See merge request gitlab-org/gitlab!28714
parents 74e1447d ba4c4fd1
import Vue from 'vue'; import Vue from 'vue';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import { projectData, branches } from 'spec/ide/mock_data'; import { projectData, branches } from 'jest/ide/mock_data';
import { createStore } from '~/ide/stores'; import { createStore } from '~/ide/stores';
import commitActions from '~/ide/components/commit_sidebar/actions.vue'; import commitActions from '~/ide/components/commit_sidebar/actions.vue';
import consts from '~/ide/stores/modules/commit/constants'; import consts from '~/ide/stores/modules/commit/constants';
const ACTION_UPDATE_COMMIT_ACTION = 'commit/updateCommitAction'; const ACTION_UPDATE_COMMIT_ACTION = 'commit/updateCommitAction';
const BRANCH_DEFAULT = 'master';
const BRANCH_PROTECTED = 'protected/access';
const BRANCH_PROTECTED_NO_ACCESS = 'protected/no-access';
const BRANCH_REGULAR = 'regular';
const BRANCH_REGULAR_NO_ACCESS = 'regular/no-access';
describe('IDE commit sidebar actions', () => { describe('IDE commit sidebar actions', () => {
let store; let store;
let vm; let vm;
...@@ -38,7 +44,7 @@ describe('IDE commit sidebar actions', () => { ...@@ -38,7 +44,7 @@ describe('IDE commit sidebar actions', () => {
beforeEach(() => { beforeEach(() => {
store = createStore(); store = createStore();
spyOn(store, 'dispatch'); jest.spyOn(store, 'dispatch').mockImplementation(() => {});
}); });
afterEach(() => { afterEach(() => {
...@@ -103,133 +109,33 @@ describe('IDE commit sidebar actions', () => { ...@@ -103,133 +109,33 @@ describe('IDE commit sidebar actions', () => {
.then(() => { .then(() => {
expect(vm.$store.dispatch).toHaveBeenCalledWith( expect(vm.$store.dispatch).toHaveBeenCalledWith(
ACTION_UPDATE_COMMIT_ACTION, ACTION_UPDATE_COMMIT_ACTION,
jasmine.anything(), expect.anything(),
); );
}) })
.then(done) .then(done)
.catch(done.fail); .catch(done.fail);
}); });
describe('default branch', () => { it.each`
it('dispatches correct action for default branch', () => { input | expectedOption
createComponent({ ${{ currentBranchId: BRANCH_DEFAULT }} | ${consts.COMMIT_TO_NEW_BRANCH}
currentBranchId: 'master', ${{ currentBranchId: BRANCH_PROTECTED, hasMR: true }} | ${consts.COMMIT_TO_CURRENT_BRANCH}
}); ${{ currentBranchId: BRANCH_PROTECTED, hasMR: false }} | ${consts.COMMIT_TO_CURRENT_BRANCH}
${{ currentBranchId: BRANCH_PROTECTED_NO_ACCESS, hasMR: true }} | ${consts.COMMIT_TO_NEW_BRANCH}
expect(vm.$store.dispatch).toHaveBeenCalledTimes(1); ${{ currentBranchId: BRANCH_PROTECTED_NO_ACCESS, hasMR: false }} | ${consts.COMMIT_TO_NEW_BRANCH}
expect(vm.$store.dispatch).toHaveBeenCalledWith( ${{ currentBranchId: BRANCH_REGULAR, hasMR: true }} | ${consts.COMMIT_TO_CURRENT_BRANCH}
ACTION_UPDATE_COMMIT_ACTION, ${{ currentBranchId: BRANCH_REGULAR, hasMR: false }} | ${consts.COMMIT_TO_CURRENT_BRANCH}
consts.COMMIT_TO_NEW_BRANCH, ${{ currentBranchId: BRANCH_REGULAR_NO_ACCESS, hasMR: true }} | ${consts.COMMIT_TO_NEW_BRANCH}
); ${{ currentBranchId: BRANCH_REGULAR_NO_ACCESS, hasMR: false }} | ${consts.COMMIT_TO_NEW_BRANCH}
}); `(
}); 'with $input, it dispatches update commit action with $expectedOption',
({ input, expectedOption }) => {
describe('protected branch', () => { createComponent(input);
describe('with write access', () => {
it('dispatches correct action when MR exists', () => { expect(vm.$store.dispatch.mock.calls).toEqual([
createComponent({ [ACTION_UPDATE_COMMIT_ACTION, expectedOption],
hasMR: true, ]);
currentBranchId: 'protected/access', },
}); );
expect(vm.$store.dispatch).toHaveBeenCalledWith(
ACTION_UPDATE_COMMIT_ACTION,
consts.COMMIT_TO_CURRENT_BRANCH,
);
});
it('dispatches correct action when MR does not exists', () => {
createComponent({
hasMR: false,
currentBranchId: 'protected/access',
});
expect(vm.$store.dispatch).toHaveBeenCalledWith(
ACTION_UPDATE_COMMIT_ACTION,
consts.COMMIT_TO_CURRENT_BRANCH,
);
});
});
describe('without write access', () => {
it('dispatches correct action when MR exists', () => {
createComponent({
hasMR: true,
currentBranchId: 'protected/no-access',
});
expect(vm.$store.dispatch).toHaveBeenCalledWith(
ACTION_UPDATE_COMMIT_ACTION,
consts.COMMIT_TO_NEW_BRANCH,
);
});
it('dispatches correct action when MR does not exists', () => {
createComponent({
hasMR: false,
currentBranchId: 'protected/no-access',
});
expect(vm.$store.dispatch).toHaveBeenCalledWith(
ACTION_UPDATE_COMMIT_ACTION,
consts.COMMIT_TO_NEW_BRANCH,
);
});
});
});
describe('regular branch', () => {
describe('with write access', () => {
it('dispatches correct action when MR exists', () => {
createComponent({
hasMR: true,
currentBranchId: 'regular',
});
expect(vm.$store.dispatch).toHaveBeenCalledWith(
ACTION_UPDATE_COMMIT_ACTION,
consts.COMMIT_TO_CURRENT_BRANCH,
);
});
it('dispatches correct action when MR does not exists', () => {
createComponent({
hasMR: false,
currentBranchId: 'regular',
});
expect(vm.$store.dispatch).toHaveBeenCalledWith(
ACTION_UPDATE_COMMIT_ACTION,
consts.COMMIT_TO_CURRENT_BRANCH,
);
});
});
describe('without write access', () => {
it('dispatches correct action when MR exists', () => {
createComponent({
hasMR: true,
currentBranchId: 'regular/no-access',
});
expect(vm.$store.dispatch).toHaveBeenCalledWith(
ACTION_UPDATE_COMMIT_ACTION,
consts.COMMIT_TO_NEW_BRANCH,
);
});
it('dispatches correct action when MR does not exists', () => {
createComponent({
hasMR: false,
currentBranchId: 'regular/no-access',
});
expect(vm.$store.dispatch).toHaveBeenCalledWith(
ACTION_UPDATE_COMMIT_ACTION,
consts.COMMIT_TO_NEW_BRANCH,
);
});
});
});
}); });
}); });
import Vue from 'vue'; import Vue from 'vue';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import getSetTimeoutPromise from 'spec/helpers/set_timeout_promise_helper'; import waitForPromises from 'helpers/wait_for_promises';
import { projectData } from 'spec/ide/mock_data'; import { projectData } from 'jest/ide/mock_data';
import store from '~/ide/stores'; import store from '~/ide/stores';
import CommitForm from '~/ide/components/commit_sidebar/form.vue'; import CommitForm from '~/ide/components/commit_sidebar/form.vue';
import { leftSidebarViews } from '~/ide/constants'; import { leftSidebarViews } from '~/ide/constants';
...@@ -12,8 +12,6 @@ describe('IDE commit form', () => { ...@@ -12,8 +12,6 @@ describe('IDE commit form', () => {
let vm; let vm;
beforeEach(() => { beforeEach(() => {
spyOnProperty(window, 'innerHeight').and.returnValue(800);
store.state.changedFiles.push('test'); store.state.changedFiles.push('test');
store.state.currentProjectId = 'abcproject'; store.state.currentProjectId = 'abcproject';
store.state.currentBranchId = 'master'; store.state.currentBranchId = 'master';
...@@ -111,7 +109,7 @@ describe('IDE commit form', () => { ...@@ -111,7 +109,7 @@ describe('IDE commit form', () => {
textarea.dispatchEvent(new Event('input')); textarea.dispatchEvent(new Event('input'));
getSetTimeoutPromise() waitForPromises()
.then(() => { .then(() => {
expect(vm.$store.state.commit.commitMessage).toBe('testing commit message'); expect(vm.$store.state.commit.commitMessage).toBe('testing commit message');
}) })
...@@ -148,7 +146,7 @@ describe('IDE commit form', () => { ...@@ -148,7 +146,7 @@ describe('IDE commit form', () => {
it('resets commitMessage when clicking discard button', done => { it('resets commitMessage when clicking discard button', done => {
vm.$store.state.commit.commitMessage = 'testing commit message'; vm.$store.state.commit.commitMessage = 'testing commit message';
getSetTimeoutPromise() waitForPromises()
.then(() => { .then(() => {
vm.$el.querySelector('.btn-default').click(); vm.$el.querySelector('.btn-default').click();
}) })
...@@ -163,14 +161,14 @@ describe('IDE commit form', () => { ...@@ -163,14 +161,14 @@ describe('IDE commit form', () => {
describe('when submitting', () => { describe('when submitting', () => {
beforeEach(() => { beforeEach(() => {
spyOn(vm, 'commitChanges'); jest.spyOn(vm, 'commitChanges').mockImplementation(() => {});
vm.$store.state.stagedFiles.push('test'); vm.$store.state.stagedFiles.push('test');
}); });
it('calls commitChanges', done => { it('calls commitChanges', done => {
vm.$store.state.commit.commitMessage = 'testing commit message'; vm.$store.state.commit.commitMessage = 'testing commit message';
getSetTimeoutPromise() waitForPromises()
.then(() => { .then(() => {
vm.$el.querySelector('.btn-success').click(); vm.$el.querySelector('.btn-success').click();
}) })
......
import Vue from 'vue'; import Vue from 'vue';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import store from '~/ide/stores'; import store from '~/ide/stores';
import listCollapsed from '~/ide/components/commit_sidebar/list_collapsed.vue'; import listCollapsed from '~/ide/components/commit_sidebar/list_collapsed.vue';
import { file } from '../../helpers'; import { file } from '../../helpers';
......
import Vue from 'vue'; import Vue from 'vue';
import { trimText } from 'spec/helpers/text_helper'; import { trimText } from 'helpers/text_helper';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import store from '~/ide/stores'; import store from '~/ide/stores';
import listItem from '~/ide/components/commit_sidebar/list_item.vue'; import listItem from '~/ide/components/commit_sidebar/list_item.vue';
import router from '~/ide/ide_router'; import router from '~/ide/ide_router';
...@@ -61,12 +61,12 @@ describe('Multi-file editor commit sidebar list item', () => { ...@@ -61,12 +61,12 @@ describe('Multi-file editor commit sidebar list item', () => {
}); });
it('opens a closed file in the editor when clicking the file path', done => { it('opens a closed file in the editor when clicking the file path', done => {
spyOn(vm, 'openPendingTab').and.callThrough(); jest.spyOn(vm, 'openPendingTab');
spyOn(router, 'push'); jest.spyOn(router, 'push').mockImplementation(() => {});
findPathEl.click(); findPathEl.click();
setTimeout(() => { setImmediate(() => {
expect(vm.openPendingTab).toHaveBeenCalled(); expect(vm.openPendingTab).toHaveBeenCalled();
expect(router.push).toHaveBeenCalled(); expect(router.push).toHaveBeenCalled();
...@@ -75,13 +75,13 @@ describe('Multi-file editor commit sidebar list item', () => { ...@@ -75,13 +75,13 @@ describe('Multi-file editor commit sidebar list item', () => {
}); });
it('calls updateViewer with diff when clicking file', done => { it('calls updateViewer with diff when clicking file', done => {
spyOn(vm, 'openFileInEditor').and.callThrough(); jest.spyOn(vm, 'openFileInEditor');
spyOn(vm, 'updateViewer').and.callThrough(); jest.spyOn(vm, 'updateViewer');
spyOn(router, 'push'); jest.spyOn(router, 'push').mockImplementation(() => {});
findPathEl.click(); findPathEl.click();
setTimeout(() => { setImmediate(() => {
expect(vm.updateViewer).toHaveBeenCalledWith('diff'); expect(vm.updateViewer).toHaveBeenCalledWith('diff');
done(); done();
......
import Vue from 'vue'; import Vue from 'vue';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import store from '~/ide/stores'; import store from '~/ide/stores';
import commitSidebarList from '~/ide/components/commit_sidebar/list.vue'; import commitSidebarList from '~/ide/components/commit_sidebar/list.vue';
import { file, resetStore } from '../../helpers'; import { file, resetStore } from '../../helpers';
......
import Vue from 'vue'; import Vue from 'vue';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { createComponentWithStore } from 'helpers/vue_mount_component_helper';
import { projectData, branches } from 'spec/ide/mock_data'; import { projectData, branches } from 'jest/ide/mock_data';
import NewMergeRequestOption from '~/ide/components/commit_sidebar/new_merge_request_option.vue'; import NewMergeRequestOption from '~/ide/components/commit_sidebar/new_merge_request_option.vue';
import { createStore } from '~/ide/stores'; import { createStore } from '~/ide/stores';
import { PERMISSION_CREATE_MR } from '~/ide/constants'; import { PERMISSION_CREATE_MR } from '~/ide/constants';
...@@ -200,11 +200,11 @@ describe('create new MR checkbox', () => { ...@@ -200,11 +200,11 @@ describe('create new MR checkbox', () => {
currentBranchId: 'regular', currentBranchId: 'regular',
}); });
const el = vm.$el.querySelector('input[type="checkbox"]'); const el = vm.$el.querySelector('input[type="checkbox"]');
spyOn(vm.$store, 'dispatch'); jest.spyOn(vm.$store, 'dispatch').mockImplementation(() => {});
el.dispatchEvent(new Event('change')); el.dispatchEvent(new Event('change'));
expect(vm.$store.dispatch.calls.allArgs()).toEqual( expect(vm.$store.dispatch.mock.calls).toEqual(
jasmine.arrayContaining([['commit/toggleShouldCreateMR', jasmine.any(Object)]]), expect.arrayContaining([['commit/toggleShouldCreateMR', expect.any(Object)]]),
); );
}); });
}); });
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