Skip to content

Commit

Permalink
Délègues aux sous-componsants de Défi de constuire la réponse
Browse files Browse the repository at this point in the history
  • Loading branch information
etienneCharignon authored and cprodhomme committed Jan 18, 2022
1 parent e2ee7ad commit 281387d
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 72 deletions.
21 changes: 2 additions & 19 deletions src/situations/commun/vues/defi.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default {
data () {
return {
envoyer: false,
reponse: ''
reponse: this.question.type === 'action' ? { succes: true } : ''
};
},
Expand All @@ -99,30 +99,13 @@ export default {
disabled () {
return (!this.reponse || this.envoyer) && this.reponsesPossibles;
},
donneesReponse () {
if (this.contenuDeTypeChamp) {
const succes = this.reponse.toLowerCase() === this.question.bonneReponse;
return { reponse: this.reponse, succes: succes };
} else if (this.question.type === 'redaction_note') {
return { reponse: this.reponse };
} else if (this.question.type === 'action') {
return { succes: true };
} else {
if (this.question.choix.length === 0) {
return { succes: true };
}
const choix = this.question.choix.find((choix) => choix.id === this.reponse);
return { reponse: choix.id, succes: choix.bonneReponse };
}
}
},
methods: {
envoi () {
this.envoyer = true;
this.$emit('reponse', this.donneesReponse);
this.$emit('reponse', this.reponse);
}
}
};
Expand Down
12 changes: 8 additions & 4 deletions src/situations/commun/vues/defi/champ_saisie.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<span></span>
</div>
<input
v-on:input="$emit('input', $event.target.value.trim())"
v-on:input="emetReponse($event.target.value)"
class="champ"
:class="{ 'champ-texte champ-texte--decale' : estTexte,
'champ-numerique' : estNumerique }"
Expand All @@ -34,9 +34,6 @@ export default {
question: {
type: Object,
required: true
},
value: {
type: String
}
},
Expand All @@ -50,6 +47,13 @@ export default {
maxLength () {
return this.estNumerique ? 4 : undefined;
}
},
methods: {
emetReponse (valeur) {
const reponse = valeur.trim();
this.$emit('input', { reponse: reponse, succes: reponse.toLowerCase() === this.question.bonneReponse });
}
}
};
</script>
2 changes: 1 addition & 1 deletion src/situations/commun/vues/defi/jauge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default ({
emetSelection () {
const reponseChoisie = this.question.choix[this.choixFait];
this.$emit('input', reponseChoisie.id);
this.$emit('input', { reponse: reponseChoisie.id });
},
afficheLectureReponse (nomTechnique) {
Expand Down
3 changes: 2 additions & 1 deletion src/situations/commun/vues/defi/qcm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export default {
},
selectReponse (valeur) {
this.reponse = valeur;
this.$emit('input', valeur);
const choix = this.question.choix.find((choix) => choix.id === this.reponse);
this.$emit('input', { reponse: choix.id, succes: choix.bonneReponse });
}
}
};
Expand Down
8 changes: 7 additions & 1 deletion src/situations/commun/vues/defi/redaction_note.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div class="question-contenu">
<textarea
v-on:input="$emit('input', $event.target.value.trim())"
v-on:input="emetReponse($event.target.value)"
:placeholder="question.reponse_placeholder"
class="reponse-redaction"></textarea>
</div>
Expand All @@ -16,6 +16,12 @@ export default {
type: Object,
required: true
}
},
methods: {
emetReponse (valeur) {
this.$emit('input', { reponse: valeur.trim() });
}
}
};
</script>
33 changes: 5 additions & 28 deletions tests/situations/commun/vues/defi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,14 @@ describe("La vue d'un défi", function () {
expect(vue.findComponent(Qcm).exists()).toBe(true);
});

it('emet un événement réponse succès quand on appuie sur le bouton envoi', function (done) {
vue.findComponent(Qcm).vm.$emit('input', 'uid-32');
vue.vm.$nextTick(() => {
vue.find('.question-bouton').trigger('click');
vue.vm.$nextTick(() => {
expect(vue.emitted().reponse.length).toEqual(1);
expect(vue.emitted().reponse[0][0]).toEqual({ reponse: 'uid-32', succes: true });
done();
});
});
});

