# == Schema Information
#
# Table name: projects
#
#  id             :integer          not null, primary key
#  title          :string           not null
#  description    :string
#  status         :integer          default(0)
#  status_message :string           not null
#  created_at     :datetime         not null
#  updated_at     :datetime         not null
#

require 'rails_helper'

RSpec.describe Project, type: :model do
  it 'has a valid factory' do
    expect(create(:project)).to be_valid
  end
  it 'requires a title' do
    record = build(:project, title: nil)
    record.valid?
    expect(record.errors[:title].size).to eq(1)
  end
  it 'requires a status_message' do
    record = build(:project, status_message: nil)
    record.valid?
    expect(record.errors[:status_message].size).to eq(1)
  end

  describe 'status enum' do
    before :each do
      @proj = create(:project)
    end
    # no need to test ActiveRecord::Enum methods but
    # do need to make sure we've declared the enum correctly
    it 'new Project default to a status of :not_set' do
      expect(@proj.status.to_sym).to eq(:not_set)
    end
    it 'status enum of :working is valid' do
      expect { @proj.working! }.not_to raise_error
    end
    # TODO: test full list of status options
    it 'status enum of :whatever is invalid' do
      expect { @proj.whatever! }.to raise_error(NoMethodError)
    end
  end

  describe 'methods' do
    describe '#valid_status?' do
      it "returns true for a valid status" do
        expect(Project.valid_status?(:working)).to be_truthy
      end
      it "returns false for a valid status" do
        expect(Project.valid_status?(:whatever)).to be_falsey
      end
    end
    describe '#update_status(status)' do
      before :each do
        @proj = create(:project, status: Project.statuses[:working])
      end
      it "sets a valid status" do
        @proj.update_status(:done)
        expect(@proj.status).to eq('done')
      end
      it "does not set an invalid status" do
        @proj.update_status(:whatever)
        expect(@proj.status).to eq('working')
      end
    end
    describe '#with_status(status)' do
      it "return records with the requested status" do
        proj1 = create(:project, status: Project.statuses[:working])
        proj2 = create(:project, status: Project.statuses[:working])
        proj3 = create(:project, status: Project.statuses[:error])
        working_projects = Project.with_status(:working)
        expect(working_projects).to include(proj1, proj2)
        expect(working_projects).not_to include(proj3)
      end
    end
    describe '#status_names_for_select' do
      it "returns an array of arrays" do
        response = Project.status_names_for_select
        expect(response).to be_kind_of(Array)
        expect(response[0]).to be_kind_of(Array)
      end
    end
  end
end

