Commit e420c0ed authored by Phil Hughes's avatar Phil Hughes

Fix currentDiffFileId being set incorrectly

This can happen when a note on a diff file that no longer exists
is linked to.
parent c7edfdec
...@@ -743,14 +743,14 @@ export function moveToNeighboringCommit({ dispatch, state }, { direction }) { ...@@ -743,14 +743,14 @@ export function moveToNeighboringCommit({ dispatch, state }, { direction }) {
} }
} }
export const setCurrentDiffFileIdFromNote = ({ commit, rootGetters }, noteId) => { export const setCurrentDiffFileIdFromNote = ({ commit, state, rootGetters }, noteId) => {
const note = rootGetters.notesById[noteId]; const note = rootGetters.notesById[noteId];
if (!note) return; if (!note) return;
const fileHash = rootGetters.getDiscussion(note.discussion_id).diff_file?.file_hash; const fileHash = rootGetters.getDiscussion(note.discussion_id).diff_file?.file_hash;
if (fileHash) { if (fileHash && state.diffFiles.some(f => f.file_hash === fileHash)) {
commit(types.UPDATE_CURRENT_DIFF_FILE_ID, fileHash); commit(types.UPDATE_CURRENT_DIFF_FILE_ID, fileHash);
} }
}; };
......
...@@ -1594,24 +1594,39 @@ describe('DiffsStoreActions', () => { ...@@ -1594,24 +1594,39 @@ describe('DiffsStoreActions', () => {
describe('setCurrentDiffFileIdFromNote', () => { describe('setCurrentDiffFileIdFromNote', () => {
it('commits UPDATE_CURRENT_DIFF_FILE_ID', () => { it('commits UPDATE_CURRENT_DIFF_FILE_ID', () => {
const commit = jest.fn(); const commit = jest.fn();
const state = { diffFiles: [{ file_hash: '123' }] };
const rootGetters = { const rootGetters = {
getDiscussion: () => ({ diff_file: { file_hash: '123' } }), getDiscussion: () => ({ diff_file: { file_hash: '123' } }),
notesById: { '1': { discussion_id: '2' } }, notesById: { '1': { discussion_id: '2' } },
}; };
setCurrentDiffFileIdFromNote({ commit, rootGetters }, '1'); setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1');
expect(commit).toHaveBeenCalledWith(types.UPDATE_CURRENT_DIFF_FILE_ID, '123'); expect(commit).toHaveBeenCalledWith(types.UPDATE_CURRENT_DIFF_FILE_ID, '123');
}); });
it('does not commit UPDATE_CURRENT_DIFF_FILE_ID when discussion has no diff_file', () => { it('does not commit UPDATE_CURRENT_DIFF_FILE_ID when discussion has no diff_file', () => {
const commit = jest.fn(); const commit = jest.fn();
const state = { diffFiles: [{ file_hash: '123' }] };
const rootGetters = { const rootGetters = {
getDiscussion: () => ({ id: '1' }), getDiscussion: () => ({ id: '1' }),
notesById: { '1': { discussion_id: '2' } }, notesById: { '1': { discussion_id: '2' } },
}; };
setCurrentDiffFileIdFromNote({ commit, rootGetters }, '1'); setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1');
expect(commit).not.toHaveBeenCalled();
});
it('does not commit UPDATE_CURRENT_DIFF_FILE_ID when diff file does not exist', () => {
const commit = jest.fn();
const state = { diffFiles: [{ file_hash: '123' }] };
const rootGetters = {
getDiscussion: () => ({ diff_file: { file_hash: '124' } }),
notesById: { '1': { discussion_id: '2' } },
};
setCurrentDiffFileIdFromNote({ commit, state, rootGetters }, '1');
expect(commit).not.toHaveBeenCalled(); expect(commit).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