diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index b686b34a1..022a14b51 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -259,183 +259,148 @@ describe ProcedureRevision do expect(child.parent_id).to eq(parent.id) end end + end - describe '#compare' do - let(:procedure) { create(:procedure, :with_type_de_champ, :with_type_de_champ_private, :with_repetition) } - let(:type_de_champ_first) { draft.types_de_champ_public.first } - let(:type_de_champ_second) { draft.types_de_champ_public.second } + describe '#compare' do + let(:first_tdc) { draft.types_de_champ_public.first } + let(:new_draft) { procedure.create_new_revision } - it 'type_de_champ' do - expect(new_draft.types_de_champ_public.size).to eq(2) - new_type_de_champ = new_draft.add_type_de_champ({ + subject { procedure.active_revision.compare(new_draft) } + + context 'when a type de champ is added' do + let(:procedure) { create(:procedure) } + let(:new_tdc) do + new_draft.add_type_de_champ( type_champ: TypeDeChamp.type_champs.fetch(:text), libelle: "Un champ text" - }) - draft.reload - new_draft.reload - expect(new_draft.types_de_champ_public.size).to eq(3) - expect(new_draft.types_de_champ_public.last).to eq(new_type_de_champ) - expect(new_draft.revision_types_de_champ_public.last.position).to eq(2) - expect(new_draft.revision_types_de_champ_public.last.type_de_champ).to eq(new_type_de_champ) - expect(new_draft.revision_types_de_champ_public.last.type_de_champ.revision).to eq(new_draft) - expect(procedure.active_revision.different_from?(new_draft)).to be_truthy - expect(procedure.active_revision.compare(new_draft)).to eq([ + ) + end + + before { new_tdc } + + it do + is_expected.to eq([ { model: :type_de_champ, op: :add, label: "Un champ text", private: false, - stable_id: new_type_de_champ.stable_id + stable_id: new_tdc.stable_id } ]) + end + end - new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.first.stable_id).update(libelle: 'modifier le libelle') - expect(procedure.active_revision.compare(new_draft.reload)).to eq([ + context 'when a type de champ is changed' do + let(:procedure) { create(:procedure, :with_type_de_champ) } + + before do + updated_tdc = new_draft.find_or_clone_type_de_champ(first_tdc.stable_id) + + updated_tdc.update(libelle: 'modifier le libelle', description: 'une description', mandatory: !updated_tdc.mandatory) + end + + it do + is_expected.to eq([ { model: :type_de_champ, op: :update, attribute: :libelle, - label: type_de_champ_first.libelle, + label: first_tdc.libelle, private: false, - from: type_de_champ_first.libelle, + from: first_tdc.libelle, to: "modifier le libelle", - stable_id: type_de_champ_first.stable_id + stable_id: first_tdc.stable_id }, - { - model: :type_de_champ, - op: :add, - label: "Un champ text", - private: false, - stable_id: new_type_de_champ.stable_id - } - ]) - expect(new_draft.types_de_champ_public.first.revision).to eq(new_draft) - - new_draft.move_type_de_champ(new_draft.types_de_champ_public.second.stable_id, 2) - expect(procedure.active_revision.compare(new_draft.reload)).to eq([ { model: :type_de_champ, op: :update, - attribute: :libelle, - label: type_de_champ_first.libelle, + attribute: :description, + label: first_tdc.libelle, private: false, - from: type_de_champ_first.libelle, - to: "modifier le libelle", - stable_id: type_de_champ_first.stable_id + from: first_tdc.description, + to: "une description", + stable_id: first_tdc.stable_id }, { model: :type_de_champ, - op: :add, - label: "Un champ text", + op: :update, + attribute: :mandatory, + label: first_tdc.libelle, private: false, - stable_id: new_type_de_champ.stable_id + from: false, + to: true, + stable_id: first_tdc.stable_id + } + ]) + end + end + + context 'when a type de champ is moved' do + let(:procedure) { create(:procedure, :with_type_de_champ, types_de_champ_count: 3) } + let(:new_draft_second_tdc) { new_draft.types_de_champ_public.second } + let(:new_draft_third_tdc) { new_draft.types_de_champ_public.third } + + before do + new_draft_second_tdc + new_draft_third_tdc + new_draft.move_type_de_champ(new_draft_second_tdc.stable_id, 2) + end + + it do + is_expected.to eq([ + { + model: :type_de_champ, + op: :move, + label: new_draft_third_tdc.libelle, + private: false, + from: 2, + to: 1, + stable_id: new_draft_third_tdc.stable_id }, { model: :type_de_champ, op: :move, - label: type_de_champ_second.libelle, + label: new_draft_second_tdc.libelle, private: false, from: 1, to: 2, - stable_id: type_de_champ_second.stable_id + stable_id: new_draft_second_tdc.stable_id } ]) - expect(new_draft.types_de_champ_public.last.revision).to eq(draft) + end + end - new_draft.remove_type_de_champ(new_draft.types_de_champ_public.first.stable_id) - expect(procedure.active_revision.compare(new_draft.reload)).to eq([ + context 'when a type de champ is removed' do + let(:procedure) { create(:procedure, :with_type_de_champ) } + + before do + new_draft.remove_type_de_champ(first_tdc.stable_id) + end + + it do + is_expected.to eq([ { model: :type_de_champ, op: :remove, - label: type_de_champ_first.libelle, + label: first_tdc.libelle, private: false, - stable_id: type_de_champ_first.stable_id - }, - { - model: :type_de_champ, - op: :add, - label: "Un champ text", - private: false, - stable_id: new_type_de_champ.stable_id + stable_id: first_tdc.stable_id } ]) + end + end - new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.stable_id).update(description: 'une description') - new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.stable_id).update(mandatory: true) - expect(procedure.active_revision.compare(new_draft.reload)).to eq([ - { - model: :type_de_champ, - op: :remove, - label: type_de_champ_first.libelle, - private: false, - stable_id: type_de_champ_first.stable_id - }, - { - model: :type_de_champ, - op: :add, - label: "Un champ text", - private: false, - stable_id: new_type_de_champ.stable_id - }, - { - model: :type_de_champ, - op: :update, - attribute: :description, - label: type_de_champ_second.libelle, - private: false, - from: type_de_champ_second.description, - to: "une description", - stable_id: type_de_champ_second.stable_id - }, - { - model: :type_de_champ, - op: :update, - attribute: :mandatory, - label: type_de_champ_second.libelle, - private: false, - from: false, - to: true, - stable_id: type_de_champ_second.stable_id - } - ]) + context 'when a child type de champ is transformed into a drop_down_list' do + let(:procedure) { create(:procedure, :with_repetition) } - new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.types_de_champ.first.stable_id).update(type_champ: :drop_down_list) - new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.types_de_champ.first.stable_id).update(drop_down_options: ['one', 'two']) - expect(procedure.active_revision.compare(new_draft.reload)).to eq([ - { - model: :type_de_champ, - op: :remove, - label: type_de_champ_first.libelle, - private: false, - stable_id: type_de_champ_first.stable_id - }, - { - model: :type_de_champ, - op: :add, - label: "Un champ text", - private: false, - stable_id: new_type_de_champ.stable_id - }, - { - model: :type_de_champ, - op: :update, - attribute: :description, - label: type_de_champ_second.libelle, - private: false, - from: type_de_champ_second.description, - to: "une description", - stable_id: type_de_champ_second.stable_id - }, - { - model: :type_de_champ, - op: :update, - attribute: :mandatory, - label: type_de_champ_second.libelle, - private: false, - from: false, - to: true, - stable_id: type_de_champ_second.stable_id - }, + before do + child = new_draft.types_de_champ_public.last.types_de_champ.first + new_draft.find_or_clone_type_de_champ(child.stable_id).update(type_champ: :drop_down_list, drop_down_options: ['one', 'two']) + end + + it do + is_expected.to eq([ { model: :type_de_champ, op: :update, @@ -457,44 +422,19 @@ describe ProcedureRevision do stable_id: new_draft.types_de_champ_public.last.types_de_champ.first.stable_id } ]) + end + end - new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.types_de_champ.first.stable_id).update(type_champ: :carte) - new_draft.find_or_clone_type_de_champ(new_draft.types_de_champ_public.last.types_de_champ.first.stable_id).update(options: { cadastres: true, znieff: true }) - expect(procedure.active_revision.compare(new_draft.reload)).to eq([ - { - model: :type_de_champ, - op: :remove, - label: type_de_champ_first.libelle, - private: false, - stable_id: type_de_champ_first.stable_id - }, - { - model: :type_de_champ, - op: :add, - label: "Un champ text", - private: false, - stable_id: new_type_de_champ.stable_id - }, - { - model: :type_de_champ, - op: :update, - attribute: :description, - label: type_de_champ_second.libelle, - private: false, - from: type_de_champ_second.description, - to: "une description", - stable_id: type_de_champ_second.stable_id - }, - { - model: :type_de_champ, - op: :update, - attribute: :mandatory, - label: type_de_champ_second.libelle, - private: false, - from: false, - to: true, - stable_id: type_de_champ_second.stable_id - }, + context 'when a child type de champ is transformed into a map' do + let(:procedure) { create(:procedure, :with_repetition) } + + before do + child = new_draft.types_de_champ_public.last.types_de_champ.first + new_draft.find_or_clone_type_de_champ(child.stable_id).update(type_champ: :carte, options: { cadastres: true, znieff: true }) + end + + it do + is_expected.to eq([ { model: :type_de_champ, op: :update,