Commit 04b8f3d3 authored by Himanshu Kapoor's avatar Himanshu Kapoor Committed by Kushal Pandya

Cleanup async actions

Use return instead of calling done() and done.fail()
parent 4529939f
...@@ -79,11 +79,13 @@ export const getFileData = ( ...@@ -79,11 +79,13 @@ export const getFileData = (
return service return service
.getFileData(url) .getFileData(url)
.then(({ data }) => { .then(({ data }) => {
setPageTitleForFile(state, file);
if (data) commit(types.SET_FILE_DATA, { data, file }); if (data) commit(types.SET_FILE_DATA, { data, file });
if (openFile) commit(types.TOGGLE_FILE_OPEN, path); if (openFile) commit(types.TOGGLE_FILE_OPEN, path);
if (makeFileActive) dispatch('setFileActive', path);
if (makeFileActive) {
setPageTitleForFile(state, file);
dispatch('setFileActive', path);
}
}) })
.catch(() => { .catch(() => {
dispatch('setErrorMessage', { dispatch('setErrorMessage', {
......
---
title: 'Web IDE: Page title should not be .editorconfig when the IDE is first loaded.'
merge_request: 36783
author:
type: fixed
...@@ -51,35 +51,27 @@ describe('IDE store file actions', () => { ...@@ -51,35 +51,27 @@ describe('IDE store file actions', () => {
store.state.entries[localFile.path] = localFile; store.state.entries[localFile.path] = localFile;
}); });
it('closes open files', done => { it('closes open files', () => {
store return store.dispatch('closeFile', localFile).then(() => {
.dispatch('closeFile', localFile) expect(localFile.opened).toBeFalsy();
.then(() => { expect(localFile.active).toBeFalsy();
expect(localFile.opened).toBeFalsy(); expect(store.state.openFiles.length).toBe(0);
expect(localFile.active).toBeFalsy(); });
expect(store.state.openFiles.length).toBe(0);
done();
})
.catch(done.fail);
}); });
it('closes file even if file has changes', done => { it('closes file even if file has changes', () => {
store.state.changedFiles.push(localFile); store.state.changedFiles.push(localFile);
store return store
.dispatch('closeFile', localFile) .dispatch('closeFile', localFile)
.then(Vue.nextTick) .then(Vue.nextTick)
.then(() => { .then(() => {
expect(store.state.openFiles.length).toBe(0); expect(store.state.openFiles.length).toBe(0);
expect(store.state.changedFiles.length).toBe(1); expect(store.state.changedFiles.length).toBe(1);
});
done();
})
.catch(done.fail);
}); });
it('closes file & opens next available file', done => { it('closes file & opens next available file', () => {
const f = { const f = {
...file('newOpenFile'), ...file('newOpenFile'),
url: '/newOpenFile', url: '/newOpenFile',
...@@ -88,31 +80,23 @@ describe('IDE store file actions', () => { ...@@ -88,31 +80,23 @@ describe('IDE store file actions', () => {
store.state.openFiles.push(f); store.state.openFiles.push(f);
store.state.entries[f.path] = f; store.state.entries[f.path] = f;
store return store
.dispatch('closeFile', localFile) .dispatch('closeFile', localFile)
.then(Vue.nextTick) .then(Vue.nextTick)
.then(() => { .then(() => {
expect(router.push).toHaveBeenCalledWith(`/project${f.url}`); expect(router.push).toHaveBeenCalledWith(`/project${f.url}`);
});
done();
})
.catch(done.fail);
}); });
it('removes file if it pending', done => { it('removes file if it pending', () => {
store.state.openFiles.push({ store.state.openFiles.push({
...localFile, ...localFile,
pending: true, pending: true,
}); });
store return store.dispatch('closeFile', localFile).then(() => {
.dispatch('closeFile', localFile) expect(store.state.openFiles.length).toBe(0);
.then(() => { });
expect(store.state.openFiles.length).toBe(0);
done();
})
.catch(done.fail);
}); });
}); });
...@@ -264,61 +248,48 @@ describe('IDE store file actions', () => { ...@@ -264,61 +248,48 @@ describe('IDE store file actions', () => {
); );
}); });
it('calls the service', done => { it('calls the service', () => {
store return store.dispatch('getFileData', { path: localFile.path }).then(() => {
.dispatch('getFileData', { path: localFile.path }) expect(service.getFileData).toHaveBeenCalledWith(
.then(() => { `${RELATIVE_URL_ROOT}/test/test/-/7297abc/${localFile.path}`,
expect(service.getFileData).toHaveBeenCalledWith( );
`${RELATIVE_URL_ROOT}/test/test/-/7297abc/${localFile.path}`, });
);
done();
})
.catch(done.fail);
}); });
it('sets document title with the branchId', done => { it('sets document title with the branchId', () => {
store return store.dispatch('getFileData', { path: localFile.path }).then(() => {
.dispatch('getFileData', { path: localFile.path }) expect(document.title).toBe(`${localFile.path} · master · test/test · GitLab`);
.then(() => { });
expect(document.title).toBe(`${localFile.path} · master · test/test · GitLab`);
done();
})
.catch(done.fail);
}); });
it('sets the file as active', done => { it('sets the file as active', () => {
store return store.dispatch('getFileData', { path: localFile.path }).then(() => {
.dispatch('getFileData', { path: localFile.path }) expect(localFile.active).toBeTruthy();
.then(() => { });
expect(localFile.active).toBeTruthy();
done();
})
.catch(done.fail);
}); });
it('sets the file not as active if we pass makeFileActive false', done => { it('sets the file not as active if we pass makeFileActive false', () => {
store return store
.dispatch('getFileData', { path: localFile.path, makeFileActive: false }) .dispatch('getFileData', { path: localFile.path, makeFileActive: false })
.then(() => { .then(() => {
expect(localFile.active).toBeFalsy(); expect(localFile.active).toBeFalsy();
});
done();
})
.catch(done.fail);
}); });
it('adds the file to open files', done => { it('does not update the page title with the path of the file if makeFileActive is false', () => {
store document.title = 'dummy title';
.dispatch('getFileData', { path: localFile.path }) return store
.dispatch('getFileData', { path: localFile.path, makeFileActive: false })
.then(() => { .then(() => {
expect(store.state.openFiles.length).toBe(1); expect(document.title).toBe(`dummy title`);
expect(store.state.openFiles[0].name).toBe(localFile.name); });
});
done(); it('adds the file to open files', () => {
}) return store.dispatch('getFileData', { path: localFile.path }).then(() => {
.catch(done.fail); expect(store.state.openFiles.length).toBe(1);
expect(store.state.openFiles[0].name).toBe(localFile.name);
});
}); });
}); });
...@@ -342,15 +313,10 @@ describe('IDE store file actions', () => { ...@@ -342,15 +313,10 @@ describe('IDE store file actions', () => {
); );
}); });
it('sets document title considering `prevPath` on a file', done => { it('sets document title considering `prevPath` on a file', () => {
store return store.dispatch('getFileData', { path: localFile.path }).then(() => {
.dispatch('getFileData', { path: localFile.path }) expect(document.title).toBe(`new-shiny-file · master · test/test · GitLab`);
.then(() => { });
expect(document.title).toBe(`new-shiny-file · master · test/test · GitLab`);
done();
})
.catch(done.fail);
}); });
}); });
...@@ -397,29 +363,19 @@ describe('IDE store file actions', () => { ...@@ -397,29 +363,19 @@ describe('IDE store file actions', () => {
mock.onGet(/(.*)/).replyOnce(200, 'raw'); mock.onGet(/(.*)/).replyOnce(200, 'raw');
}); });
it('calls getRawFileData service method', done => { it('calls getRawFileData service method', () => {
store return store.dispatch('getRawFileData', { path: tmpFile.path }).then(() => {
.dispatch('getRawFileData', { path: tmpFile.path }) expect(service.getRawFileData).toHaveBeenCalledWith(tmpFile);
.then(() => { });
expect(service.getRawFileData).toHaveBeenCalledWith(tmpFile);
done();
})
.catch(done.fail);
}); });
it('updates file raw data', done => { it('updates file raw data', () => {
store return store.dispatch('getRawFileData', { path: tmpFile.path }).then(() => {
.dispatch('getRawFileData', { path: tmpFile.path }) expect(tmpFile.raw).toBe('raw');
.then(() => { });
expect(tmpFile.raw).toBe('raw');
done();
})
.catch(done.fail);
}); });
it('calls also getBaseRawFileData service method', done => { it('calls also getBaseRawFileData service method', () => {
jest.spyOn(service, 'getBaseRawFileData').mockReturnValue(Promise.resolve('baseraw')); jest.spyOn(service, 'getBaseRawFileData').mockReturnValue(Promise.resolve('baseraw'));
store.state.currentProjectId = 'gitlab-org/gitlab-ce'; store.state.currentProjectId = 'gitlab-org/gitlab-ce';
...@@ -436,15 +392,10 @@ describe('IDE store file actions', () => { ...@@ -436,15 +392,10 @@ describe('IDE store file actions', () => {
tmpFile.mrChange = { new_file: false }; tmpFile.mrChange = { new_file: false };
store return store.dispatch('getRawFileData', { path: tmpFile.path }).then(() => {
.dispatch('getRawFileData', { path: tmpFile.path }) expect(service.getBaseRawFileData).toHaveBeenCalledWith(tmpFile, 'SHA');
.then(() => { expect(tmpFile.baseRaw).toBe('baseraw');
expect(service.getBaseRawFileData).toHaveBeenCalledWith(tmpFile, 'SHA'); });
expect(tmpFile.baseRaw).toBe('baseraw');
done();
})
.catch(done.fail);
}); });
describe('sets file loading to true', () => { describe('sets file loading to true', () => {
...@@ -501,15 +452,10 @@ describe('IDE store file actions', () => { ...@@ -501,15 +452,10 @@ describe('IDE store file actions', () => {
mock.onGet(/(.*)/).replyOnce(200, JSON.stringify({ test: '123' })); mock.onGet(/(.*)/).replyOnce(200, JSON.stringify({ test: '123' }));
}); });
it('does not parse returned JSON', done => { it('does not parse returned JSON', () => {
store return store.dispatch('getRawFileData', { path: tmpFile.path }).then(() => {
.dispatch('getRawFileData', { path: tmpFile.path }) expect(tmpFile.raw).toEqual('{"test":"123"}');
.then(() => { });
expect(tmpFile.raw).toEqual('{"test":"123"}');
done();
})
.catch(done.fail);
}); });
}); });
...@@ -558,32 +504,25 @@ describe('IDE store file actions', () => { ...@@ -558,32 +504,25 @@ describe('IDE store file actions', () => {
store.state.entries[tmpFile.path] = tmpFile; store.state.entries[tmpFile.path] = tmpFile;
}); });
it('updates file content', done => { it('updates file content', () => {
callAction() return callAction().then(() => {
.then(() => { expect(tmpFile.content).toBe('content\n');
expect(tmpFile.content).toBe('content\n'); });
done();
})
.catch(done.fail);
}); });
it('adds file into stagedFiles array', done => { it('adds file into stagedFiles array', () => {
store return store
.dispatch('changeFileContent', { .dispatch('changeFileContent', {
path: tmpFile.path, path: tmpFile.path,
content: 'content', content: 'content',
}) })
.then(() => { .then(() => {
expect(store.state.stagedFiles.length).toBe(1); expect(store.state.stagedFiles.length).toBe(1);
});
done();
})
.catch(done.fail);
}); });
it('adds file not more than once into stagedFiles array', done => { it('adds file not more than once into stagedFiles array', () => {
store return store
.dispatch('changeFileContent', { .dispatch('changeFileContent', {
path: tmpFile.path, path: tmpFile.path,
content: 'content', content: 'content',
...@@ -596,14 +535,11 @@ describe('IDE store file actions', () => { ...@@ -596,14 +535,11 @@ describe('IDE store file actions', () => {
) )
.then(() => { .then(() => {
expect(store.state.stagedFiles.length).toBe(1); expect(store.state.stagedFiles.length).toBe(1);
});
done();
})
.catch(done.fail);
}); });
it('removes file from changedFiles array if not changed', done => { it('removes file from changedFiles array if not changed', () => {
store return store
.dispatch('changeFileContent', { .dispatch('changeFileContent', {
path: tmpFile.path, path: tmpFile.path,
content: 'content\n', content: 'content\n',
...@@ -616,10 +552,7 @@ describe('IDE store file actions', () => { ...@@ -616,10 +552,7 @@ describe('IDE store file actions', () => {
) )
.then(() => { .then(() => {
expect(store.state.changedFiles.length).toBe(0); expect(store.state.changedFiles.length).toBe(0);
});
done();
})
.catch(done.fail);
}); });
}); });
...@@ -777,52 +710,36 @@ describe('IDE store file actions', () => { ...@@ -777,52 +710,36 @@ describe('IDE store file actions', () => {
store.state.entries[f.path] = f; store.state.entries[f.path] = f;
}); });
it('makes file pending in openFiles', done => { it('makes file pending in openFiles', () => {
store return store.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }).then(() => {
.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }) expect(store.state.openFiles[0].pending).toBe(true);
.then(() => { });
expect(store.state.openFiles[0].pending).toBe(true);
})
.then(done)
.catch(done.fail);
}); });
it('returns true when opened', done => { it('returns true when opened', () => {
store return store.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }).then(added => {
.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }) expect(added).toBe(true);
.then(added => { });
expect(added).toBe(true);
})
.then(done)
.catch(done.fail);
}); });
it('returns false when already opened', done => { it('returns false when already opened', () => {
store.state.openFiles.push({ store.state.openFiles.push({
...f, ...f,
active: true, active: true,
key: `pending-${f.key}`, key: `pending-${f.key}`,
}); });
store return store.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }).then(added => {
.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }) expect(added).toBe(false);
.then(added => { });
expect(added).toBe(false);
})
.then(done)
.catch(done.fail);
}); });
it('pushes router URL when added', done => { it('pushes router URL when added', () => {
store.state.currentBranchId = 'master'; store.state.currentBranchId = 'master';
store return store.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }).then(() => {
.dispatch('openPendingTab', { file: f, keyPrefix: 'pending' }) expect(router.push).toHaveBeenCalledWith('/project/123/tree/master/');
.then(() => { });
expect(router.push).toHaveBeenCalledWith('/project/123/tree/master/');
})
.then(done)
.catch(done.fail);
}); });
}); });
...@@ -838,26 +755,18 @@ describe('IDE store file actions', () => { ...@@ -838,26 +755,18 @@ describe('IDE store file actions', () => {
}; };
}); });
it('removes pending file from open files', done => { it('removes pending file from open files', () => {
store.state.openFiles.push(f); store.state.openFiles.push(f);
store return store.dispatch('removePendingTab', f).then(() => {
.dispatch('removePendingTab', f) expect(store.state.openFiles.length).toBe(0);
.then(() => { });
expect(store.state.openFiles.length).toBe(0);
})
.then(done)
.catch(done.fail);
}); });
it('emits event to dispose model', done => { it('emits event to dispose model', () => {
store return store.dispatch('removePendingTab', f).then(() => {
.dispatch('removePendingTab', f) expect(eventHub.$emit).toHaveBeenCalledWith(`editor.update.model.dispose.${f.key}`);
.then(() => { });
expect(eventHub.$emit).toHaveBeenCalledWith(`editor.update.model.dispose.${f.key}`);
})
.then(done)
.catch(done.fail);
}); });
}); });
...@@ -866,14 +775,10 @@ describe('IDE store file actions', () => { ...@@ -866,14 +775,10 @@ describe('IDE store file actions', () => {
jest.spyOn(eventHub, '$emit').mockImplementation(() => {}); jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
}); });
it('emits event that files have changed', done => { it('emits event that files have changed', () => {
store return store.dispatch('triggerFilesChange').then(() => {
.dispatch('triggerFilesChange') expect(eventHub.$emit).toHaveBeenCalledWith('ide.files.change');
.then(() => { });
expect(eventHub.$emit).toHaveBeenCalledWith('ide.files.change');
})
.then(done)
.catch(done.fail);
}); });
}); });
}); });
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