From 4c6eb20fdc4ca9610f11a377a7e12ca438043490 Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Fri, 17 Jan 2025 19:28:24 +0200 Subject: [PATCH] Added tests --- spec/features/show_calculator_spec.rb | 49 ++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/spec/features/show_calculator_spec.rb b/spec/features/show_calculator_spec.rb index 5b8cdbcd1..84111b871 100644 --- a/spec/features/show_calculator_spec.rb +++ b/spec/features/show_calculator_spec.rb @@ -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 @@ -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