require 'rails_helper'

RSpec.describe TaskListsController, type: :controller do

  render_views

  before(:each) do
    request.accept = "application/json"
  end

  let(:user) {
    create(:user)
  }

  let(:valid_session) { {user_id: user.id} }

  describe 'GET #index' do
    it 'assigns all user lists as @task_lists' do
      l1 = create(:task_list, user: user)
      l2 = create(:task_list, user: user)
      create(:task_list)

      get :index, params: {}, session: valid_session

      expect(assigns(:task_lists)).to contain_exactly(l1, l2)
    end
  end

  describe 'GET #show' do
    it 'assigns @task_list' do
      l = create(:task_list, user: user)
      get :show, params: {id: l.id}, session: valid_session
      expect(assigns(:task_list)).to eq l
    end
  end

  describe 'POST #create' do
    it 'creates a task_list' do
      expect do
        post :create, params: {task_list: {name: 'name'}}, session: valid_session
      end.to change(TaskList, :count).by 1
    end
  end

  describe 'PATCH #update' do
    it 'updates a task_list' do
      l = create(:task_list, user: user)
      patch :update, params: {id: l.id, task_list: {name: 'new name'}}, session: valid_session
      l.reload
      expect(l.name).to eq 'new name'
    end
  end

  describe 'DELETE #destroy' do
    it 'destroys the list' do
      l = create(:task_list, user: user)
      delete :destroy, params: {id: l.id}, session: valid_session
      expect(TaskList.where(id: l.id).count).to eq 0
    end
  end

end