BigW Consortium Gitlab

improved mockservicecall and fixed up a settimeout

parent 4a4b586a
...@@ -14,12 +14,17 @@ const waitForAllCallsToFinish = (service, waitForCount, successCallback) => { ...@@ -14,12 +14,17 @@ const waitForAllCallsToFinish = (service, waitForCount, successCallback) => {
timer(); timer();
}; };
function mockServiceCall(service, mockCall) { function mockServiceCall(service, response, shouldFail) {
const action = shouldFail ? Promise.reject : Promise.resolve;
const responseObject = response;
if (!responseObject.headers) responseObject.headers = {};
service.fetch.calls.reset(); service.fetch.calls.reset();
service.fetch.and.callFake(mockCall); service.fetch.and.callFake(() => action(responseObject));
} }
describe('Poll', () => { fdescribe('Poll', () => {
const service = jasmine.createSpyObj('service', ['fetch']); const service = jasmine.createSpyObj('service', ['fetch']);
let callbacks; let callbacks;
...@@ -33,11 +38,8 @@ describe('Poll', () => { ...@@ -33,11 +38,8 @@ describe('Poll', () => {
spyOn(callbacks, 'error'); spyOn(callbacks, 'error');
}); });
it('calls the success callback when no header for interval is provided', (done) => { fit('calls the success callback when no header for interval is provided', (done) => {
mockServiceCall( mockServiceCall(service, { status: 200 });
service,
() => Promise.resolve({ status: 200, headers: {} }),
);
new Poll({ new Poll({
resource: service, resource: service,
...@@ -55,10 +57,7 @@ describe('Poll', () => { ...@@ -55,10 +57,7 @@ describe('Poll', () => {
}); });
it('calls the error callback whe the http request returns an error', (done) => { it('calls the error callback whe the http request returns an error', (done) => {
mockServiceCall( mockServiceCall(service, { status: 500 }, true);
service,
() => Promise.reject({ status: 500, headers: {} }),
);
new Poll({ new Poll({
resource: service, resource: service,
...@@ -76,31 +75,23 @@ describe('Poll', () => { ...@@ -76,31 +75,23 @@ describe('Poll', () => {
}); });
it('should call the success callback when the interval header is -1', (done) => { it('should call the success callback when the interval header is -1', (done) => {
mockServiceCall( mockServiceCall(service, { status: 200, headers: { 'poll-interval': -1 } });
service,
() => Promise.resolve({ status: 200, headers: { 'poll-interval': -1 } }),
);
new Poll({ new Poll({
resource: service, resource: service,
method: 'fetch', method: 'fetch',
successCallback: callbacks.success, successCallback: callbacks.success,
errorCallback: callbacks.error, errorCallback: callbacks.error,
}).makeRequest(); }).makeRequest().then(() => {
setTimeout(() => {
expect(callbacks.success).toHaveBeenCalled(); expect(callbacks.success).toHaveBeenCalled();
expect(callbacks.error).not.toHaveBeenCalled(); expect(callbacks.error).not.toHaveBeenCalled();
done(); done();
}, 0); }).catch(done.fail);
}); });
it('starts polling when http status is 200 and interval header is provided', (done) => { it('starts polling when http status is 200 and interval header is provided', (done) => {
mockServiceCall( mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
service,
() => Promise.resolve({ status: 200, headers: { 'poll-interval': 1 } }),
);
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
...@@ -126,10 +117,7 @@ describe('Poll', () => { ...@@ -126,10 +117,7 @@ describe('Poll', () => {
describe('stop', () => { describe('stop', () => {
it('stops polling when method is called', (done) => { it('stops polling when method is called', (done) => {
mockServiceCall( mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
service,
() => Promise.resolve({ status: 200, headers: { 'poll-interval': 1 } }),
);
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
...@@ -157,10 +145,7 @@ describe('Poll', () => { ...@@ -157,10 +145,7 @@ describe('Poll', () => {
describe('restart', () => { describe('restart', () => {
it('should restart polling when its called', (done) => { it('should restart polling when its called', (done) => {
mockServiceCall( mockServiceCall(service, { status: 200, headers: { 'poll-interval': 1 } });
service,
() => Promise.resolve({ status: 200, headers: { 'poll-interval': 1 } }),
);
const Polling = new Poll({ const Polling = new Poll({
resource: service, resource: service,
......
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