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

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

  describe('setEntries', () => {

    it('adds the entries to the state', () => {
      const state = Map();
      const entries = List.of('Travis Rice', 'Joe Sexton');
      const nextState = setEntries(state, entries);

      expect(nextState).to.equal(Map({
        entries: List.of(
          'Travis Rice',
          'Joe Sexton'
        )
      }));
    });

    it('converts to immutable', () => {
      const state = Map();
      const entries = List.of('Travis Rice', 'Joe Sexton');
      const nextState = setEntries(state, entries);

      expect(nextState).to.equal(Map({
        entries: List.of(
          'Travis Rice',
          'Joe Sexton'
        )
      }));
    });

  });

  describe('next', () => {

    it('takes the next two entries under vote', () => {
      const state = Map({
        entries: List.of(
          'Jeremy Jones',
          'Alex Andrews',
          'Nicolas Müller'
        )
      });
      const nextState = next(state);

      expect(nextState).to.equal(Map({
        vote: Map({
          pair: List.of(
            'Jeremy Jones',
            'Alex Andrews'
          )
        }),
        entries: List.of('Nicolas Müller')
      }));
    });

  });

  describe('vote', () => {

    it('creates a tally for a voted entry', () => {
      const state = Map({
        vote: Map({
          pair: List.of(
            'Jeremy Jones',
            'Alex Andrews'
          )
        }),
        entries: List()
      });
      const nextState = vote(state, 'Jeremy Jones');

      expect(nextState).to.equal(Map({
        vote: Map({
          pair: List.of(
            'Jeremy Jones',
            'Alex Andrews'
          ),
          tally: Map({
            'Jeremy Jones': 1
          })
        }),
        entries: List()
      }));
    });

    it('adds to existing tally for the voted entry', () => {
      const state = Map({
        vote: Map({
          pair: List.of(
            'Jeremy Jones',
            'Alex Andrews'
          ),
          tally: Map({
            'Jeremy Jones': 5,
            'Alex Andrews': 2
          }),
          entries: List()
        })
      });
      const nextState = vote(state, 'Jeremy Jones');

      expect(nextState).to.equal(Map({
        vote: Map({
          pair: List.of(
            'Jeremy Jones',
            'Alex Andrews'
          ),
          tally: Map({
            'Jeremy Jones': 6,
            'Alex Andrews': 2
          }),
          entries: List()
        })
      }));
    });

    it('puts winner of current vote back bottom of entries', () => {
      const state = Map({
        vote: Map({
          pair: List.of('Jeremy Jones', 'Alex Andrews'),
          tally: Map({
            'Jeremy Jones': 5,
            'Alex Andrews': 2
          })
        }),
        entries: List.of('Travis Rice', 'Joe Sexton', 'Nicolas Müller')
      });
      const nextState = next(state);

      expect(nextState).to.equal(Map({
        vote: Map({
          pair: List.of('Travis Rice', 'Joe Sexton')
        }),
        entries: List.of('Nicolas Müller', 'Jeremy Jones')
      }));
    });

  });

});

@@ -3256,23 +3256,632 @@
 );%0A    %7D);%0A%0A
+    it('puts both from tied vote back to entries', () =%3E %7B%0A      const state = Map(%7B%0A        vote: Map(%7B%0A          pair: List.of('Jeremy Jones', 'Alex Andrews'),%0A          tally: Map(%7B%0A            'Jeremy Jones': 3,%0A            'Alex Andrews': 3%0A          %7D)%0A        %7D),%0A        entries: List.of('Travis Rice', 'Joe Sexton', 'Nicolas M%C3%BCller')%0A      %7D);%0A      const nextState = next(state);%0A%0A      expect(nextState).to.equal(Map(%7B%0A        vote: Map(%7B%0A          pair: List.of('Travis Rice', 'Joe Sexton')%0A        %7D),%0A        entries: List.of('Nicolas M%C3%BCller', 'Jeremy Jones', 'Alex Andrews')%0A      %7D))%0A    %7D);%0A%0A
   %7D);%0A%0A%7D);%0A

Implement test spec for putting tied votes back into entries.