demarches-normaliennes/spec/models/procedure_revision_preloader_spec.rb

48 lines
2.2 KiB
Ruby
Raw Normal View History

2024-01-26 13:43:10 +01:00
describe ProcedureRevisionPreloader do
let(:procedure) do
create(:procedure, :published,
types_de_champ_public: [
{ type: :integer_number },
{ type: :decimal_number }
],
types_de_champ_private: [
{ type: :text },
{ type: :textarea }
])
end
describe '.load_one' do
let(:revision) { procedure.draft_revision }
subject { ProcedureRevisionPreloader.load_one(revision) }
it 'assigns stuffs correctly' do
# check it changes loaded from false to true
expect { subject }.to change { procedure.draft_revision.association(:revision_types_de_champ).loaded? }.from(false).to(true)
# check loaded relationships
2024-01-26 13:43:10 +01:00
expect(procedure.draft_revision.association(:revision_types_de_champ_public).loaded?).to be_truthy
expect(procedure.draft_revision.association(:revision_types_de_champ_private).loaded?).to be_truthy
expect(procedure.draft_revision.association(:types_de_champ).loaded?).to be_truthy
expect(procedure.draft_revision.association(:types_de_champ_public).loaded?).to be_truthy
expect(procedure.draft_revision.association(:types_de_champ_private).loaded?).to be_truthy
# check nested relationship
2024-01-26 13:43:10 +01:00
expect(revision.revision_types_de_champ.first.association(:revision).loaded?).to eq(true)
expect(revision.revision_types_de_champ.first.association(:procedure).loaded?).to eq(true)
# check order
original = Procedure.find(procedure.id)
expect_relation_is_preloaded_sorted(original, procedure, :revision_types_de_champ)
expect_relation_is_preloaded_sorted(original, procedure, :revision_types_de_champ_public)
expect_relation_is_preloaded_sorted(original, procedure, :revision_types_de_champ_private)
expect_relation_is_preloaded_sorted(original, procedure, :types_de_champ)
expect_relation_is_preloaded_sorted(original, procedure, :types_de_champ_public)
expect_relation_is_preloaded_sorted(original, procedure, :types_de_champ_private)
end
def expect_relation_is_preloaded_sorted(original, preloaded, association)
expect(original.draft_revision.send(association).map(&:id)).to eq(preloaded.draft_revision.send(association).map(&:id))
2024-01-26 13:43:10 +01:00
end
end
end