1
0

Test pass: test_reapply_binding_target

Add Targets #reapply action
Explicit params[:date] conversion
This commit is contained in:
cryptogopher 2021-04-30 00:49:09 +02:00
parent 4eda035e47
commit 8cac724694
3 changed files with 65 additions and 44 deletions

View File

@ -6,7 +6,7 @@ class TargetsController < ApplicationController
include Concerns::Finders
before_action :find_goal_by_goal_id,
only: [:index, :new, :create, :edit, :update, :destroy, :toggle_exposure]
only: [:index, :new, :create, :edit, :update, :destroy, :reapply, :toggle_exposure]
before_action :find_quantity_by_quantity_id, only: [:toggle_exposure, :subthresholds]
before_action :authorize
#before_action :set_view_params
@ -22,7 +22,7 @@ class TargetsController < ApplicationController
end
def create
@effective_from = params[:goal].delete(:effective_from)
@effective_from = params[:goal].delete(:effective_from).to_date
params[:goal][:targets_attributes].each { |ta| ta[:effective_from] = @effective_from }
if @goal.update(targets_params)
@ -43,14 +43,13 @@ class TargetsController < ApplicationController
end
def edit
@targets = @goal.targets.joins(:quantity).where(effective_from: params[:date])
@effective_from = params[:date].to_date
@targets = @goal.targets.joins(:quantity).where(effective_from: @effective_from)
.order('quantities.lft' => :asc).to_a
@effective_from = @targets.first&.effective_from
end
def update
# TODO: DRY same code with #create
@effective_from = params[:goal].delete(:effective_from)
@effective_from = params[:goal].delete(:effective_from).to_date
params[:goal][:targets_attributes].each { |ta| ta[:effective_from] = @effective_from }
if @goal.update(targets_params)
@ -67,7 +66,7 @@ class TargetsController < ApplicationController
end
def destroy
@effective_from = params[:date]
@effective_from = params[:date].to_date
@targets = @goal.targets.where(effective_from: @effective_from)
count = @targets.destroy_all.length
if @targets.all?(&:destroyed?)
@ -78,6 +77,14 @@ class TargetsController < ApplicationController
end
def reapply
@effective_from = ''
targets = @goal.targets.where(effective_from: params[:date].to_date)
@targets = targets.map do |t|
duplicate = t.dup
duplicate.thresholds = t.thresholds.map(&:dup)
duplicate
end
render :new
end
def toggle_exposure

View File

@ -68,7 +68,7 @@ end
resources :goals, only: [] do
resources :targets, param: :date, except: [:update] do
member do
post 'reapply'
get 'reapply'
end
end
resource :targets, only: [:update]

View File

@ -267,6 +267,7 @@ class TargetsTest < BodyTrackingSystemTestCase
def test_edit_binding_target
goal = @project.goals.binding
date = goal.targets.distinct.pluck(:effective_from).sample
targets = goal.targets.where(effective_from: date)
visit goal_targets_path(goal)
assert_no_selector 'form#edit-target-form'
@ -277,40 +278,7 @@ class TargetsTest < BodyTrackingSystemTestCase
assert_selector :xpath, 'following-sibling::tr//form[@id="edit-target-form"]'
end
within 'form#edit-target-form' do
assert has_field?(t(:field_effective_from), with: date, count: 1)
targets = goal.targets.where(effective_from: date)
assert_selector 'p.target', count: targets.length
targets.each do |target|
within find('option:checked', exact_text: target.quantity.name)
.ancestor('p.target') do
field_count = 1 + 3*target.thresholds.length
field_count += (target.thresholds.last.quantity.leaf? ? 0 : 1)
assert_selector 'input, select', count: field_count
target.thresholds.each do |threshold|
within find('option:checked', exact_text: threshold.quantity.name)
.ancestor('select') do
assert has_selector?(:xpath,
'following-sibling::input[not(@type="hidden")][1]',
exact_text: threshold.value)
assert has_selector?(:xpath, 'following-sibling::select//option[@selected]',
exact_text: threshold.unit.shortname)
end
end
if targets.length == 1
assert has_no_link?(t('targets.form.button_delete_target'))
else
assert has_link?(t('targets.form.button_delete_target'))
end
end
end
assert has_link?(t('targets.form.button_new_target'), count: 1)
end
assert_form_content 'form#edit-target-form', date, targets
end
def test_update_binding_target
@ -453,8 +421,18 @@ class TargetsTest < BodyTrackingSystemTestCase
end
end
def test_reapply
# TODO
def test_reapply_binding_target
goal = @project.goals.binding
date = goal.targets.distinct.pluck(:effective_from).sample
targets = goal.targets.where(effective_from: date)
visit goal_targets_path(goal)
assert_no_selector 'form#new-target-form'
assert_no_difference 'Target.count', 'Threshold.count' do
find('td', text: date).ancestor('tr').click_link t(:button_reapply)
end
assert_form_content 'form#new-target-form', '', targets
end
def fill_thresholds(thresholds)
@ -469,4 +447,40 @@ class TargetsTest < BodyTrackingSystemTestCase
end
end
end
def assert_form_content(location, date, targets)
within location do
assert has_field?(t(:field_effective_from), with: date, count: 1)
assert_selector 'p.target', count: targets.length
targets.each do |target|
within find('option:checked', exact_text: target.quantity.name)
.ancestor('p.target') do
field_count = 1 + 3*target.thresholds.length
field_count += (target.thresholds.last.quantity.leaf? ? 0 : 1)
assert_selector 'input, select', count: field_count
target.thresholds.each do |threshold|
within find('option:checked', exact_text: threshold.quantity.name)
.ancestor('select') do
assert has_selector?(:xpath,
'following-sibling::input[not(@type="hidden")][1]',
exact_text: threshold.value)
assert has_selector?(:xpath, 'following-sibling::select//option[@selected]',
exact_text: threshold.unit.shortname)
end
end
if targets.length == 1
assert has_no_link?(t('targets.form.button_delete_target'))
else
assert has_link?(t('targets.form.button_delete_target'))
end
end
end
assert has_link?(t('targets.form.button_new_target'), count: 1)
end
end
end