Skip to content

Commit

Permalink
bug(TypesDeChamp::ConditionValidator): should allow to use types_de_c…
Browse files Browse the repository at this point in the history
…hamp_public on condition for types_de_champ_private
  • Loading branch information
mfo committed Jun 10, 2024
1 parent 5ef29a2 commit d2ccea7
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions spec/models/procedure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,37 +352,35 @@
end

describe 'draft_types_de_champ validations' do
let(:repetition) { repetition = procedure.draft_revision.types_de_champ_public.find(&:repetition?) }
let(:text_field) { build(:type_de_champ_text) }
let(:invalid_repetition_error_message) { 'Le champ « Enfants » doit comporter au moins un champ répétable' }

let(:drop_down) { build(:type_de_champ_drop_down_list, :without_selectable_values, libelle: 'Civilité') }
let(:invalid_drop_down_error_message) { 'Le champ « Civilité » doit comporter au moins un choix sélectionnable' }

let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :repetition, children: [{ type: :text }, { type: :integer_number }] }]) }
let(:draft) { procedure.draft_revision }

before do
draft.revision_types_de_champ.create(type_de_champ: drop_down, position: 100)

repetition.update(libelle: 'Enfants')
draft.children_of(repetition).destroy_all
end
let(:procedure) { create(:procedure, types_de_champ_public:, types_de_champ_private:) }

context 'on a draft procedure' do
let(:types_de_champ_private) { [] }
let(:types_de_champ_public) { [{ type: :repetition, libelle: 'Enfants', children: [] }] }

it 'doesn’t validate the types de champs' do
procedure.validate
expect(procedure.errors[:draft_types_de_champ_public]).not_to be_present
end
end

context 'when validating for publication' do
let(:types_de_champ_public) do
[
{ type: :repetition, libelle: 'Enfants', children: [] },
{ type: :drop_down_list, libelle: 'Civilité', options: [] }
]
end
let(:types_de_champ_private) { [] }
let(:invalid_repetition_error_message) { 'Le champ « Enfants » doit comporter au moins un champ répétable' }
let(:invalid_drop_down_error_message) { 'Le champ « Civilité » doit comporter au moins un choix sélectionnable' }

it 'validates that no repetition type de champ is empty' do
procedure.validate(:publication)
expect(procedure.errors.full_messages_for(:draft_types_de_champ_public)).to include(invalid_repetition_error_message)

new_draft = procedure.draft_revision

repetition = procedure.draft_revision.types_de_champ_public.find(&:repetition?)
parent_coordinate = new_draft.revision_types_de_champ.find_by(type_de_champ: repetition)
new_draft.revision_types_de_champ.create(type_de_champ: create(:type_de_champ), position: 0, parent: parent_coordinate)

Expand All @@ -394,17 +392,21 @@
procedure.validate(:publication)
expect(procedure.errors.full_messages_for(:draft_types_de_champ_public)).to include(invalid_drop_down_error_message)

drop_down = procedure.draft_revision.types_de_champ_public.find(&:drop_down_list?)
drop_down.update!(drop_down_list_value: "--title--\r\nsome value")
procedure.reload.validate(:publication)
expect(procedure.errors.full_messages_for(:draft_types_de_champ_public)).not_to include(invalid_drop_down_error_message)
end
end

context 'when the champ is private' do
before do
repetition.update(private: true)
drop_down.update(private: true)
let(:types_de_champ_private) do
[
{ type: :repetition, libelle: 'Enfants', children: [] },
{ type: :drop_down_list, libelle: 'Civilité', options: [] }
]
end
let(:types_de_champ_public) { [] }

let(:invalid_repetition_error_message) { 'L’annotation privée « Enfants » doit comporter au moins un champ répétable' }
let(:invalid_drop_down_error_message) { 'L’annotation privée « Civilité » doit comporter au moins un choix sélectionnable' }
Expand All @@ -418,6 +420,23 @@
procedure.validate(:publication)
expect(procedure.errors.full_messages_for(:draft_types_de_champ_private)).to include(invalid_drop_down_error_message)
end

it 'validates that types de champ private condition works types de champ public and private' do
end
end

context 'when condition on champ private use public champ' do
include Logic
let(:types_de_champ_private) { [{ type: :text, condition: ds_eq(champ_value(1), constant(2)) }] }
let(:types_de_champ_public) { [{ type: :number, stable_id: 1 }] }

it 'validate without context' do
expect(procedure.validate).to be_truthy
end

it 'validate with types_de_champ_private_editor' do
expect(procedure.validate(:types_de_champ_private_editor)).to be_falsey
end
end
end

Expand Down

0 comments on commit d2ccea7

Please sign in to comment.