const assert = require('assert')
const Redux = require('redux')

const reducer = require('../../src/reducers/reducer')

describe('reducers', () => {
  it('has the correct default initial state', () => {
    const store = Redux.createStore(reducer)
    assert.deepEqual({ tasks: [] }, store.getState())
  })

  describe('TASKS_SELECT', () => {
    it('selects specified task', () => {
      const now = new Date().toISOString()
      const initialTasks = [
        { id: 1, key: 'issue-1', title: 'task', updatedAt: now },
        { id: 2, key: 'pull-2', title: 'more task', updatedAt: now },
      ]

      const selectedTask = {
        id: 2,
        key: 'pull-2',
        title: 'more task',
        updatedAt: now,
      }

      const store = Redux.createStore(reducer, { tasks: initialTasks })
      store.dispatch({ type: 'TASKS_SELECT', task: selectedTask })

      const expectedTasks = [
        { id: 1, key: 'issue-1', title: 'task', updatedAt: now },
        {
          id: 2,
          key: 'pull-2',
          title: 'more task',
          updatedAt: now,
          isSelected: true,
        },
      ]

      assert.deepEqual(expectedTasks, store.getState().tasks)
    })
  })

  describe('TASKS_DESELECT', () => {})

  describe('TASKS_SNOOZE', () => {})

  describe('TASKS_ARCHIVE', () => {})

  describe('TASKS_EMPTY', () => {
    it('empties the tasks list', () => {
      const initialTasks = [
        {
          id: 1,
          key: 'issue-1',
          title: 'task',
          updatedAt: '2016-06-15T20:14:46Z',
        },
      ]

      const store = Redux.createStore(reducer, { tasks: initialTasks })
      store.dispatch({ type: 'TASKS_EMPTY' })

      assert.deepEqual([], store.getState().tasks)
    })
  })

  describe('TASKS_UPDATE', () => {
    it('updates existing tasks', () => {
      const now = new Date().toISOString()
      const initialTasks = [
        { id: 1, key: 'issue-1', title: 'task', updatedAt: now },
        { id: 2, key: 'pull-2', title: 'more task', updatedAt: now },
      ]

      const updatedTasks = [
        { id: 1, key: 'issue-1', title: 'task', updatedAt: now },
        { id: 2, key: 'pull-2', title: 'updated title', updatedAt: now },
      ]

      const store = Redux.createStore(reducer, { tasks: initialTasks })
      store.dispatch({ type: 'TASKS_UPDATE', tasks: updatedTasks })

      const expectedTasks = [
        { id: 1, key: 'issue-1', title: 'task', updatedAt: now },
        { id: 2, key: 'pull-2', title: 'updated title', updatedAt: now },
      ]

      assert.deepEqual(expectedTasks, store.getState().tasks)
    })

    it('adds new tasks', () => {
      const now = new Date().toISOString()
      const initialTasks = [
        { id: 1, key: 'pull-1', title: 'task', updatedAt: now },
      ]

      const updatedTasks = [
        { id: 2, key: 'pull-2', title: 'new task', updatedAt: now },
      ]

      const store = Redux.createStore(reducer, { tasks: initialTasks })
      store.dispatch({ type: 'TASKS_UPDATE', tasks: updatedTasks })

      const expectedTasks = [
        { id: 1, key: 'pull-1', title: 'task', updatedAt: now },
        { id: 2, key: 'pull-2', title: 'new task', updatedAt: now },
      ]

      assert.deepEqual(expectedTasks, store.getState().tasks)
    })
  })
})

@@ -598,136 +598,8 @@
  %5D%0A%0A
-      const selectedTask = %7B%0A        id: 2,%0A        key: 'pull-2',%0A        title: 'more task',%0A        updatedAt: now,%0A      %7D%0A%0A
     
@@ -721,20 +721,25 @@
 sk: 
-selectedTask
+%7B key: 'pull-2' %7D
  %7D)%0A
@@ -1058,32 +1058,32 @@
 s)%0A    %7D)%0A  %7D)%0A%0A
-
   describe('TASK
@@ -1094,32 +1094,846 @@
 SELECT', () =%3E %7B
+%0A    it('deselects the specified task', () =%3E %7B%0A      const now = new Date().toISOString()%0A      const initialTasks = %5B%0A        %7B%0A          id: 1,%0A          key: 'issue-1',%0A          title: 'task',%0A          updatedAt: now,%0A          isSelected: true,%0A        %7D,%0A        %7B id: 2, key: 'pull-2', title: 'more task', updatedAt: now %7D,%0A      %5D%0A%0A      const store = Redux.createStore(reducer, %7B tasks: initialTasks %7D)%0A      store.dispatch(%7B type: 'TASKS_DESELECT', task: %7B key: 'issue-1' %7D %7D)%0A%0A      const expectedTasks = %5B%0A        %7B%0A          id: 1,%0A          key: 'issue-1',%0A          title: 'task',%0A          updatedAt: now,%0A          isSelected: false,%0A        %7D,%0A        %7B id: 2, key: 'pull-2', title: 'more task', updatedAt: now %7D,%0A      %5D%0A%0A      assert.deepEqual(expectedTasks, store.getState().tasks)%0A    %7D)%0A  
 %7D)%0A%0A  describe('

Add test for TASKS_DESELECT in reducer