require 'rails_helper'

RSpec.describe RecipeIngredient, type: :model do

  describe 'to_mass' do

    it 'converts volume ingredients with density' do
      ri = RecipeIngredient.new(quantity: 2, units: 'tbsp', ingredient: create(:ingredient_with_density))
      expect(ri.as_value_unit.mass?).to be_falsey
      ri.to_mass
      expect(ri.as_value_unit.mass?).to be_truthy
    end

    it 'converts ingredients with custom units' do
      i = create(:ingredient_with_density)
      i.ingredient_units << IngredientUnit.new(name: 'pat', gram_weight: 25)
      ri = RecipeIngredient.new(quantity: 2, units: 'pat', ingredient: i)
      ri.to_mass
      vu = ri.as_value_unit
      expect(vu.raw_value).to eq 50
      expect(vu.unit.to_s).to eq 'gram'
    end

  end

  describe '#can_convert_to_grams' do
    it 'returns false if no quantity or unit' do
      ri = RecipeIngredient.new
      expect(ri.can_convert_to_grams?).to be_falsey
    end

    it 'returns false if no quantity' do
      ri = RecipeIngredient.new(units: 'lbs')
      expect(ri.can_convert_to_grams?).to be_falsey
    end

    it 'returns false if no units' do
      ri = RecipeIngredient.new(quantity: '5 1/2')
      expect(ri.can_convert_to_grams?).to be_falsey
    end

    it 'returns false if weird units' do
      ri = RecipeIngredient.new(quantity: '5 1/2', units: 'dogs')
      expect(ri.can_convert_to_grams?).to be_falsey
    end

    it 'returns true if unit is mass' do
      ri = RecipeIngredient.new(quantity: '5 1/2', units: 'lbs')
      expect(ri.can_convert_to_grams?).to be_truthy
    end

    it 'returns false if unit is volume and there is no ingredient' do
      ri = RecipeIngredient.new(quantity: '5 1/2', units: 'cups')
      expect(ri.can_convert_to_grams?).to be_falsey
    end

    it 'returns false if unit is volume and ingredient has no density' do
      ri = RecipeIngredient.new(quantity: '5 1/2', units: 'cups', ingredient: create(:ingredient))
      expect(ri.can_convert_to_grams?).to be_falsey
    end

    it 'returns false if unit is volume and ingredient has density' do
      ri = RecipeIngredient.new(quantity: '5 1/2', units: 'cups', ingredient: create(:ingredient_with_density))
      expect(ri.can_convert_to_grams?).to be_truthy
    end
  end

end
