demarches-normaliennes/spec/graphql/annotation_spec.rb

132 lines
4 KiB
Ruby
Raw Normal View History

RSpec.describe Mutations::DossierModifierAnnotation, type: :graphql do
let(:admin) { create(:administrateur) }
let(:procedure) { create(:procedure, :published, :for_individual, :with_private_repetition, :with_type_de_champ_private, administrateurs: [admin]) }
let(:dossiers) { [] }
let(:instructeur) { create(:instructeur, followed_dossiers: dossiers) }
let(:query) { '' }
let(:context) { { administrateur_id: admin.id } }
let(:variables) { {} }
subject { API::V2::Schema.execute(query, variables: variables, context: context) }
let(:data) { subject['data'].deep_symbolize_keys }
let(:errors) { subject['errors'].deep_symbolize_keys }
before do
instructeur.assign_to_procedure(procedure)
end
describe 'dossierModifierAnnotationAjouterLigne' do
let(:dossier) { create(:dossier, :en_construction, :with_populated_annotations, procedure: procedure) }
let(:dossiers) { [dossier] }
let(:annotation) { dossier.champs_private.find(&:repetition?) }
let(:query) { DOSSIER_MODIFIER_ANNOTATION_AJOUTER_LIGNE_MUTATION }
let(:variables) do
{
input: {
dossierId: dossier.to_typed_id,
annotationId: annotation.to_typed_id,
instructeurId: instructeur.to_typed_id
}
}
end
context 'with invalid champ' do
let(:annotation) { dossier.champs_private.last }
it 'return error' do
expect(data).to eq(dossierModifierAnnotationAjouterLigne: {
annotation: nil,
errors: [{ message: "Lannotation \"#{annotation.to_typed_id}\" nexiste pas" }]
})
end
end
it 'add row' do
expect(annotation.champs.size).to eq(4)
expect(data).to eq(dossierModifierAnnotationAjouterLigne: {
annotation: {
id: annotation.to_typed_id
},
errors: nil
})
expect(annotation.reload.champs.size).to eq(6)
end
end
describe 'dossierModifierAnnotationText' do
let(:dossier) { create(:dossier, :en_construction, :with_populated_annotations, procedure: procedure) }
let(:dossiers) { [dossier] }
let(:annotation) { dossier.champs_private.last }
let(:query) { DOSSIER_MODIFIER_ANNOTATION_TEXT_MUTATION }
let(:variables) do
{
input: {
dossierId: dossier.to_typed_id,
annotationId: annotation.to_typed_id,
instructeurId: instructeur.to_typed_id,
value: 'Hello world'
}
}
end
it 'update champ' do
expect(data).to eq(dossierModifierAnnotationText: {
annotation: {
id: annotation.to_typed_id
},
errors: nil
})
expect(annotation.reload.value).to eq('Hello world')
end
context 'with invalid champ' do
let(:annotation) { dossier.champs_private.find(&:repetition?) }
it 'return error' do
expect(data).to eq(dossierModifierAnnotationText: {
annotation: nil,
errors: [{ message: "Lannotation \"#{annotation.to_typed_id}\" nexiste pas" }]
})
end
end
context 'with rows' do
let(:annotation) { dossier.champs_private.find(&:repetition?).rows.first.first }
let(:other_annotation) { dossier.champs_private.find(&:repetition?).rows.second.first }
it 'update champ' do
expect(data).to eq(dossierModifierAnnotationText: {
annotation: {
id: annotation.to_typed_id
},
errors: nil
})
expect(annotation.reload.value).to eq('Hello world')
expect(other_annotation.reload.value).not_to eq('Hello world')
end
end
end
DOSSIER_MODIFIER_ANNOTATION_AJOUTER_LIGNE_MUTATION = <<-GRAPHQL
mutation($input: DossierModifierAnnotationAjouterLigneInput!) {
dossierModifierAnnotationAjouterLigne(input: $input) {
annotation { id }
errors { message }
}
}
GRAPHQL
DOSSIER_MODIFIER_ANNOTATION_TEXT_MUTATION = <<-GRAPHQL
mutation($input: DossierModifierAnnotationTextInput!) {
dossierModifierAnnotationText(input: $input) {
annotation { id }
errors { message }
}
}
GRAPHQL
end