Commit a82aec39 authored by Himanshu Kapoor's avatar Himanshu Kapoor

Call openPendingTab only if open file has change

Now that staging and unstaging might result in absence of a change
when the changes cancel each other out, we need to add if condition
checks before opening a tab for a file to see if there are actually
any changes for that file or not. For example, adding a word "hello"
in staged and removing the same word in unstaged.
parent f94ce61e
......@@ -143,10 +143,14 @@ export const stageAllChanges = ({ state, commit, dispatch, getters }) => {
commit(types.STAGE_CHANGE, { path: file.path, diffInfo: getters.getDiffInfo(file.path) }),
);
const file = getters.getStagedFile(openFile.path);
if (file) {
dispatch('openPendingTab', {
file: state.stagedFiles.find(f => f.path === openFile.path),
file,
keyPrefix: stageKeys.staged,
});
}
};
export const unstageAllChanges = ({ state, commit, dispatch, getters }) => {
......@@ -156,10 +160,14 @@ export const unstageAllChanges = ({ state, commit, dispatch, getters }) => {
commit(types.UNSTAGE_CHANGE, { path: file.path, diffInfo: getters.getDiffInfo(file.path) }),
);
const file = getters.getChangedFile(openFile.path);
if (file) {
dispatch('openPendingTab', {
file: state.changedFiles.find(f => f.path === openFile.path),
file,
keyPrefix: stageKeys.unstaged,
});
}
};
export const updateViewer = ({ commit }, viewer) => {
......
......@@ -214,9 +214,9 @@ export const discardFileChanges = ({ dispatch, state, commit, getters }, path) =
eventHub.$emit(`editor.update.model.dispose.unstaged-${file.key}`, file.content);
};
export const stageChange = ({ commit, state, dispatch, getters }, path) => {
const stagedFile = state.stagedFiles.find(f => f.path === path);
const openFile = state.openFiles.find(f => f.path === path);
export const stageChange = ({ commit, dispatch, getters }, path) => {
const stagedFile = getters.getStagedFile(path);
const openFile = getters.getOpenFile(path);
commit(types.STAGE_CHANGE, { path, diffInfo: getters.getDiffInfo(path) });
commit(types.SET_LAST_COMMIT_MSG, '');
......@@ -225,9 +225,9 @@ export const stageChange = ({ commit, state, dispatch, getters }, path) => {
eventHub.$emit(`editor.update.model.new.content.staged-${stagedFile.key}`, stagedFile.content);
}
if (openFile && openFile.active) {
const file = state.stagedFiles.find(f => f.path === path);
const file = getters.getStagedFile(path);
if (openFile && openFile.active && file) {
dispatch('openPendingTab', {
file,
keyPrefix: stageKeys.staged,
......@@ -235,14 +235,14 @@ export const stageChange = ({ commit, state, dispatch, getters }, path) => {
}
};
export const unstageChange = ({ commit, dispatch, state, getters }, path) => {
const openFile = state.openFiles.find(f => f.path === path);
export const unstageChange = ({ commit, dispatch, getters }, path) => {
const openFile = getters.getOpenFile(path);
commit(types.UNSTAGE_CHANGE, { path, diffInfo: getters.getDiffInfo(path) });
if (openFile && openFile.active) {
const file = state.changedFiles.find(f => f.path === path);
const file = getters.getChangedFile(path);
if (openFile && openFile.active && file) {
dispatch('openPendingTab', {
file,
keyPrefix: stageKeys.unstaged,
......
......@@ -64,6 +64,7 @@ export const allBlobs = state =>
export const getChangedFile = state => path => state.changedFiles.find(f => f.path === path);
export const getStagedFile = state => path => state.stagedFiles.find(f => f.path === path);
export const getOpenFile = state => path => state.openFiles.find(f => f.path === path);
export const lastOpenedFile = state =>
[...state.changedFiles, ...state.stagedFiles].sort((a, b) => b.lastOpenedAt - a.lastOpenedAt)[0];
......
......@@ -673,10 +673,11 @@ describe('IDE store file actions', () => {
actions.stageChange(store, 'path');
expect(store.commit.calls.allArgs()).toEqual([
[types.STAGE_CHANGE, jest.objectContaining({ path: 'path' })],
[types.SET_LAST_COMMIT_MSG, ''],
]);
expect(store.commit).toHaveBeenCalledWith(
types.STAGE_CHANGE,
expect.objectContaining({ path: 'path' }),
);
expect(store.commit).toHaveBeenCalledWith(types.SET_LAST_COMMIT_MSG, '');
});
});
......@@ -689,9 +690,10 @@ describe('IDE store file actions', () => {
actions.unstageChange(store, 'path');
expect(store.commit.calls.allArgs()).toEqual([
[types.UNSTAGE_CHANGE, jest.objectContaining({ path: 'path' })],
]);
expect(store.commit).toHaveBeenCalledWith(
types.UNSTAGE_CHANGE,
expect.objectContaining({ path: 'path' }),
);
});
});
......
......@@ -417,6 +417,27 @@ describe('Multi-file store actions', () => {
[types.STAGE_CHANGE, jasmine.objectContaining({ path: file1.path })],
]);
});
it('opens pending tab if a change exists in that file', () => {
stageAllChanges(store);
expect(store.dispatch.calls.allArgs()).toEqual([
[
'openPendingTab',
{ file: { ...file1, staged: true, changed: true }, keyPrefix: 'staged' },
],
]);
});
it('does not open pending tab if no change exists in that file', () => {
store.state.entries[file1.path].content = 'test';
store.state.stagedFiles = [file1];
store.state.changedFiles = [store.state.entries[file1.path]];
stageAllChanges(store);
expect(store.dispatch).not.toHaveBeenCalled();
});
});
describe('unstageAllChanges', () => {
......@@ -427,6 +448,24 @@ describe('Multi-file store actions', () => {
[types.UNSTAGE_CHANGE, jasmine.objectContaining({ path: file2.path })],
]);
});
it('opens pending tab if a change exists in that file', () => {
unstageAllChanges(store);
expect(store.dispatch.calls.allArgs()).toEqual([
['openPendingTab', { file: file1, keyPrefix: 'unstaged' }],
]);
});
it('does not open pending tab if no change exists in that file', () => {
store.state.entries[file1.path].content = 'test';
store.state.stagedFiles = [file1];
store.state.changedFiles = [store.state.entries[file1.path]];
unstageAllChanges(store);
expect(store.dispatch).not.toHaveBeenCalled();
});
});
});
......
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