2024-04-29 00:17:15 +02:00
# frozen_string_literal: true
2017-10-02 17:03:38 +02:00
describe ProcedurePresentation do
2023-06-08 18:58:47 +02:00
include ActiveSupport :: Testing :: TimeHelpers
2024-07-22 14:58:16 +02:00
let ( :procedure ) { create ( :procedure , :published , types_de_champ_public : , types_de_champ_private : [ { } ] ) }
2024-09-27 15:45:37 +02:00
let ( :procedure_id ) { procedure . id }
2024-07-22 14:58:16 +02:00
let ( :types_de_champ_public ) { [ { } ] }
2020-07-20 16:13:51 +02:00
let ( :instructeur ) { create ( :instructeur ) }
2024-09-27 15:45:37 +02:00
let ( :assign_to ) { create ( :assign_to , procedure : , instructeur : ) }
2022-11-16 12:46:33 +01:00
let ( :first_type_de_champ ) { assign_to . procedure . active_revision . types_de_champ_public . first }
2020-10-30 15:01:13 +01:00
let ( :first_type_de_champ_id ) { first_type_de_champ . stable_id . to_s }
2020-06-08 14:33:58 +02:00
let ( :procedure_presentation ) {
2020-07-20 16:13:51 +02:00
create ( :procedure_presentation ,
2024-09-27 15:45:37 +02:00
assign_to : ,
2017-09-28 11:04:18 +02:00
displayed_fields : [
2024-07-19 11:16:40 +02:00
{ label : " test1 " , table : " user " , column : " email " } ,
2024-09-13 11:00:08 +02:00
{ label : " test2 " , table : " type_de_champ " , column : first_type_de_champ_id }
2018-01-15 19:17:01 +01:00
] ,
2024-07-19 11:16:40 +02:00
sort : { table : " user " , column : " email " , " order " = > " asc " } ,
2020-07-20 16:59:27 +02:00
filters : filters )
2017-09-28 11:04:18 +02:00
}
2020-06-08 14:33:58 +02:00
let ( :procedure_presentation_id ) { procedure_presentation . id }
let ( :filters ) { { " a-suivre " = > [ ] , " suivis " = > [ { " label " = > " label1 " , " table " = > " self " , " column " = > " created_at " } ] } }
2017-10-02 17:03:38 +02:00
2024-09-27 15:45:37 +02:00
def to_filter ( ( label , filter ) ) = FilteredColumn . new ( column : procedure . find_column ( label : label ) , filter : filter )
2017-10-02 17:03:38 +02:00
describe " # displayed_fields " do
2024-09-13 11:00:08 +02:00
it { expect ( procedure_presentation . displayed_fields ) . to eq ( [ { " label " = > " test1 " , " table " = > " user " , " column " = > " email " } , { " label " = > " test2 " , " table " = > " type_de_champ " , " column " = > first_type_de_champ_id } ] ) }
2017-10-02 17:03:38 +02:00
end
2017-09-27 15:16:07 +02:00
describe " # sort " do
2018-12-24 17:37:57 +01:00
it { expect ( procedure_presentation . sort ) . to eq ( { " table " = > " user " , " column " = > " email " , " order " = > " asc " } ) }
2017-09-27 15:16:07 +02:00
end
2017-09-28 11:04:18 +02:00
describe " # filters " do
2018-10-02 17:04:23 +02:00
it { expect ( procedure_presentation . filters ) . to eq ( { " a-suivre " = > [ ] , " suivis " = > [ { " label " = > " label1 " , " table " = > " self " , " column " = > " created_at " } ] } ) }
end
describe 'validation' do
it { expect ( build ( :procedure_presentation ) ) . to be_valid }
2024-10-07 15:00:05 +02:00
context 'of displayed columns' do
it do
pp = build ( :procedure_presentation , displayed_columns : [ { table : " user " , column : " reset_password_token " , procedure_id : } ] )
expect { pp . displayed_columns } . to raise_error ( ActiveRecord :: RecordNotFound )
end
2018-10-02 17:04:23 +02:00
end
context 'of filters' do
2024-10-07 09:54:17 +02:00
it 'validates the filter_column objects' do
expect ( build ( :procedure_presentation , " suivis_filters " : [ { id : { column_id : " user/email " , procedure_id : } , " filter " : " not so long filter value " } ] ) ) . to be_valid
expect ( build ( :procedure_presentation , " suivis_filters " : [ { id : { column_id : " user/email " , procedure_id : } , " filter " : " exceedingly long filter value " * 400 } ] ) ) . to be_invalid
2024-09-10 17:40:18 +02:00
end
2018-10-02 17:04:23 +02:00
end
2017-09-28 11:04:18 +02:00
end
2018-10-03 14:46:12 +02:00
2024-10-07 22:04:44 +02:00
describe '#update_displayed_fields' do
2024-10-28 17:48:14 +01:00
let ( :en_construction_column ) { procedure . find_column ( label : 'Date de passage en construction' ) }
let ( :mise_a_jour_column ) { procedure . find_column ( label : 'Date du dernier évènement' ) }
2024-10-07 15:00:05 +02:00
2024-10-07 22:04:44 +02:00
let ( :procedure_presentation ) do
create ( :procedure_presentation , assign_to : ) . tap do | pp |
2024-09-25 17:43:43 +02:00
pp . update ( sorted_column : SortedColumn . new ( column : procedure . find_column ( label : 'Demandeur' ) , order : 'desc' ) )
2024-10-07 22:04:44 +02:00
end
end
subject do
2024-10-07 15:00:05 +02:00
procedure_presentation . update ( displayed_columns : [
en_construction_column . id , mise_a_jour_column . id
2024-10-07 22:04:44 +02:00
] )
end
it 'should update displayed_fields' do
2024-10-07 15:00:05 +02:00
expect ( procedure_presentation . displayed_columns ) . to eq ( procedure . default_displayed_columns )
2024-10-07 22:04:44 +02:00
subject
expect ( procedure_presentation . displayed_columns ) . to eq ( [
2024-10-07 15:00:05 +02:00
en_construction_column , mise_a_jour_column
2024-10-07 22:04:44 +02:00
] )
2024-10-07 22:02:43 +02:00
end
end
2017-10-02 17:03:38 +02:00
end