|
| 1 | +import { |
| 2 | + createServer, |
| 3 | + databaseBuilder, |
| 4 | + expect, |
| 5 | + generateValidRequestAuthorizationHeader, |
| 6 | + knex, |
| 7 | + mockLearningContent, |
| 8 | +} from '../../../test-helper.js'; |
| 9 | + |
| 10 | +describe('Certification | Evaluation | Acceptance | answer-route', function () { |
| 11 | + let server; |
| 12 | + |
| 13 | + beforeEach(async function () { |
| 14 | + server = await createServer(); |
| 15 | + }); |
| 16 | + |
| 17 | + describe('POST /api/answers', function () { |
| 18 | + context('when challenge is focused out and answer is correct', function () { |
| 19 | + context('when the candidate needs an accessibility adjustment', function () { |
| 20 | + it('should save the answer as correct', async function () { |
| 21 | + // given |
| 22 | + const { competenceId, challengeId } = _buildLearningContent(); |
| 23 | + const { assessmentId, userId } = await _setupTestData(databaseBuilder, { |
| 24 | + competenceId, |
| 25 | + doesCandidateNeedAccessibilityAdjustment: true, |
| 26 | + }); |
| 27 | + const options = _setupRequestOptions({ userId, challengeId, assessmentId }); |
| 28 | + |
| 29 | + // when |
| 30 | + await server.inject(options); |
| 31 | + |
| 32 | + // then |
| 33 | + const [answer] = await knex('answers'); |
| 34 | + expect(answer.result).to.equal('ok'); |
| 35 | + expect(answer.isFocusedOut).to.equal(true); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + context('when the candidate does not need an accessibility adjustment', function () { |
| 40 | + it('should save the answer as focused out', async function () { |
| 41 | + // given |
| 42 | + const { competenceId, challengeId } = _buildLearningContent(); |
| 43 | + const { assessmentId, userId } = await _setupTestData(databaseBuilder, { |
| 44 | + competenceId, |
| 45 | + doesCandidateNeedAccessibilityAdjustment: false, |
| 46 | + }); |
| 47 | + const options = _setupRequestOptions({ userId, challengeId, assessmentId }); |
| 48 | + |
| 49 | + // when |
| 50 | + await server.inject(options); |
| 51 | + |
| 52 | + // then |
| 53 | + const [answer] = await knex('answers'); |
| 54 | + expect(answer.result).to.equal('focusedOut'); |
| 55 | + expect(answer.isFocusedOut).to.equal(true); |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +async function _setupTestData(databaseBuilder, { competenceId, doesCandidateNeedAccessibilityAdjustment }) { |
| 63 | + const userId = databaseBuilder.factory.buildUser().id; |
| 64 | + |
| 65 | + const session = databaseBuilder.factory.buildSession({}); |
| 66 | + |
| 67 | + databaseBuilder.factory.buildCertificationCandidate({ |
| 68 | + sessionId: session.id, |
| 69 | + userId, |
| 70 | + accessibilityAdjustmentNeeded: doesCandidateNeedAccessibilityAdjustment, |
| 71 | + }); |
| 72 | + |
| 73 | + const certificationCourse = databaseBuilder.factory.buildCertificationCourse({ |
| 74 | + userId, |
| 75 | + sessionId: session.id, |
| 76 | + }); |
| 77 | + |
| 78 | + const assessment = databaseBuilder.factory.buildAssessment({ |
| 79 | + type: 'CERTIFICATION', |
| 80 | + userId, |
| 81 | + competenceId, |
| 82 | + certificationCourseId: certificationCourse.id, |
| 83 | + }); |
| 84 | + |
| 85 | + await databaseBuilder.commit(); |
| 86 | + |
| 87 | + return { assessmentId: assessment.id, userId }; |
| 88 | +} |
| 89 | + |
| 90 | +function _setupRequestOptions({ userId, challengeId, assessmentId }) { |
| 91 | + return { |
| 92 | + method: 'POST', |
| 93 | + url: '/api/answers', |
| 94 | + headers: { authorization: generateValidRequestAuthorizationHeader(userId) }, |
| 95 | + payload: { |
| 96 | + data: { |
| 97 | + type: 'answers', |
| 98 | + attributes: { |
| 99 | + value: 'correct', |
| 100 | + 'focused-out': true, |
| 101 | + }, |
| 102 | + relationships: { |
| 103 | + assessment: { |
| 104 | + data: { |
| 105 | + type: 'assessments', |
| 106 | + id: assessmentId, |
| 107 | + }, |
| 108 | + }, |
| 109 | + challenge: { |
| 110 | + data: { |
| 111 | + type: 'challenges', |
| 112 | + id: challengeId, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }; |
| 119 | +} |
| 120 | + |
| 121 | +function _buildLearningContent() { |
| 122 | + const challengeId = 'a_challenge_id'; |
| 123 | + const competenceId = 'recCompetence'; |
| 124 | + |
| 125 | + const learningContent = { |
| 126 | + areas: [{ id: 'recArea1', competenceIds: ['recCompetence'] }], |
| 127 | + competences: [ |
| 128 | + { |
| 129 | + id: 'recCompetence', |
| 130 | + areaId: 'recArea1', |
| 131 | + skillIds: ['recSkill1'], |
| 132 | + origin: 'Pix', |
| 133 | + name_i18n: { |
| 134 | + fr: 'Nom de la competence FR', |
| 135 | + en: 'Nom de la competence EN', |
| 136 | + }, |
| 137 | + statue: 'active', |
| 138 | + }, |
| 139 | + ], |
| 140 | + skills: [ |
| 141 | + { |
| 142 | + id: 'recSkill1', |
| 143 | + name: '@recArea1_Competence1_Tube1_Skill1', |
| 144 | + status: 'actif', |
| 145 | + competenceId: competenceId, |
| 146 | + pixValue: '5', |
| 147 | + }, |
| 148 | + ], |
| 149 | + challenges: [ |
| 150 | + { |
| 151 | + id: challengeId, |
| 152 | + competenceId: competenceId, |
| 153 | + skillId: 'recSkill1', |
| 154 | + status: 'validé', |
| 155 | + solution: 'correct', |
| 156 | + proposals: '${a}', |
| 157 | + locales: ['fr-fr'], |
| 158 | + type: 'QROC', |
| 159 | + focusable: true, |
| 160 | + }, |
| 161 | + ], |
| 162 | + }; |
| 163 | + mockLearningContent(learningContent); |
| 164 | + |
| 165 | + return { |
| 166 | + competenceId, |
| 167 | + challengeId, |
| 168 | + }; |
| 169 | +} |
0 commit comments