import expect from 'expect.js';
import Immutable from 'immutable';

import todos from '../../js/reducers/todos';

describe('todos reducer', () => {
  const state = Immutable.fromJS({
    todoList: [{
      id: 1,
      index: 1,
      isComplete: true,
      label: 'Hello'
    }, {
      id: 2,
      index: 2,
      isComplete: false,
      label: 'World'
    }]
  });

  it('exposes a function', () => {
    expect(todos).to.be.a('function');
  });

  describe('addTodo handler', () => {
    const action = {
      type: 'ADD_TODO',
      payload: {
        todo: {
          id: 3,
          label: 'New',
          isComplete: false
        }
      }
    };

    it('appends a new todo', () => {
      const subject = todos(state, action).get('todoList');
      expect(subject.size).to.equal(3);
      expect(subject.last().get('label')).to.equal('New');
    });
  });

  describe('clearCompleteTodos handler', () => {
    const action = {
      type: 'CLEAR_COMPLETE_TODOS'
    };

    it('removes todos where isComplete = true', () => {
      const subject = todos(state, action).get('todoList');
      expect(subject.size).to.equal(1);
      expect(subject.every(todo => !todo.get('isComplete'))).to.be(true);
    });
  });

  describe('deleteTodo handler', () => {
    const action = {
      type: 'DELETE_TODO',
      payload: {
        id: 2
      }
    };

    it('removes the correct todo', () => {
      const subject = todos(state, action).get('todoList');
      expect(subject.size).to.equal(1);
      expect(subject.every(todo => todo.get('id') !== 2)).to.be(true);
    });
  });

  describe('editTodo handler', () => {
    const action = {
      type: 'EDIT_TODO',
      payload: {
        id: 2,
        label: 'New label'
      }
    };

    it('modifies the correct todo', () => {
      const subject = todos(state, action)
        .get('todoList')
        .find(todo => todo.get('id') === 2);

      expect(subject.get('label')).to.equal('New label');
    });
  });


  describe('fetchAllTodos handler', () => {
    const action = {
      type: 'FETCH_ALL_TODOS',
      payload: {
        id: 2,
        todos: [{
          id: 1,
          label: 'A couple',
          isComplete: true
        }, {
          id: 2,
          label: 'of new',
          isComplete: true
        }, {
          id: 3,
          label: 'todos',
          isComplete: true
        }, {
          id: 4,
          label: 'all completed',
          isComplete: true
        }]
      }
    };

    it('sets todoList to the new fetched todos', () => {
      const subject = todos(state, action).get('todoList');
      expect(subject.size).to.equal(4);
      expect(subject.every(todo => todo.get('isComplete'))).to.be(true);
    });
  });

  describe('markAllTodos handler', () => {
    const action = {
      type: 'MARK_ALL_TODOS',
      payload: {
        id: 2,
        isComplete: true
      }
    };

    it('modifies all todos', () => {
      const subject = todos(state, action).get('todoList');
      expect(subject.every(todo => todo.get('isComplete'))).to.be(true);
    });
  });

  describe('markTodo handler', () => {
    const action = {
      type: 'MARK_TODO',
      payload: {
        id: 2,
        isComplete: true
      }
    };

    it('modifies the correct todo', () => {
      const subject = todos(state, action)
        .get('todoList')
        .find(todo => todo.get('id') === 2);

      expect(subject.get('isComplete')).to.be(true);
    });
  });

  describe('moveTodo handler', () => {
    const action = {
      type: 'MOVE_TODO',
      payload: {
        at: 2,
        to: 1
      }
    };

    it('modifies the todo list indices correctly', () => {
      const subject = todos(state, action).get('todoList');

      expect(subject.first().get('index')).to.equal(2);
      expect(subject.last().get('index')).to.equal(1);
    });
  });
});

@@ -126,16 +126,8 @@
 odos
- reducer
 ', (
@@ -133,16 +133,16 @@
 () =%3E %7B%0A
+
   const 
@@ -440,24 +440,23 @@
  %7D);%0A%0A  
-describe
+context
 ('addTod
@@ -452,32 +452,24 @@
 ext('addTodo
- handler
 ', () =%3E %7B%0A 
@@ -853,24 +853,23 @@
  %7D);%0A%0A  
-describe
+context
 ('clearC
@@ -876,32 +876,24 @@
 ompleteTodos
- handler
 ', () =%3E %7B%0A 
@@ -1202,24 +1202,23 @@
  %7D);%0A%0A  
-describe
+context
 ('delete
@@ -1217,32 +1217,24 @@
 ('deleteTodo
- handler
 ', () =%3E %7B%0A 
@@ -1558,24 +1558,23 @@
  %7D);%0A%0A  
-describe
+context
 ('editTo
@@ -1571,32 +1571,24 @@
 xt('editTodo
- handler
 ', () =%3E %7B%0A 
@@ -1941,24 +1941,23 @@
 %7D);%0A%0A%0A  
-describe
+context
 ('fetchA
@@ -1959,32 +1959,24 @@
 etchAllTodos
- handler
 ', () =%3E %7B%0A 
@@ -2677,32 +2677,31 @@
 );%0A  %7D);%0A%0A  
-describe
+context
 ('markAllTod
@@ -2702,24 +2702,16 @@
 AllTodos
- handler
 ', () =%3E
@@ -3024,24 +3024,23 @@
  %7D);%0A%0A  
-describe
+context
 ('markTo
@@ -3037,32 +3037,24 @@
 xt('markTodo
- handler
 ', () =%3E %7B%0A 
@@ -3399,24 +3399,23 @@
  %7D);%0A%0A  
-describe
+context
 ('moveTo
@@ -3420,16 +3420,8 @@
 Todo
- handler
 ', (

Refactor tests