# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Job do
  it { should belong_to(:user) }
  it { should belong_to(:city) }
  it { should have_and_belong_to_many(:tags) }
  it { should validate_presence_of(:title) }
  it { should validate_presence_of(:description) }
  it { should validate_presence_of(:how_to_apply) }
  it { should validate_presence_of(:user) }
  it { should validate_presence_of(:city) }

  describe '#to_param' do
    it 'returns the parameterized title' do
      job = described_class.make(id: 12, title: 'Some Title')
      expect(job.to_param).to eq('12-some-title')
    end
  end

  describe '.open' do
    it 'returns only the enabled records' do
      open = described_class.make! open: true
      described_class.make! open: false

      expect(described_class.open).to eq([open])
    end
  end

  describe '#state_id' do
    it 'returns the state id' do
      city = City.new state_id: 1

      expect do
        subject.city = city
      end.to change { subject.state_id }.from(nil).to(1)

      expect do
        subject.state_id = '2'
      end.to change { subject.state_id }.to(2)
    end
  end

  describe '#belongs_to?' do
    let(:owner) { User.new(id: 1) }
    let(:other_user) { User.new(id: 2) }
    let(:job) { Job.new(user: owner) }

    it 'returns true when user owns job' do
      expect(job.belongs_to?(owner)).to be(true)
    end

    it 'returns true when user owns job' do
      expect(job.belongs_to?(other_user)).to be(false)
    end
  end
end
