Created
October 1, 2015 07:56
-
-
Save wolframkriesing/c0e14945ac25402f5b30 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global require, describe, it */ | |
var assert = require('assert'); | |
const URL_PREFIX = 'http://katas.u/katas/es6/language/'; | |
const KATAS_URL = `${URL_PREFIX}__grouped__.json`; | |
describe('load ES6 kata data', function() { | |
it('loaded data are as expected', function(done) { | |
function onSuccess(groupedKatas) { | |
assert.ok(groupedKatas); | |
done(); | |
} | |
loadGroupedKata(KATAS_URL, function() {}, onSuccess); | |
}); | |
describe('on error, call error callback and the error passed', function() { | |
it('invalid JSON', function(done) { | |
function onError(err) { | |
assert.ok(err); | |
done(); | |
} | |
var invalidUrl = URL_PREFIX; | |
loadGroupedKata(invalidUrl, onError); | |
}); | |
it('for invalid data', function(done) { | |
function onError(err) { | |
assert.ok(err); | |
done(); | |
} | |
function onSuccess() { | |
done(new Error('`onError` was not called')); | |
} | |
var invalidData = URL_PREFIX + '__all__.json'; | |
loadGroupedKata(invalidData, onError, onSuccess); | |
}); | |
}); | |
}); | |
import http from 'http'; | |
import url from 'url'; | |
function loadGroupedKata(katasUrl, onError, onSuccess) { | |
var data = ''; | |
var options = url.parse(katasUrl); | |
options.withCredentials = false; | |
var request = http.request(options, function(res) { | |
res.on('data', function(chunk) {data += chunk;}); | |
res.on('end', function() { | |
try { | |
const parsed = JSON.parse(data); | |
if (!onSuccess) { | |
return | |
} | |
if ('groups' in parsed) { | |
onSuccess(parsed); | |
} else { | |
onError('No groups found in this data.'); | |
} | |
} catch (e) { | |
onError(e); | |
} | |
}); | |
}); | |
request.on('error', function(e) { | |
onError(e); | |
}); | |
request.end(); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "testing-async-workshop", | |
"version": "1.0.0", | |
"description": "Testing async workshop", | |
"main": "index.js", | |
"scripts": { | |
"test": "npm run lint && mocha --compilers js:babel/register test", | |
"lint": "eslint src" | |
}, | |
"keywords": [ | |
"es6", | |
"tests" | |
], | |
"author": "Wolfram Kriesing", | |
"license": "MIT", | |
"devDependencies": { | |
"babel": "^5.8.23", | |
"mocha": "^2.3.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment