Commit 0e6c08f5 authored by Fatih Acet's avatar Fatih Acet

Simplify and unify Promise mocks in app_spec file

parent 69b87a97
...@@ -146,16 +146,10 @@ describe('Issuable output', () => { ...@@ -146,16 +146,10 @@ describe('Issuable output', () => {
it('fetches new data after update', done => { it('fetches new data after update', done => {
spyOn(vm, 'updateStoreState').and.callThrough(); spyOn(vm, 'updateStoreState').and.callThrough();
spyOn(vm.service, 'getData').and.callThrough(); spyOn(vm.service, 'getData').and.callThrough();
spyOn(vm.service, 'updateIssuable').and.callFake( spyOn(vm.service, 'updateIssuable').and.returnValue(
() => Promise.resolve({
new Promise(resolve => { data: { web_url: window.location.pathname },
resolve({ }),
data: {
confidential: false,
web_url: window.location.pathname,
},
});
}),
); );
vm.updateIssuable() vm.updateIssuable()
...@@ -168,11 +162,10 @@ describe('Issuable output', () => { ...@@ -168,11 +162,10 @@ describe('Issuable output', () => {
}); });
it('correctly updates issuable data', done => { it('correctly updates issuable data', done => {
spyOn(vm.service, 'updateIssuable').and.callFake( spyOn(vm.service, 'updateIssuable').and.returnValue(
() => Promise.resolve({
new Promise(resolve => { data: { web_url: window.location.pathname },
resolve(); }),
}),
); );
vm.updateIssuable() vm.updateIssuable()
...@@ -186,16 +179,13 @@ describe('Issuable output', () => { ...@@ -186,16 +179,13 @@ describe('Issuable output', () => {
it('does not redirect if issue has not moved', done => { it('does not redirect if issue has not moved', done => {
const visitUrl = spyOnDependency(issuableApp, 'visitUrl'); const visitUrl = spyOnDependency(issuableApp, 'visitUrl');
spyOn(vm.service, 'updateIssuable').and.callFake( spyOn(vm.service, 'updateIssuable').and.returnValue(
() => Promise.resolve({
new Promise(resolve => { data: {
resolve({ web_url: window.location.pathname,
data: { confidential: vm.isConfidential,
web_url: window.location.pathname, },
confidential: vm.isConfidential, }),
},
});
}),
); );
vm.updateIssuable(); vm.updateIssuable();
...@@ -208,16 +198,13 @@ describe('Issuable output', () => { ...@@ -208,16 +198,13 @@ describe('Issuable output', () => {
it('redirects if returned web_url has changed', done => { it('redirects if returned web_url has changed', done => {
const visitUrl = spyOnDependency(issuableApp, 'visitUrl'); const visitUrl = spyOnDependency(issuableApp, 'visitUrl');
spyOn(vm.service, 'updateIssuable').and.callFake( spyOn(vm.service, 'updateIssuable').and.returnValue(
() => Promise.resolve({
new Promise(resolve => { data: {
resolve({ web_url: '/testing-issue-move',
data: { confidential: vm.isConfidential,
web_url: '/testing-issue-move', },
confidential: vm.isConfidential, }),
},
});
}),
); );
vm.updateIssuable(); vm.updateIssuable();
...@@ -236,6 +223,7 @@ describe('Issuable output', () => { ...@@ -236,6 +223,7 @@ describe('Issuable output', () => {
vm.handleBeforeUnloadEvent(e); vm.handleBeforeUnloadEvent(e);
Vue.nextTick(() => { Vue.nextTick(() => {
expect(e.returnValue).not.toBeNull(); expect(e.returnValue).not.toBeNull();
done(); done();
}); });
}); });
...@@ -247,6 +235,7 @@ describe('Issuable output', () => { ...@@ -247,6 +235,7 @@ describe('Issuable output', () => {
vm.handleBeforeUnloadEvent(e); vm.handleBeforeUnloadEvent(e);
Vue.nextTick(() => { Vue.nextTick(() => {
expect(e.returnValue).not.toBeNull(); expect(e.returnValue).not.toBeNull();
done(); done();
}); });
}); });
...@@ -256,6 +245,7 @@ describe('Issuable output', () => { ...@@ -256,6 +245,7 @@ describe('Issuable output', () => {
vm.handleBeforeUnloadEvent(e); vm.handleBeforeUnloadEvent(e);
Vue.nextTick(() => { Vue.nextTick(() => {
expect(e.returnValue).toBeNull(); expect(e.returnValue).toBeNull();
done(); done();
}); });
}); });
...@@ -297,7 +287,8 @@ describe('Issuable output', () => { ...@@ -297,7 +287,8 @@ describe('Issuable output', () => {
it('shows error mesage from backend if exists', done => { it('shows error mesage from backend if exists', done => {
const msg = 'Custom error message from backend'; const msg = 'Custom error message from backend';
spyOn(vm.service, 'updateIssuable').and.callFake( spyOn(vm.service, 'updateIssuable').and.callFake(
() => Promise.reject({ response: { data: { errors: [msg] } } }), // eslint-disable-line prefer-promise-reject-errors // eslint-disable-next-line prefer-promise-reject-errors
() => Promise.reject({ response: { data: { errors: [msg] } } }),
); );
vm.updateIssuable(); vm.updateIssuable();
...@@ -361,21 +352,19 @@ describe('Issuable output', () => { ...@@ -361,21 +352,19 @@ describe('Issuable output', () => {
describe('deleteIssuable', () => { describe('deleteIssuable', () => {
it('changes URL when deleted', done => { it('changes URL when deleted', done => {
const visitUrl = spyOnDependency(issuableApp, 'visitUrl'); const visitUrl = spyOnDependency(issuableApp, 'visitUrl');
spyOn(vm.service, 'deleteIssuable').and.callFake( spyOn(vm.service, 'deleteIssuable').and.returnValue(
() => Promise.resolve({
new Promise(resolve => { data: {
resolve({ web_url: '/test',
data: { },
web_url: '/test', }),
},
});
}),
); );
vm.deleteIssuable(); vm.deleteIssuable();
setTimeout(() => { setTimeout(() => {
expect(visitUrl).toHaveBeenCalledWith('/test'); expect(visitUrl).toHaveBeenCalledWith('/test');
done(); done();
}); });
}); });
...@@ -383,37 +372,30 @@ describe('Issuable output', () => { ...@@ -383,37 +372,30 @@ describe('Issuable output', () => {
it('stops polling when deleting', done => { it('stops polling when deleting', done => {
spyOnDependency(issuableApp, 'visitUrl'); spyOnDependency(issuableApp, 'visitUrl');
spyOn(vm.poll, 'stop').and.callThrough(); spyOn(vm.poll, 'stop').and.callThrough();
spyOn(vm.service, 'deleteIssuable').and.callFake( spyOn(vm.service, 'deleteIssuable').and.returnValue(
() => Promise.resolve({
new Promise(resolve => { data: {
resolve({ web_url: '/test',
data: { },
web_url: '/test', }),
},
});
}),
); );
vm.deleteIssuable(); vm.deleteIssuable();
setTimeout(() => { setTimeout(() => {
expect(vm.poll.stop).toHaveBeenCalledWith(); expect(vm.poll.stop).toHaveBeenCalledWith();
done(); done();
}); });
}); });
it('closes form on error', done => { it('closes form on error', done => {
spyOn(vm.service, 'deleteIssuable').and.callFake( spyOn(vm.service, 'deleteIssuable').and.returnValue(Promise.reject());
() =>
new Promise((resolve, reject) => {
reject();
}),
);
vm.deleteIssuable(); vm.deleteIssuable();
setTimeout(() => { setTimeout(() => {
expect(eventHub.$emit).toHaveBeenCalledWith('close.form'); expect(eventHub.$emit).not.toHaveBeenCalledWith('close.form');
expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe( expect(document.querySelector('.flash-container .flash-text').innerText.trim()).toBe(
'Error deleting issue', 'Error deleting issue',
); );
......
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