From 776d4ab9173bef6e89f5174c2ace6b1e9ae91264 Mon Sep 17 00:00:00 2001 From: yui-har <7232538+yui-har@users.noreply.github.com> Date: Thu, 26 Jan 2023 10:37:56 +0900 Subject: [PATCH] Improved getting the Role IDs(master) (#53) * Fixed a factory of NoteTemplate * Add test * Fixed getting role ids --- app/controllers/note_templates_controller.rb | 2 +- spec/factories/note_templates.rb | 3 +- spec/features/drag_and_drop_spec.rb | 100 ++++++++++++++----- 3 files changed, 76 insertions(+), 29 deletions(-) diff --git a/app/controllers/note_templates_controller.rb b/app/controllers/note_templates_controller.rb index d710a4a8..7f47dc7f 100644 --- a/app/controllers/note_templates_controller.rb +++ b/app/controllers/note_templates_controller.rb @@ -52,7 +52,7 @@ def update if template_params.key?(:role_ids) template_params[:role_ids] else - @note_template.note_visible_role_ids + @note_template.note_visible_roles.pluck(:role_id) end save_and_flash(:notice_successful_update, :show) diff --git a/spec/factories/note_templates.rb b/spec/factories/note_templates.rb index 5d46a0c6..7fc5a0f5 100644 --- a/spec/factories/note_templates.rb +++ b/spec/factories/note_templates.rb @@ -8,7 +8,6 @@ sequence(:memo) { |n| "note-template-memo: #{n}" } enabled { true } sequence(:position) { |n| n } - visibility { 1 } - role_ids { [1] } + visibility { NoteTemplate::visibilities[:open] } end end diff --git a/spec/features/drag_and_drop_spec.rb b/spec/features/drag_and_drop_spec.rb index ec7293b9..02353c91 100644 --- a/spec/features/drag_and_drop_spec.rb +++ b/spec/features/drag_and_drop_spec.rb @@ -49,32 +49,80 @@ }.from([4, 1, 2, 3]).to([3, 1, 4, 2]) end - scenario 'Can drag and drop on Note Templates' do - FactoryBot.create_list(:note_template, 4, project_id: project.id, tracker_id: tracker.id) - - visit_note_template_list(user) - - first_target = table.find('tr:nth-child(1) > td.buttons > span') - last_target = table.find('tr:nth-child(4) > td.buttons > span') - - # change id: 1, 2, 3, 4 to 4, 1, 2, 3 - expect do - first_target.drag_to(last_target) - wait_for_ajax - end.to change { - NoteTemplate.reorder(:id).pluck(:position).to_a - }.from([1, 2, 3, 4]).to([4, 1, 2, 3]) - - # change id: 4, 1, 2, 3 to 3, 1, 4, 2 - second_target = table.find('tr:nth-child(2) > td.buttons > span') - last_target = table.find('tr:nth-child(4) > td.buttons > span') - - expect do - second_target.drag_to(last_target) - wait_for_ajax - end.to change { - NoteTemplate.reorder(:id).pluck(:position).to_a - }.from([4, 1, 2, 3]).to([3, 1, 4, 2]) + feature 'Note Templates' do + background do + FactoryBot.rewind_sequences + end + + scenario 'Can drag and drop on Note Templates' do + FactoryBot.create_list(:note_template, 4, project_id: project.id, tracker_id: tracker.id) + + visit_note_template_list(user) + + first_target = table.find('tr:nth-child(1) > td.buttons > span') + last_target = table.find('tr:nth-child(4) > td.buttons > span') + + # id: 1, 2, 3, 4 1, 2, 3, 4 + #-------------------------------------------- + # change position: 1, 2, 3, 4 to 4, 1, 2, 3 + expect do + first_target.drag_to(last_target) + wait_for_ajax + end.to change { + NoteTemplate.reorder(:id).pluck(:position).to_a + }.from([1, 2, 3, 4]).to([4, 1, 2, 3]) + + # id: 1, 2, 3, 4 1, 2, 3, 4 + #-------------------------------------------- + # change position: 4, 1, 2, 3 to 3, 1, 4, 2 + second_target = table.find('tr:nth-child(2) > td.buttons > span') + last_target = table.find('tr:nth-child(4) > td.buttons > span') + + expect do + second_target.drag_to(last_target) + wait_for_ajax + end.to change { + NoteTemplate.reorder(:id).pluck(:position).to_a + }.from([4, 1, 2, 3]).to([3, 1, 4, 2]) + end + + scenario 'After changing the sort position, Role settings should not change' do + developer_role = FactoryBot.create(:role) + template_list = + FactoryBot.create_list(:note_template, 2, + project_id: project.id, tracker_id: tracker.id, + visibility: NoteTemplate::visibilities[:roles], + role_ids: [role.id, developer_role.id], + ) + template_list.each(&:reload) + + template1 = template_list.first + expect(template1.position).to eq(1) + expect(template1.roles?).to be_truthy + expect(template1.note_visible_roles.pluck(:role_id)).to eq([role.id, developer_role.id]) + + visit_note_template_list(user) + + expect do + tr_idx = [1, 2] + 5.times do + first_target = table.find("tr:nth-child(#{tr_idx.first}) > td.buttons > span") + last_target = table.find("tr:nth-child(#{tr_idx.last}) > td.buttons > span") + + first_target.drag_to(last_target) + wait_for_ajax + tr_idx.reverse! + end + end.to \ + change { + NoteTemplate.reorder(:id).pluck(:position).to_a + }.from([1, 2]).to([2, 1]) + + template1.reload + expect(template1.position).to eq(2) + expect(template1.roles?).to be_truthy + expect(template1.note_visible_roles.pluck(:role_id)).to eq([role.id, developer_role.id]) + end end scenario 'Can drag and drop on Global Issue Templates' do