Skip to content

Instantly share code, notes, and snippets.

@JoelCodes
Created June 1, 2017 00:07
Show Gist options
  • Select an option

  • Save JoelCodes/f2310bcc226bc2e29642494d29817330 to your computer and use it in GitHub Desktop.

Select an option

Save JoelCodes/f2310bcc226bc2e29642494d29817330 to your computer and use it in GitHub Desktop.
function getOrStoreTeam(db, teamId, teamName) {
return db
.getTeam(teamId)
.then((team) => {
if (team === undefined) {
return db.saveTeam(teamId, teamName);
}
return team;
});
}
function startGame(db, homeTeam, awayTeam) {
const homePromise = getOrStoreTeam(db, homeTeam.id, homeTeam.name);
const awayPromise = getOrStoreTeam(db, awayTeam.id, awayTeam.name);
return Promise.all([homePromise, awayPromise]);
}
module.exports = {
getOrStoreTeam,
startGame,
};
{
"name": "ti",
"version": "1.0.0",
"description": "",
"main": "erica.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chai": "^4.0.1",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^15.0.1",
"eslint-plugin-import": "^2.3.0",
"eslint-plugin-jsx-a11y": "^5.0.3",
"eslint-plugin-react": "^7.0.1",
"mocha": "^3.4.2"
}
}
const expect = require('chai').expect;
const {getOrStoreTeam, startGame} = require('./edurne.js');
describe('#getOrStoreTeam(teamName, teamId)', () =>{
it('gets a team from the database', (done) => {
const dodgers = {id: 1, name: 'Dodgers'};
const teams = [dodgers]
const db = {
getTeam(teamId){
return Promise.resolve(teams.find(team => team.id === teamId));
}
}
getOrStoreTeam(db, 1, 'Dodgers').then((team) => {
expect(team).to.eq(dodgers);
done();
})
});
it('saves the team to the database if not found', (done) => {
const dodgers = {id: 1, name: 'Dodgers'};
const teams = [dodgers]
const db = {
getTeam(teamId){
return Promise.resolve(teams.find(team => team.id === teamId));
},
saveTeam(teamId, teamName){
const createdTeam = {id: teamId, name: teamName};
teams.push(createdTeam);
return Promise.resolve(createdTeam);
}
}
getOrStoreTeam(db, 2, "Lakers")
.then((team) => {
expect(team).to.deep.eq({id: 2, name: "Lakers"});
expect(teams).to.deep.include({id: 2, name: "Lakers"});
done();
})
});
});
describe('#startGame(homeTeam, awayTeam)', () => {
it('creates or gets the teams as needed', (done) => {
const dodgers = {id: 1, name: 'Dodgers'};
const lakers = {id: 2, name: 'Lakers'};
const teams = [dodgers]
const db = {
getTeam(teamId){
return Promise.resolve(teams.find(team => team.id === teamId));
},
saveTeam(teamId, teamName){
const createdTeam = {id: teamId, name: teamName};
teams.push(createdTeam);
return Promise.resolve(createdTeam);
}
}
startGame(db, dodgers, lakers)
.then(([home, away]) => {
expect(home).to.deep.eq(dodgers);
expect(away).to.deep.eq(lakers);
expect(teams).to.deep.include(lakers);
done();
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment