diff options
Diffstat (limited to 'spec/frontend/ide/stores/mutations/file_spec.js')
-rw-r--r-- | spec/frontend/ide/stores/mutations/file_spec.js | 174 |
1 files changed, 143 insertions, 31 deletions
diff --git a/spec/frontend/ide/stores/mutations/file_spec.js b/spec/frontend/ide/stores/mutations/file_spec.js index 91506c1b46c..8cb386d27e5 100644 --- a/spec/frontend/ide/stores/mutations/file_spec.js +++ b/spec/frontend/ide/stores/mutations/file_spec.js @@ -1,14 +1,16 @@ import mutations from '~/ide/stores/mutations/file'; -import state from '~/ide/stores/state'; +import { createStore } from '~/ide/stores'; import { FILE_VIEW_MODE_PREVIEW } from '~/ide/constants'; import { file } from '../../helpers'; describe('IDE store file mutations', () => { let localState; + let localStore; let localFile; beforeEach(() => { - localState = state(); + localStore = createStore(); + localState = localStore.state; localFile = { ...file(), type: 'blob' }; localState.entries[localFile.path] = localFile; @@ -333,44 +335,154 @@ describe('IDE store file mutations', () => { }); }); - describe('STAGE_CHANGE', () => { - beforeEach(() => { - mutations.STAGE_CHANGE(localState, localFile.path); - }); + describe.each` + mutationName | mutation | addedTo | removedFrom | staged | changedFilesCount | stagedFilesCount + ${'STAGE_CHANGE'} | ${mutations.STAGE_CHANGE} | ${'stagedFiles'} | ${'changedFiles'} | ${true} | ${0} | ${1} + ${'UNSTAGE_CHANGE'} | ${mutations.UNSTAGE_CHANGE} | ${'changedFiles'} | ${'stagedFiles'} | ${false} | ${1} | ${0} + `( + '$mutationName', + ({ mutation, changedFilesCount, removedFrom, addedTo, staged, stagedFilesCount }) => { + let unstagedFile; + let stagedFile; + + beforeEach(() => { + unstagedFile = { + ...file('file'), + type: 'blob', + raw: 'original content', + content: 'changed content', + }; + + stagedFile = { + ...unstagedFile, + content: 'staged content', + staged: true, + }; + + localState.changedFiles.push(unstagedFile); + localState.stagedFiles.push(stagedFile); + localState.entries[unstagedFile.path] = unstagedFile; + }); - it('adds file into stagedFiles array', () => { - expect(localState.stagedFiles.length).toBe(1); - expect(localState.stagedFiles[0]).toEqual(localFile); - }); + it('removes all changes of a file if staged and unstaged change contents are equal', () => { + unstagedFile.content = 'original content'; - it('updates stagedFile if it is already staged', () => { - localFile.raw = 'testing 123'; + mutation(localState, { + path: unstagedFile.path, + diffInfo: localStore.getters.getDiffInfo(unstagedFile.path), + }); - mutations.STAGE_CHANGE(localState, localFile.path); + expect(localState.entries.file).toEqual( + expect.objectContaining({ + content: 'original content', + staged: false, + changed: false, + }), + ); - expect(localState.stagedFiles.length).toBe(1); - expect(localState.stagedFiles[0].raw).toEqual('testing 123'); - }); - }); + expect(localState.stagedFiles.length).toBe(0); + expect(localState.changedFiles.length).toBe(0); + }); - describe('UNSTAGE_CHANGE', () => { - let f; + it('removes all changes of a file if a file is deleted and a new file with same content is added', () => { + stagedFile.deleted = true; + unstagedFile.tempFile = true; + unstagedFile.content = 'original content'; - beforeEach(() => { - f = { ...file(), type: 'blob', staged: true }; + mutation(localState, { + path: unstagedFile.path, + diffInfo: localStore.getters.getDiffInfo(unstagedFile.path), + }); - localState.stagedFiles.push(f); - localState.changedFiles.push(f); - localState.entries[f.path] = f; - }); + expect(localState.stagedFiles.length).toBe(0); + expect(localState.changedFiles.length).toBe(0); - it('removes from stagedFiles array', () => { - mutations.UNSTAGE_CHANGE(localState, f.path); + expect(localState.entries.file).toEqual( + expect.objectContaining({ + content: 'original content', + deleted: false, + tempFile: false, + }), + ); + }); - expect(localState.stagedFiles.length).toBe(0); - expect(localState.changedFiles.length).toBe(1); - }); - }); + it('merges deleted and added file into a changed file if the contents differ', () => { + stagedFile.deleted = true; + unstagedFile.tempFile = true; + unstagedFile.content = 'hello'; + + mutation(localState, { + path: unstagedFile.path, + diffInfo: localStore.getters.getDiffInfo(unstagedFile.path), + }); + + expect(localState.stagedFiles.length).toBe(stagedFilesCount); + expect(localState.changedFiles.length).toBe(changedFilesCount); + + expect(unstagedFile).toEqual( + expect.objectContaining({ + content: 'hello', + staged, + deleted: false, + tempFile: false, + changed: true, + }), + ); + }); + + it('does not remove file from stagedFiles and changedFiles if the file was renamed, even if the contents are equal', () => { + unstagedFile.content = 'original content'; + unstagedFile.prevPath = 'old_file'; + + mutation(localState, { + path: unstagedFile.path, + diffInfo: localStore.getters.getDiffInfo(unstagedFile.path), + }); + + expect(localState.entries.file).toEqual( + expect.objectContaining({ + content: 'original content', + staged, + changed: false, + prevPath: 'old_file', + }), + ); + + expect(localState.stagedFiles.length).toBe(stagedFilesCount); + expect(localState.changedFiles.length).toBe(changedFilesCount); + }); + + it(`removes file from ${removedFrom} array and adds it into ${addedTo} array`, () => { + localState.stagedFiles.length = 0; + + mutation(localState, { + path: unstagedFile.path, + diffInfo: localStore.getters.getDiffInfo(unstagedFile.path), + }); + + expect(localState.stagedFiles.length).toBe(stagedFilesCount); + expect(localState.changedFiles.length).toBe(changedFilesCount); + + const f = localState.stagedFiles[0] || localState.changedFiles[0]; + expect(f).toEqual(unstagedFile); + }); + + it(`updates file in ${addedTo} array if it is was already present in it`, () => { + unstagedFile.raw = 'testing 123'; + + mutation(localState, { + path: unstagedFile.path, + diffInfo: localStore.getters.getDiffInfo(unstagedFile.path), + }); + + expect(localState.stagedFiles.length).toBe(stagedFilesCount); + expect(localState.changedFiles.length).toBe(changedFilesCount); + + const f = localState.stagedFiles[0] || localState.changedFiles[0]; + expect(f.raw).toEqual('testing 123'); + }); + }, + ); describe('TOGGLE_FILE_CHANGED', () => { it('updates file changed status', () => { |