Skip to content

Instantly share code, notes, and snippets.

@webpapaya
Created October 29, 2016 08:59
Show Gist options
  • Save webpapaya/d0a6c6926d116e2447a8057bcb25182c to your computer and use it in GitHub Desktop.
Save webpapaya/d0a6c6926d116e2447a8057bcb25182c to your computer and use it in GitHub Desktop.
// 75: Promise - basics
// To do: make all tests pass, leave the assert lines unchanged!
function succeedingApiCall() {
return Promise.resolve('200');
}
function failingApiCall() {
return Promise.reject('500');
}
function succeedingGpsFetch() {
return Promise.resolve('Gps');
}
function failingGpsFetch() {
return Promise.reject('Gps Error');
}
function clockInWithoutGps(apiCall) {
return apiCall();
}
function clockInWithGps(fetchGps, apiCall) {
return fetchGps()
.then((gps) => {
return [apiCall(), Promise.resolve(gps)];
})
.then((results) => Promise.all(results))
.then((results) => results.join('+'));
}
describe('clock in without gps', () => {
it('sends a timestamp, and server acknolages', () => {
return clockInWithoutGps(succeedingApiCall)
.then((response) => assert.equal(response, '200'))
});
it('sends a timestap, and server doesn\'t respond', (done) => {
clockInWithoutGps(failingApiCall)
.then(() => assert(false))
.catch((response) => assert.equal(response, '500'))
.then(done);
});
});
describe('clock in with gps', () => {
it('doesn\'t send the timestamp if gps fails', () => {
let wasCalled = false;
function apiCall() { assert(false) }
return clockInWithGps(failingGpsFetch, apiCall)
.then(() => assert(false))
.catch((response) => {
assert.equal(wasCalled, false);
assert.equal(response, 'Gps Error');
});
});
it('sends timestamp and gps, and server acknowledges', () => {
return clockInWithGps(succeedingGpsFetch, succeedingApiCall)
.then((response) => assert.equal(response, '200+Gps'))
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment