Commit 9e87c561 authored by Paul Slaughter's avatar Paul Slaughter

Add spec for openPendingTab for repo_commit_section

- Also migrates to Jest and VTU
parent 7a2fa72c
import Vue from 'vue'; import { mount } from '@vue/test-utils';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { createStore } from '~/ide/stores';
import store from '~/ide/stores';
import router from '~/ide/ide_router'; import router from '~/ide/ide_router';
import repoCommitSection from '~/ide/components/repo_commit_section.vue'; import RepoCommitSection from '~/ide/components/repo_commit_section.vue';
import { file, resetStore } from '../helpers'; import { stageKeys } from '~/ide/constants';
import { file } from '../helpers';
const TEST_NO_CHANGES_SVG = 'nochangessvg';
describe('RepoCommitSection', () => { describe('RepoCommitSection', () => {
let vm; let wrapper;
let store;
function createComponent() { function createComponent() {
const Component = Vue.extend(repoCommitSection); wrapper = mount(RepoCommitSection, { store });
}
function setupDefaultState() {
store.state.noChangesStateSvgPath = 'svg'; store.state.noChangesStateSvgPath = 'svg';
store.state.committedStateSvgPath = 'commitsvg'; store.state.committedStateSvgPath = 'commitsvg';
store.state.currentProjectId = 'abcproject';
vm = createComponentWithStore(Component, store); store.state.currentBranchId = 'master';
store.state.projects.abcproject = {
vm.$store.state.currentProjectId = 'abcproject';
vm.$store.state.currentBranchId = 'master';
vm.$store.state.projects.abcproject = {
web_url: '', web_url: '',
branches: { branches: {
master: { master: {
...@@ -34,79 +36,98 @@ describe('RepoCommitSection', () => { ...@@ -34,79 +36,98 @@ describe('RepoCommitSection', () => {
}), }),
); );
vm.$store.state.rightPanelCollapsed = false; store.state.rightPanelCollapsed = false;
vm.$store.state.currentBranch = 'master'; store.state.currentBranch = 'master';
vm.$store.state.changedFiles = []; store.state.changedFiles = [];
vm.$store.state.stagedFiles = [{ ...files[0] }, { ...files[1] }]; store.state.stagedFiles = [{ ...files[0] }, { ...files[1] }];
vm.$store.state.stagedFiles.forEach(f => store.state.stagedFiles.forEach(f =>
Object.assign(f, { Object.assign(f, {
changed: true, changed: true,
staged: true,
content: 'testing', content: 'testing',
}), }),
); );
files.forEach(f => { files.forEach(f => {
vm.$store.state.entries[f.path] = f; store.state.entries[f.path] = f;
}); });
return vm;
} }
beforeEach(done => { beforeEach(() => {
spyOn(router, 'push'); store = createStore();
vm = createComponent();
spyOn(vm, 'openPendingTab').and.callThrough();
vm.$mount();
Vue.nextTick(done); jest.spyOn(store, 'dispatch');
jest.spyOn(router, 'push').mockImplementation();
}); });
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
resetStore(vm.$store);
}); });
describe('empty Stage', () => { describe('empty Stage', () => {
it('renders no changes text', () => { beforeEach(() => {
resetStore(vm.$store); store.state.noChangesStateSvgPath = TEST_NO_CHANGES_SVG;
const Component = Vue.extend(repoCommitSection);
store.state.noChangesStateSvgPath = 'nochangessvg';
store.state.committedStateSvgPath = 'svg'; store.state.committedStateSvgPath = 'svg';
vm.$destroy(); createComponent();
vm = createComponentWithStore(Component, store).$mount(); });
expect(vm.$el.querySelector('.js-empty-state').textContent.trim()).toContain('No changes'); it('renders no changes text', () => {
expect(vm.$el.querySelector('.js-empty-state img').getAttribute('src')).toBe('nochangessvg'); expect(
wrapper
.find('.js-empty-state')
.text()
.trim(),
).toContain('No changes');
expect(wrapper.find('.js-empty-state img').attributes('src')).toBe(TEST_NO_CHANGES_SVG);
}); });
}); });
it('renders a commit section', () => { describe('default', () => {
const changedFileElements = [...vm.$el.querySelectorAll('.multi-file-commit-list > li')]; beforeEach(() => {
const allFiles = vm.$store.state.changedFiles.concat(vm.$store.state.stagedFiles); setupDefaultState();
expect(changedFileElements).toHaveLength(2);
changedFileElements.forEach((changedFile, i) => { createComponent();
expect(changedFile.textContent.trim()).toContain(allFiles[i].path);
});
}); });
describe('mounted', () => {
it('opens last opened file', () => { it('opens last opened file', () => {
expect(store.state.openFiles.length).toBe(1); expect(store.state.openFiles.length).toBe(1);
expect(store.state.openFiles[0].pending).toBe(true); expect(store.state.openFiles[0].pending).toBe(true);
}); });
it('calls openPendingTab', () => { it('calls openPendingTab', () => {
expect(vm.openPendingTab).toHaveBeenCalledWith({ expect(store.dispatch).toHaveBeenCalledWith('openPendingTab', {
file: vm.lastOpenedFile, file: store.getters.lastOpenedFile,
keyPrefix: 'unstaged', keyPrefix: stageKeys.staged,
});
});
it('renders a commit section', () => {
const allFiles = store.state.changedFiles.concat(store.state.stagedFiles);
const changedFileNames = wrapper
.findAll('.multi-file-commit-list > li')
.wrappers.map(x => x.text().trim());
expect(changedFileNames).toEqual(allFiles.map(x => x.path));
});
});
describe('with unstaged file', () => {
beforeEach(() => {
setupDefaultState();
store.state.changedFiles = store.state.stagedFiles.map(x =>
Object.assign(x, { staged: false }),
);
store.state.stagedFiles = [];
createComponent();
});
it('calls openPendingTab with unstaged prefix', () => {
expect(store.dispatch).toHaveBeenCalledWith('openPendingTab', {
file: store.getters.lastOpenedFile,
keyPrefix: stageKeys.unstaged,
}); });
}); });
}); });
......
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