From 3bf95ba352f2506cf13204ee35740fe78aeac2f0 Mon Sep 17 00:00:00 2001 From: OlexanderVanzuriak Date: Wed, 27 Nov 2024 13:24:59 +0200 Subject: [PATCH] Added calculation service helper test --- .../calculation_service_helper_spec.rb | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 spec/helpers/calculators/calculation_service_helper_spec.rb diff --git a/spec/helpers/calculators/calculation_service_helper_spec.rb b/spec/helpers/calculators/calculation_service_helper_spec.rb new file mode 100644 index 000000000..d095e2bb3 --- /dev/null +++ b/spec/helpers/calculators/calculation_service_helper_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe Calculators::CalculationService, type: :helper do + let(:calculator) { instance_double("Calculator", formulas: formulas) } + let(:formulas) do + [ + instance_double("Formula", expression: "x + y", en_label: "Addition", en_unit: "units", uk_label: "Додавання", uk_unit: "одиниці"), + instance_double("Formula", expression: "x * y", en_label: "Multiplication", en_unit: "units", uk_label: "Множення", uk_unit: "одиниці") + ] + end + let(:inputs) { ActionController::Parameters.new({ x: 5, y: 3 }) } + + before do + allow_any_instance_of(ApplicationHelper).to receive(:current_locale?).with(:en).and_return(locale_en) + end + + describe "#perform" do + subject { described_class.new(calculator, inputs).perform } + + context "when locale is English" do + let(:locale_en) { true } + + it "returns results with English labels and units" do + expect(subject).to eq([ + { label: "Addition", result: 8, en_unit: "units" }, + { label: "Multiplication", result: 15, en_unit: "units" } + ]) + end + end + + context "when locale is Ukrainian" do + let(:locale_en) { false } + + it "returns results with Ukrainian labels and units" do + expect(subject).to eq([ + { label: "Додавання", result: 8, en_unit: "одиниці" }, + { label: "Множення", result: 15, en_unit: "одиниці" } + ]) + end + end + end + end + \ No newline at end of file