From 6de1053d4b7cb2f108b1d2c7548c2e2f8b79d9e0 Mon Sep 17 00:00:00 2001 From: Mantas Masalskis Date: Tue, 12 Oct 2021 15:46:46 +0200 Subject: [PATCH] Fixes #3800 - Sort order group_by broken (alphabetical) --- .../_application_controller/table.coffee | 4 +- spec/system/overview_spec.rb | 88 +++++++++++-------- 2 files changed, 56 insertions(+), 36 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller/table.coffee b/app/assets/javascripts/app/controllers/_application_controller/table.coffee index 7304710a0..3056a6a32 100644 --- a/app/assets/javascripts/app/controllers/_application_controller/table.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller/table.coffee @@ -497,7 +497,9 @@ class App.ControllerTable extends App.Controller reference_key = groupBy + '_id' if reference_key of object - return reference_key + attribute = _.findWhere(object.constructor.configure_attributes, { name: reference_key }) + + return App[attribute.relation]?.find(object[reference_key])?.displayName() || reference_key groupBy diff --git a/spec/system/overview_spec.rb b/spec/system/overview_spec.rb index 763619ec2..b255844c1 100644 --- a/spec/system/overview_spec.rb +++ b/spec/system/overview_spec.rb @@ -66,13 +66,19 @@ RSpec.describe 'Overview', type: :system do end end - context 'sorting when group by is set' do - let(:user) { create(:customer) } - let(:ticket1) { create(:ticket, group: Group.find_by(name: 'Users'), priority_id: 1, customer: user) } - let(:ticket2) { create(:ticket, group: Group.find_by(name: 'Users'), priority_id: 2, customer: user) } - let(:ticket3) { create(:ticket, group: Group.find_by(name: 'Users'), priority_id: 3, customer: user) } + context 'sorting when group by is set', authenticated_as: :user do + let(:user) { create(:agent, groups: [group_c, group_a, group_b]) } + + let(:group_a) { create(:group, name: 'aaa') } + let(:group_b) { create(:group, name: 'bbb') } + let(:group_c) { create(:group, name: 'ccc') } + + let(:ticket1) { create(:ticket, group: group_a, priority_id: 1, customer: user) } + let(:ticket2) { create(:ticket, group: group_c, priority_id: 2, customer: user) } + let(:ticket3) { create(:ticket, group: group_b, priority_id: 3, customer: user) } + let(:overview) do - create(:overview, group_by: 'priority', group_direction: group_direction, condition: { + create(:overview, group_by: group_key, group_direction: group_direction, condition: { 'ticket.customer_id' => { operator: 'is', value: user.id @@ -86,48 +92,60 @@ RSpec.describe 'Overview', type: :system do visit "ticket/view/#{overview.link}" end - context 'when group direction is default' do - let(:group_direction) { nil } + context 'when grouping by priority' do + let(:group_key) { 'priority' } - it 'sorts groups 1 > 3' do - within :active_content do - expect(find('.table-overview table tbody tr:first-child td:nth-child(1)').text).to match('1 low') - expect(find('.table-overview table tbody tr:nth-child(5) td:nth-child(1)').text).to match('3 high') + context 'when group direction is default' do + let(:group_direction) { nil } + + it 'sorts groups 1 > 3' do + within :active_content do + expect(all('.table-overview table b').map(&:text)).to eq ['1 low', '2 normal', '3 high'] + end + end + + it 'does not show duplicates when any ticket attribute is updated using bulk update' do + find("tr[data-id='#{ticket3.id}']").check('bulk', allow_label_click: true) + select '2 normal', from: 'priority_id' + + click '.js-confirm' + find('.js-confirm-step textarea').fill_in with: 'test tickets ordering' + click '.js-submit' + + within :active_content do + expect(page).to have_css("tr[data-id='#{ticket3.id}']", count: 1) + end end end - it 'does not show duplicates when any ticket attribute is updated using bulk update' do - find("tr[data-id='#{ticket3.id}']").check('bulk', allow_label_click: true) - select '2 normal', from: 'priority_id' + context 'when group direction is ASC' do + let(:group_direction) { 'ASC' } - click '.js-confirm' - find('.js-confirm-step textarea').fill_in with: 'test tickets ordering' - click '.js-submit' + it 'sorts groups 1 > 3' do + within :active_content do + expect(all('.table-overview table b').map(&:text)).to eq ['1 low', '2 normal', '3 high'] + end + end + end - within :active_content do - expect(page).to have_css("tr[data-id='#{ticket3.id}']", count: 1) + context 'when group direction is DESC' do + let(:group_direction) { 'DESC' } + + it 'sorts groups 3 > 1' do + within :active_content do + expect(all('.table-overview table b').map(&:text)).to eq ['3 high', '2 normal', '1 low'] + end end end end - context 'when group direction is ASC' do + context 'when grouping by groups' do + let(:group_key) { 'group' } let(:group_direction) { 'ASC' } - it 'sorts groups 1 > 3' do + it 'sorts groups a > b > c' do within :active_content do - expect(find('.table-overview table tbody tr:first-child td:nth-child(1)').text).to match('1 low') - expect(find('.table-overview table tbody tr:nth-child(5) td:nth-child(1)').text).to match('3 high') - end - end - end - - context 'when group direction is DESC' do - let(:group_direction) { 'DESC' } - - it 'sorts groups 3 > 1' do - within :active_content do - expect(find('.table-overview table tbody tr:first-child td:nth-child(1)').text).to match('3 high') - expect(find('.table-overview table tbody tr:nth-child(5) td:nth-child(1)').text).to match('1 low') + expect(all('.table-overview table b').map(&:text)).to eq %w[aaa bbb ccc] end end end