Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olexandervanzuriak committed Jan 17, 2025
1 parent da0aad9 commit 4c6eb20
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion spec/features/show_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@

RSpec.describe CalculatorsController, type: :controller do
describe "GET #show" do
let(:calculator) { create(:calculator) }
let(:calculator) do
create(:calculator,
en_name: "Test Calculator",
uk_name: "Тестовий Калькулятор",
fields_attributes: [
{ en_label: "Field A", uk_label: "Поле A", var_name: "a", kind: "number" },
{ en_label: "Field B", uk_label: "Поле B", var_name: "b", kind: "number" }
],
formulas_attributes: [
{ expression: "a + b", en_label: "Formula 1", uk_label: "Формула 1", en_unit: "unit", uk_unit: "одиниця" },
{ expression: "a + b", en_label: "Formula 2", uk_label: "Формула 2", en_unit: "unit", uk_unit: "одиниця" }
])
end

let(:expected_result) do
[
{ label: "Formula 1", result: 0, unit: "unit", formula_image: "/assets/money_spent.png" },
{ label: "Formula 2", result: 0, unit: "unit", formula_image: "/assets/money_spent.png" }
]
end

include_context :enable_calculators_constructor

Expand All @@ -12,7 +31,35 @@

it "assigns the correct calculator to @calculator" do
get :show, params: { slug: calculator.slug, locale: :en }

expect(assigns(:calculator)).to eq(calculator)
end

it "assigns the correct initial results" do
get :show, params: { slug: calculator.slug, locale: :en }

expect(assigns(:results)).to eq(expected_result)
end

context "when formulas have images attached" do
before do
formula_with_image = calculator.formulas.first
allow(formula_with_image).to receive(:formula_image).and_return(double("FormulaImage", attached?: true))
allow(controller).to receive(:rails_blob_path).and_return("/rails/active_storage/blobs/formula_image.jpg")
end

let(:expected_result_with_image) do
[
{ label: "Formula 1", result: 0, unit: "unit", formula_image: "/rails/active_storage/blobs/formula_image.jpg" },
{ label: "Formula 2", result: 0, unit: "unit", formula_image: "/assets/money_spent.png" }
]
end

it "assigns the correct formula images" do
get :show, params: { slug: calculator.slug, locale: :en }

expect(assigns(:results)).to eq(expected_result_with_image)
end
end
end
end

0 comments on commit 4c6eb20

Please sign in to comment.