it('emet un événement réponse échec quand on appuie sur le bouton envoi', function (done) {
vue.findComponent(Qcm).vm.$emit('input', 'uid-32-2');
it('emet un événement réponse quand on appuie sur le bouton envoi', function (done) {
const reponse = { reponse: 'uid-32', succes: true };
vue.findComponent(Qcm).vm.$emit('input', reponse);
vue.vm.$nextTick(() => {
vue.find('.question-bouton').trigger('click');
vue.vm.$nextTick(() => {
expect(vue.emitted().reponse.length).toEqual(1);
expect(vue.emitted().reponse[0][0]).toEqual({ reponse: 'uid-32-2', succes: false });
expect(vue.emitted().reponse[0][0]).toEqual(reponse);
done();
});
});
Expand All @@ -71,7 +60,7 @@ describe("La vue d'un défi", function () {
vue.find('.question-bouton').trigger('click');
vue.vm.$nextTick(() => {
expect(vue.emitted().reponse.length).toEqual(1);
expect(vue.emitted().reponse[0][0]).toEqual({ succes: true });
expect(vue.emitted().reponse[0][0]).toEqual('');
done();
});
});
Expand All @@ -96,18 +85,6 @@ describe("La vue d'un défi", function () {
expect(vue.findComponent(ChampSaisie).vm.question.espacerChiffres).toBe(true);
});

it('émet un évement réponse', function (done) {
vue.findComponent(ChampSaisie).vm.$emit('input', '1800');
vue.vm.$nextTick(() => {
vue.find('.question-bouton').trigger('click');
vue.vm.$nextTick(() => {
expect(vue.emitted('reponse').length).toEqual(1);
expect(vue.emitted('reponse')[0][0]).toEqual({ reponse: '1800', succes: true });
done();
});
});
});

describe('#disabled', function () {
it("désactive le bouton quand aucune réponse numérique n'est donnée", function () {
expect(vue.find('.question-bouton').attributes('disabled')).toEqual('disabled');
Expand Down
11 changes: 3 additions & 8 deletions tests/situations/commun/vues/defi/champ_saisie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@ describe('Le composant champ de saisie', function () {
});

describe('peut être utilisé avec la propriété v-model', function () {
it('afficher la valeur initial si elle est passé', function () {
vue = composant({ question: {}, value: 'valeurInitiale' });
const input = vue.find('input[type=text]');
expect(input.element.value).toEqual('valeurInitiale');
});

it('envoie la réponse dans un événement input', function () {
vue = composant({ question: { bonneReponse: 'boulangerie' } });
const input = vue.find('input[type=text]');
input.setValue('1800 ');
input.setValue('Boulangerie ');
expect(vue.emitted('input').length).toEqual(1);
expect(vue.emitted('input')[0][0]).toEqual('1800');
expect(vue.emitted('input')[0][0]).toEqual({ reponse: 'Boulangerie', succes: true });
});
});

Expand Down
4 changes: 2 additions & 2 deletions tests/situations/commun/vues/defi/jauge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('La vue jauge', function () {
wrapper.find('.label-libelle').trigger('click');
wrapper.vm.$nextTick(() => {
expect(wrapper.emitted().input[0])
.toEqual(['3c178015-a7c1-4ff8-a344-8553a61e754a']);
.toEqual([{ reponse: '3c178015-a7c1-4ff8-a344-8553a61e754a' }]);
done();
});
});
Expand All @@ -85,7 +85,7 @@ describe('La vue jauge', function () {
wrapper.find('.jauge input').setValue(2);
wrapper.vm.$nextTick(() => {
expect(wrapper.emitted().input[0])
.toEqual(['3c178015-a7c1-4ff8-a344-8553a61e754a']);
.toEqual([{ reponse: '3c178015-a7c1-4ff8-a344-8553a61e754a' }]);
done();
});
});
Expand Down
22 changes: 16 additions & 6 deletions tests/situations/commun/vues/defi/qcm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,21 @@ describe('Le componsant defi QCM', function () {
expect(vue.findAll('img').length).toBe(3);
});

it('envoie la réponse dans un événement input', function () {
question.choix = [{ id: 'uid-32', bonneReponse: true }];
const vue = composant(question);
vue.find('input[type=radio][value=uid-32]').setChecked();
expect(vue.emitted('input').length).toEqual(1);
expect(vue.emitted('input')[0][0]).toEqual('uid-32');
describe('envoie la réponse dans un événement input', function () {
it('quand il y a une bonne réponse', function () {
question.choix = [{ id: 'uid-32', bonneReponse: true }];
const vue = composant(question);
vue.find('input[type=radio][value=uid-32]').setChecked();
expect(vue.emitted('input').length).toEqual(1);
expect(vue.emitted('input')[0][0]).toEqual({ reponse: 'uid-32', succes: true });
});

it("quand il n'y a pas de bonne réponse", function () {
question.choix = [{ id: 'uid-32' }];
const vue = composant(question);
vue.find('input[type=radio][value=uid-32]').setChecked();
expect(vue.emitted('input').length).toEqual(1);
expect(vue.emitted('input')[0][0]).toEqual({ reponse: 'uid-32' });
});
});
});
4 changes: 2 additions & 2 deletions tests/situations/commun/vues/defi/redaction_note.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ describe('Le composant RedactionNote', function () {
it('envoie la réponse dans un événement input', function () {
const vue = composant({ question: {} });
const input = vue.find('textarea');
const reponse = 'Une rédaction de plusieurs\nlignes';
const reponse = 'Une rédaction de plusieurs\nlignes\n\n';
input.setValue(reponse);
expect(vue.emitted('input').length).toEqual(1);
expect(vue.emitted('input')[0][0]).toEqual(reponse);
expect(vue.emitted('input')[0][0]).toEqual({ reponse: 'Une rédaction de plusieurs\nlignes' });
});
});
});

0 comments on commit 281387d

Please sign in to comment.