Skip to content

Commit d1b81e4

Browse files
feat(voting): add function for managing next voting entries
1 parent 2c4ccbe commit d1b81e4

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/core.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Core application logic
33
*/
4-
import { List } from 'immutable';
4+
import { List, Map } from 'immutable';
55

66
/**
77
* set the entries of the app's state
@@ -12,3 +12,18 @@ import { List } from 'immutable';
1212
export function setEntries(state, entries = []) {
1313
return state.set('entries', new List(entries));
1414
}
15+
16+
/**
17+
* extracts the next two entries for vote
18+
* e.g: the first two entries in the list.
19+
* @param {Immutable.Map} state the application's current state
20+
* @return {Immutable.Map} modified state
21+
*/
22+
export function next(state) {
23+
const entries = state.get('entries');
24+
25+
return state.merge({
26+
vote: new Map({ pair: entries.take(2) }),
27+
entries: entries.skip(2)
28+
});
29+
}

test/core_spec.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { expect } from 'chai';
22
import { List, Map } from 'immutable';
33

4-
import { setEntries } from '../src/core';
4+
import { setEntries, next } from '../src/core';
55

66
// describe application logic
77
describe('Application Logic', () => {
8-
describe('setEntries', () => {
9-
let state = null;
8+
let state = null;
109

10+
describe('setEntries', () => {
1111
beforeEach(() => {
1212
// runs before each test in this block
1313
state = new Map();
@@ -32,4 +32,24 @@ describe('Application Logic', () => {
3232
expect(nextState.get('entries')).to.equal(List.of(...entries));
3333
});
3434
});
35+
36+
describe('next', () => {
37+
beforeEach(() => {
38+
state = new Map({
39+
entries: List.of('Kill Bill', 'Pulp Fiction', 'Reservoir Dogs')
40+
});
41+
});
42+
43+
// the next vote reducer should take the next two entries
44+
// in the state and put them under vote. it should also remove
45+
// those voting entries from the 'entries' map in the state.
46+
it('takes the next two entries under vote', () => {
47+
const nextState = next(state);
48+
49+
expect(nextState).to.include.key('vote');
50+
expect(nextState.get('vote')).to.include.key('pair');
51+
expect(nextState.get('vote').get('pair')).to.have.sizeOf(2);
52+
expect(nextState.get('vote').get('pair')).to.equal(List.of('Kill Bill', 'Pulp Fiction'));
53+
});
54+
});
3555
});

0 commit comments

Comments
 (0)