2020-04-14 21:09:52 +00:00
|
|
|
import { mount } from '@vue/test-utils';
|
2021-01-15 00:10:45 +00:00
|
|
|
import { keepAlive } from 'helpers/keep_alive_component_helper';
|
2020-05-27 18:08:14 +00:00
|
|
|
import EmptyState from '~/ide/components/commit_sidebar/empty_state.vue';
|
2021-02-14 18:09:20 +00:00
|
|
|
import RepoCommitSection from '~/ide/components/repo_commit_section.vue';
|
2020-04-14 21:09:52 +00:00
|
|
|
import { stageKeys } from '~/ide/constants';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { createRouter } from '~/ide/ide_router';
|
|
|
|
import { createStore } from '~/ide/stores';
|
2020-04-14 21:09:52 +00:00
|
|
|
import { file } from '../helpers';
|
|
|
|
|
|
|
|
const TEST_NO_CHANGES_SVG = 'nochangessvg';
|
|
|
|
|
|
|
|
describe('RepoCommitSection', () => {
|
|
|
|
let wrapper;
|
2020-06-05 00:08:38 +00:00
|
|
|
let router;
|
2020-04-14 21:09:52 +00:00
|
|
|
let store;
|
|
|
|
|
|
|
|
function createComponent() {
|
2020-09-25 15:09:36 +00:00
|
|
|
wrapper = mount(keepAlive(RepoCommitSection), { store });
|
2020-04-14 21:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setupDefaultState() {
|
|
|
|
store.state.noChangesStateSvgPath = 'svg';
|
|
|
|
store.state.committedStateSvgPath = 'commitsvg';
|
|
|
|
store.state.currentProjectId = 'abcproject';
|
2021-06-15 00:10:11 +00:00
|
|
|
store.state.currentBranchId = 'main';
|
2020-04-14 21:09:52 +00:00
|
|
|
store.state.projects.abcproject = {
|
|
|
|
web_url: '',
|
|
|
|
branches: {
|
2021-06-15 00:10:11 +00:00
|
|
|
main: {
|
2020-04-14 21:09:52 +00:00
|
|
|
workingReference: '1',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
const files = [file('file1'), file('file2')].map((f) =>
|
2020-04-14 21:09:52 +00:00
|
|
|
Object.assign(f, {
|
|
|
|
type: 'blob',
|
|
|
|
content: 'orginal content',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2021-06-15 00:10:11 +00:00
|
|
|
store.state.currentBranch = 'main';
|
2020-04-14 21:09:52 +00:00
|
|
|
store.state.changedFiles = [];
|
|
|
|
store.state.stagedFiles = [{ ...files[0] }, { ...files[1] }];
|
2020-12-23 21:10:24 +00:00
|
|
|
store.state.stagedFiles.forEach((f) =>
|
2020-04-14 21:09:52 +00:00
|
|
|
Object.assign(f, {
|
|
|
|
changed: true,
|
|
|
|
staged: true,
|
|
|
|
content: 'testing',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
files.forEach((f) => {
|
2020-04-14 21:09:52 +00:00
|
|
|
store.state.entries[f.path] = f;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
store = createStore();
|
2020-06-05 00:08:38 +00:00
|
|
|
router = createRouter(store);
|
2020-04-14 21:09:52 +00:00
|
|
|
|
|
|
|
jest.spyOn(store, 'dispatch');
|
|
|
|
jest.spyOn(router, 'push').mockImplementation();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
2020-09-25 15:09:36 +00:00
|
|
|
wrapper = null;
|
2020-04-14 21:09:52 +00:00
|
|
|
});
|
|
|
|
|
2020-05-27 18:08:14 +00:00
|
|
|
describe('empty state', () => {
|
2020-04-14 21:09:52 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
store.state.noChangesStateSvgPath = TEST_NO_CHANGES_SVG;
|
|
|
|
store.state.committedStateSvgPath = 'svg';
|
|
|
|
|
|
|
|
createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders no changes text', () => {
|
2020-12-23 12:10:26 +00:00
|
|
|
expect(wrapper.find(EmptyState).text().trim()).toContain('No changes');
|
|
|
|
expect(wrapper.find(EmptyState).find('img').attributes('src')).toBe(TEST_NO_CHANGES_SVG);
|
2020-04-14 21:09:52 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('default', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setupDefaultState();
|
|
|
|
|
|
|
|
createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('opens last opened file', () => {
|
|
|
|
expect(store.state.openFiles.length).toBe(1);
|
|
|
|
expect(store.state.openFiles[0].pending).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls openPendingTab', () => {
|
|
|
|
expect(store.dispatch).toHaveBeenCalledWith('openPendingTab', {
|
|
|
|
file: store.getters.lastOpenedFile,
|
|
|
|
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')
|
2020-12-23 21:10:24 +00:00
|
|
|
.wrappers.map((x) => x.text().trim());
|
2020-04-14 21:09:52 +00:00
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
expect(changedFileNames).toEqual(allFiles.map((x) => x.path));
|
2020-04-14 21:09:52 +00:00
|
|
|
});
|
2020-05-27 18:08:14 +00:00
|
|
|
|
|
|
|
it('does not show empty state', () => {
|
|
|
|
expect(wrapper.find(EmptyState).exists()).toBe(false);
|
|
|
|
});
|
2020-04-14 21:09:52 +00:00
|
|
|
});
|
|
|
|
|
2020-06-11 09:08:16 +00:00
|
|
|
describe('if nothing is changed or staged', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setupDefaultState();
|
|
|
|
|
|
|
|
store.state.openFiles = [...Object.values(store.state.entries)];
|
|
|
|
store.state.openFiles[0].active = true;
|
|
|
|
store.state.stagedFiles = [];
|
|
|
|
|
|
|
|
createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('opens currently active file', () => {
|
|
|
|
expect(store.state.openFiles.length).toBe(1);
|
|
|
|
expect(store.state.openFiles[0].pending).toBe(true);
|
|
|
|
|
|
|
|
expect(store.dispatch).toHaveBeenCalledWith('openPendingTab', {
|
|
|
|
file: store.state.entries[store.getters.activeFile.path],
|
|
|
|
keyPrefix: stageKeys.unstaged,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-14 21:09:52 +00:00
|
|
|
describe('with unstaged file', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setupDefaultState();
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
store.state.changedFiles = store.state.stagedFiles.map((x) =>
|
2020-04-14 21:09:52 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
});
|
2020-05-27 18:08:14 +00:00
|
|
|
|
|
|
|
it('does not show empty state', () => {
|
|
|
|
expect(wrapper.find(EmptyState).exists()).toBe(false);
|
|
|
|
});
|
2020-04-14 21:09:52 +00:00
|
|
|
});
|
2020-09-25 15:09:36 +00:00
|
|
|
|
|
|
|
describe('activated', () => {
|
|
|
|
let inititializeSpy;
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
inititializeSpy = jest.spyOn(wrapper.find(RepoCommitSection).vm, 'initialize');
|
|
|
|
store.state.viewer = 'diff';
|
|
|
|
|
|
|
|
await wrapper.vm.reactivate();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('re initializes the component', () => {
|
|
|
|
expect(inititializeSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2020-04-14 21:09:52 +00:00
|
|
|
});
|