import {List, Map} from 'immutable';
import {expect} from 'chai';
import {setEntries, next, vote} from '../src/core';

describe('application logic', () => {

    describe('setEntries', () => {

        it('adds the entries to the state', () => {
            const state = Map();
            const entries = ['Trainspotting', '28 Days Later'];
            const nextState = setEntries(state, entries);
            expect(nextState).to.equal(Map({
                entries: List.of('Trainspotting', '28 Days Later')
            }));
        });
    });

    describe('next', () => {

        it('takes the next two entries under vote', () => {
            const state = Map({
                entries: List.of('Trainspotting', '28 Days Later', 'Sunshine')
            });
            const nextState = next(state);
            console.log(nextState)
            expect(nextState).to.equal(Map({
                vote: Map({
                    pair: List.of('Trainspotting', '28 Days Later')
                }),
                entries: List.of('Sunshine')
            }));
        });
        it('puts the winner of the current vote back to the 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 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'
            }));
        });

    });

    describe('vote', () => {

        it('creates 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 an 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()
            }));
        });
    });

});


@@ -3232,38 +3232,8 @@
 p(%7B%0A
-               vote: Map(%7B%0A   
     
@@ -3296,58 +3296,8 @@
 r')%0A
-               %7D),%0A               entries: List()%0A
     
@@ -3417,40 +3417,8 @@
 p(%7B%0A
-                vote: Map(%7B%0A    
     
@@ -3486,36 +3486,32 @@
                 
-    
 tally: Map(%7B%0A   
@@ -3519,36 +3519,32 @@
                 
-   
- 
 'Trainspotting':
@@ -3566,67 +3566,11 @@
     
-    
 %7D)%0A
-                %7D),%0A                entries: List()%0A
     
@@ -3698,40 +3698,8 @@
 p(%7B%0A
-                vote: Map(%7B%0A    
     
@@ -3767,36 +3767,32 @@
                 
-    
 tally: Map(%7B%0A   
@@ -3800,36 +3800,32 @@
                 
- 
-   
 'Trainspotting':
@@ -3820,36 +3820,32 @@
 inspotting': 3,%0A
-    
                 
@@ -3887,65 +3887,9 @@
     
-    %7D)%0A                %7D),%0A                entries: List(
+%7D
 )%0A  
@@ -4011,40 +4011,8 @@
 p(%7B%0A
-                vote: Map(%7B%0A    
     
@@ -4080,36 +4080,32 @@
                 
-    
 tally: Map(%7B%0A   
@@ -4113,36 +4113,32 @@
                 
- 
-   
 'Trainspotting':
@@ -4153,36 +4153,32 @@
                 
-   
- 
 '28 Days Later':
@@ -4200,66 +4200,9 @@
     
-    %7D),%0A                %7D),%0A                entries: List(
+%7D
 )%0A  

update core tests to reflect change