diff --git a/app/controllers/api/v1/diaper_calculators_controller.rb b/app/controllers/api/v1/diaper_calculators_controller.rb index 2b2bd06a9..e2a3b23f0 100644 --- a/app/controllers/api/v1/diaper_calculators_controller.rb +++ b/app/controllers/api/v1/diaper_calculators_controller.rb @@ -10,6 +10,12 @@ class DiaperCalculatorsController < ApplicationController { name: 'to_be_used_diapers_amount', result: 0 } ].freeze + LOW = 'LOW' + MEDIUM = 'MEDIUM' + HIGH = 'HIGH' + + DIAPER_TITLE = 'diaper' + def create result = diapers_service_handler(childs_age).calculate! diapers_be_used = diapers_correct_form(result.to_be_used_diapers_amount) @@ -36,6 +42,20 @@ def diapers_service_handler(age) def childs_age params[:childs_age].to_i end + + def diaper + @diaper ||= Product.find_by(title: DIAPER_TITLE) + end + + def default_price + ProductPrice.find_by(category: MEDIUM, product: diaper) + end + + def product_price + selected_price = ProductPrice.find_by(category: params[:price_id], + product: diaper) + selected_price || default_price + end end end end diff --git a/app/javascript/ajax/calculate_result_button.js b/app/javascript/ajax/calculate_result_button.js index adf1a0b4f..19288f382 100644 --- a/app/javascript/ajax/calculate_result_button.js +++ b/app/javascript/ajax/calculate_result_button.js @@ -40,7 +40,8 @@ $(document).on('turbolinks:load', function() { } const formData = { - childs_age: $("#childs_years").val() * 12 + months + childs_age: $("#childs_years").val() * 12 + months, + price_id: $("#product_category").selectedIndex } $.ajax({ diff --git a/app/views/calculators/calculator.html.slim b/app/views/calculators/calculator.html.slim index 40312b95a..ae30ee1c4 100644 --- a/app/views/calculators/calculator.html.slim +++ b/app/views/calculators/calculator.html.slim @@ -25,14 +25,17 @@ - (0..6).each do |month| option value = month = month - - if FeatureFlag.get('feature_budget_category').active? + + - if FeatureFlag.get('feature_budget_category').active? p.form-a = t '.form_price' - select.custom-select#childs_months [required] - - ([(t('.form.budgetary')), (t('.form.medium')), (t('.form.premium'))]).each do |price| - option value = price - = price - + select.custom-select#product_category [required] + option + =t('calculators.calculator.form.budgetary') + option selected="" + =t('calculators.calculator.form.medium') + option + =t('calculators.calculator.form.premium') = form.text_field :baby_weight, placeholder:t('.form.baby_weight_label'), class:"form-input d-none" = form.text_field :diapers_per_day, placeholder: "Diapers per day", class:"form-input d-none" = render_email_receiver_checkbox diff --git a/db/seeds.rb b/db/seeds.rb index 211cdab8f..0c35c2172 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -38,19 +38,19 @@ product_type: hygiene_type ) -budgetary = ProductPrice.create!( +ProductPrice.create!( price: 4.99, category: 0, product: diaper ) -medium = ProductPrice.create!( +ProductPrice.create!( price: 6.36, category: 1, product: diaper ) -premium = ProductPrice.create!( +ProductPrice.create!( price: 8.21, category: 2, product: diaper diff --git a/spec/controllers/diaper_calculators_spec.rb b/spec/controllers/diaper_calculators_spec.rb index 7192af6b3..c1064074e 100644 --- a/spec/controllers/diaper_calculators_spec.rb +++ b/spec/controllers/diaper_calculators_spec.rb @@ -22,9 +22,67 @@ before do get :create end + it 'renders expected result' do expect(response.body).to eq(expected.to_json) end end end + + describe '#product_price' do + context 'when get awaited value' do + it 'first diaper price category returned' do + low = create(:product_price, :LOW) + controller.params[:price_id] = 0 + result = controller.send(:product_price) + expect(result).not_to eq(nil) + expect(result).to eq(low) + end + + it 'default diaper price category returned' do + medium = create(:product_price, :MEDIUM) + + controller.params[:price_id] = 1 + result = controller.send(:product_price) + expect(result).not_to eq(nil) + expect(result).to eq(medium) + end + + it 'last diaper price category returned' do + high = create(:product_price, :HIGH) + + controller.params[:price_id] = 2 + result = controller.send(:product_price) + expect(result).not_to eq(nil) + expect(result).to eq(high) + end + end + context 'when get unawaited value' do + it 'get unawaited number' do + medium = create(:product_price, :MEDIUM) + + controller.params[:price_id] = -1 + result = controller.send(:product_price) + expect(result).not_to eq(nil) + expect(result).to eq(medium) + end + it 'get nil' do + medium = create(:product_price, :MEDIUM) + + controller.params[:price_id] = nil + result = controller.send(:product_price) + expect(result).not_to eq(nil) + expect(result).to eq(medium) + end + end + end + + describe '#childs_age' do + context 'when default value' do + it 'child`s age selected' do + controller.params[:childs_age] = 1 + expect(controller.send(:childs_age)).to eq(1) + end + end + end end diff --git a/spec/factories/product_price.rb b/spec/factories/product_price.rb new file mode 100644 index 000000000..690b1abd4 --- /dev/null +++ b/spec/factories/product_price.rb @@ -0,0 +1,17 @@ +FactoryBot.define do + factory :product_price do + association :product, title: "diaper", id: 1 + trait :LOW do + category { :LOW } + price { 4.99 } + end + trait :MEDIUM do + category { :MEDIUM } + price { 6.36 } + end + trait :HIGH do + category { :HIGH } + price { 8.21 } + end + end +end diff --git a/spec/factories/product_types.rb b/spec/factories/product_types.rb index 6426e7d8a..485e44534 100644 --- a/spec/factories/product_types.rb +++ b/spec/factories/product_types.rb @@ -2,6 +2,6 @@ FactoryBot.define do factory :product_type do - title { 'Title' } + title { 'hygiene' } end end diff --git a/spec/factories/products.rb b/spec/factories/products.rb index 94e0a1058..1df30697e 100644 --- a/spec/factories/products.rb +++ b/spec/factories/products.rb @@ -2,6 +2,7 @@ FactoryBot.define do factory :product do - title { 'Diaper' } + title { 'diaper' } + association :product_type, title: "hygiene", id: 1 end end diff --git a/spec/features/product_category_spec.rb b/spec/features/product_category_spec.rb new file mode 100644 index 000000000..c0d3aeb79 --- /dev/null +++ b/spec/features/product_category_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'rails_helper' + +BUDGETARY_OPTION = 'budgetary' +MEDIUM_OPTION = 'medium' +PREMIUM_OPTION = 'premium' + +describe 'product category dropdown list', js: true do + let(:calculator) { create(:calculator) } + before do + FeatureFlag.get('feature_budget_category').activate + visit '/calculator' + find(:select, 'product_category') + has_select?('product_category', with_options: [BUDGETARY_OPTION, MEDIUM_OPTION, PREMIUM_OPTION]) + end + + it 'default product category' do + expect(page).to have_select('product_category', selected: MEDIUM_OPTION) + end + + it 'custom product category selected' do + select(BUDGETARY_OPTION, from: 'product_category') + expect(page).to have_select('product_category', selected: BUDGETARY_OPTION) + end +end diff --git a/spec/models/product_price_spec.rb b/spec/models/product_price_spec.rb new file mode 100644 index 000000000..3b0c3a5e7 --- /dev/null +++ b/spec/models/product_price_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Product, type: :model do + describe 'factory' do + context 'product prices factory not nil' do + it 'low' do + expect(create(:product_price, :LOW)).not_to eq(nil) + end + it 'medium' do + expect(create(:product_price, :MEDIUM)).not_to eq(nil) + end + it 'high' do + expect(create(:product_price, :HIGH)).not_to eq(nil) + end + end + end +end