Skip to content

Instantly share code, notes, and snippets.

@z2015
Created May 21, 2016 09:03
Show Gist options
  • Save z2015/bee3b72affe6e18be27e2debec579173 to your computer and use it in GitHub Desktop.
Save z2015/bee3b72affe6e18be27e2debec579173 to your computer and use it in GitHub Desktop.
Immutable and Mocha and Chai Pure Function Test
import {expect} from 'chai';
import {List, Map} from 'immutable';
//set Entries function
function setEntries(state, entries){
return state.set('entries', List(entries));
}
//get Winners
function getWinners(vote){
if (!vote) return [];
const [a,b] = vote.get('pair');
const aVotes = vote.getIn(['tally', a], 0);
const bVotes = vote.getIn(['tally', b], 0);
if (aVotes > bVotes) {
return [a];
} else if (aVotes < bVotes) {
return [b];
} else {
return [a,b];
}
}
//next Vote function
function next(state) {
const entries = state.get('entries').concat(getWinners(state.get('vote')));
if (entries.size === 1){
return state.remove('vote').remove('entries').set('winner',entries.first());
} else {
return state.merge({
entries: entries.skip(2),
vote: Map({pair: entries.take(2)})
});
}
}
//ongoing Vote function
function vote(state, entry){
return state.updateIn(
['vote', 'tally', entry],
0,
tally => tally+1
)
}
describe('WillMoCha', ()=>{
//A list
describe('A List', () => {
function addMovie(currentState, movie) {
return currentState.unshift(movie);
}
it('is immutable', () => {
let state = List.of('Trainspotting', '28 Days Later');
let nextState = addMovie(state, 'Sunshine');
expect(nextState).to.equal(List.of(
'Sunshine',
'Trainspotting',
'28 Days Later'
));
expect(state).to.equal(List.of(
'Trainspotting',
'28 Days Later'
));
});
});
//A tree
describe('A tree', ()=>{
function addMovie(currentState, movie){
return currentState.update('movies', movies => movies.unshift(movie));
}
it('is immutable tree', ()=>{
let state = Map({
movies: List.of('Game of Thrones', 'Zoopita')
});
let nextState = addMovie(state, 'Sunshine');
expect(nextState).to.equal(Map({
movies: List.of(
'Sunshine',
'Game of Thrones',
'Zoopita'
)
}));
expect(state).to.equal(Map({
movies: List.of(
'Game of Thrones',
'Zoopita'
)
}));
});
});
//set Entries
describe('setEntries',()=>{
it('adds the entries to the state', ()=>{
const state = Map();
const entries = List.of('Trainspotting', '28 Days Later');
const nextState = setEntries(state, entries);
expect(nextState).to.equal(Map({
entries: List.of('Trainspotting', '28 Days Later')
}));
});
});
//next
describe('next', ()=>{
it('Take the next two entries under vote',()=>{
const state = Map({
entries: List.of('Trainspotting', '28 Days Later', 'Sunshine')
});
const nextState = next(state);
expect(nextState).to.equal(Map({
vote: Map({
pair: List.of('Trainspotting', '28 Days Later')
}),
entries: List.of('Sunshine')
}));
});
it('puts winner of current vote back to entries', ()=>{
const state = Map({
vote: Map({
pair: List.of('Trainspotting', '28 Days Later'),
tally: Map({
'Trainspotting': 4,
'28 Days Later': 2
})
}),
entries: List.of('Sunshine','Millions','127 Hours')
});
const nextState = next(state);
expect(nextState).to.equal(Map({
vote: Map({
pair: List.of('Sunshine', 'Millions')
}),
entries: List.of('127 Hours', 'Trainspotting')
}));
});
it('puts both from tied vote back to entries', ()=>{
const state = Map({
vote: Map({
pair: List.of('Trainspotting', '28 Days Later'),
tally: Map({
'Trainspotting': 3,
'28 Days Later': 3
})
}),
entries: List.of('Sunshine','Millions','127 Hours')
});
const nextState = next(state);
expect(nextState).to.equal(Map({
vote: Map({
pair: List.of('Sunshine', 'Millions')
}),
entries: List.of('127 Hours', 'Trainspotting', '28 Days Later')
}));
});
it('marks winner when just one entry left', ()=>{
const state = Map({
vote: Map({
pair: List.of('Trainspotting', '28 Days Later'),
tally: Map({
'Trainspotting': 4,
'28 Days Later': 2
})
}),
entries: List()
});
const nextState = next(state);
expect(nextState).to.equal(Map({
winner: 'Trainspotting'
}));
});
});
//vote
describe('vote', ()=>{
it('create a tally for the voted entry', ()=>{
const state = Map({
vote: Map({
pair: List.of('Trainspotting', '28 Days Later')
}),
entries: List()
});
const nextState = vote(state, 'Trainspotting');
expect(nextState).to.equal(Map({
vote: Map({
pair: List.of('Trainspotting','28 Days Later'),
tally: Map({
'Trainspotting' : 1
})
}),
entries: List()
}));
});
it('adds to existing tally for the voted entry', ()=>{
const state = Map({
vote: Map({
pair: List.of('Trainspotting', '28 Days Later'),
tally: Map({
'Trainspotting':3,
'28 Days Later':2
})
}),
entries: List()
});
const nextState = vote(state, 'Trainspotting');
expect(nextState).to.equal(Map({
vote:Map({
pair: List.of('Trainspotting', '28 Days Later'),
tally: Map({
Trainspotting: 4,
'28 Days Later': 2
})
}),
entries: List()
}));
})
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment