Skip to content

Commit

Permalink
added calculator new specs; fixed controller;
Browse files Browse the repository at this point in the history
  • Loading branch information
ktpe committed Nov 19, 2024
1 parent b1bb9aa commit 2eedb17
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
9 changes: 7 additions & 2 deletions app/controllers/account/calculators_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

class Account::CalculatorsController < Account::BaseController
before_action :resource, only: [:edit, :update, :destroy]
load_and_authorize_resource

def index
Expand All @@ -23,6 +22,8 @@ def new
end

def edit
@calculator = resource

collect_fields_for_form
end

Expand All @@ -37,6 +38,8 @@ def create
end

def update
@calculator = resource

if updater
redirect_to edit_account_calculator_path(slug: @calculator), notice: t("notifications.calculator_updated")
else
Expand All @@ -47,6 +50,8 @@ def update
end

def destroy
@calculator = resource

@calculator.destroy

redirect_to account_calculators_path, notice: t("notifications.calculator_deleted"), status: :see_other
Expand All @@ -59,7 +64,7 @@ def collection
end

def resource
Calculator.find(params[:slug])
collection.friendly.find(params[:slug])
end

def collect_fields_for_form
Expand Down
3 changes: 1 addition & 2 deletions spec/factories/calculators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
FactoryBot.define do
factory :calculator do
en_name { "English Calculator" }
uk_name { "Український калькуялтор" }
slug { "calculator" }
uk_name { "Український калькулятор" }
end
end
2 changes: 1 addition & 1 deletion spec/factories/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#
FactoryBot.define do
factory :field do
kind { 0 }
kind { "number" }
en_label { "Label" }
uk_label { "Label" }
var_name { "var" }
Expand Down
51 changes: 51 additions & 0 deletions spec/requests/account/calculators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

let!(:calculator) { create(:calculator) }

let(:user) { create(:user) }
let(:locale) { "en" }
let(:new_path) { new_account_calculator_path(locale: locale) }

describe "DELETE #destroy" do
it "destroys the calculator and redirects" do
expect do
Expand Down Expand Up @@ -40,4 +44,51 @@
end
end
end

describe "GET #new" do
subject { get new_path }

context "when the user is authorized" do
it "initializes a new Calculator object with fields and formulas" do
subject

expect(response).to have_http_status(:ok)

calculator = assigns(:calculator)
expect(calculator).to be_a_new(Calculator)

expect(calculator.fields.size).to eq(1)
expect(calculator.formulas.size).to eq(1)

expect(calculator.fields.first).to be_a_new(Field)
expect(calculator.formulas.first).to be_a_new(Formula)
end

it "renders the new template" do
subject
expect(response).to render_template(:new)
end
end

context "when the locale is different" do
let(:locale) { "uk" }

it "handles the locale correctly" do
subject
expect(I18n.locale).to eq(:uk)
expect(response).to have_http_status(:ok)
end
end

context "when the user is not logged in" do
before do
sign_out(:user)
end

it "redirects to the login page" do
subject
expect(response).to redirect_to(new_user_session_path)
end
end
end
end

0 comments on commit 2eedb17

Please sign in to comment.