Skip to content

Commit

Permalink
test(mon-pix): test attestation result component
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume authored Nov 18, 2024
1 parent 36d16b9 commit 0e9a1bd
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mon-pix/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import getChallenges from './routes/get-challenges';
import getCompetenceEvaluationsByAssessment from './routes/get-competence-evaluations-by-assessment';
import getFeatureToggles from './routes/get-feature-toggles';
import getProgression from './routes/get-progression';
import getQuestResults from './routes/get-quest-results';
import getScorecard from './routes/get-scorecard';
import getScorecardsTutorials from './routes/get-scorecards-tutorials';
import loadUserTutorialsRoutes from './routes/get-user-tutorials';
Expand Down Expand Up @@ -112,6 +113,8 @@ function routes() {

this.post('/feedbacks');

this.get('/campaign-participations/1/quest-results', getQuestResults);

this.patch(
'/certification-candidates/:certificationCandidateId/validate-certification-instructions',
updateCertificationCandidates,
Expand Down
3 changes: 3 additions & 0 deletions mon-pix/mirage/routes/get-quest-results.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function (schema) {
return schema.questResults.all();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { render } from '@1024pix/ember-testing-library';
import Service from '@ember/service';
import { click } from '@ember/test-helpers';
import { t } from 'ember-intl/test-support';
import AttestationResult from 'mon-pix/components/campaigns/assessment/results/evaluation-results-hero/attestation-result';
import { module, test } from 'qunit';
import sinon from 'sinon';

import setupIntlRenderingTest from '../../../../helpers/setup-intl-rendering';

module('Integration | Component | Campaign | Skill Review | attestation-result', function (hooks) {
setupIntlRenderingTest(hooks);

module('when the attestation is obtained', function () {
test('it should display the expected message', async function (assert) {
const result = [
{
reward: { key: 'SIXTH_GRADE' },
obtained: true,
},
];

const screen = await render(<template><AttestationResult @results={{result}} /></template>);
const obtainedTitle = t('components.campaigns.attestation-result.obtained');
assert.dom(screen.getByText(obtainedTitle)).exists();
});

test('it should display the attestation title', async function (assert) {
const result = [
{
reward: { key: 'SIXTH_GRADE' },
obtained: true,
},
];

const screen = await render(<template><AttestationResult @results={{result}} /></template>);
const rewardTitle = t(`components.campaigns.attestation-result.${result[0].reward.key}.title`);
assert.dom(screen.getByText(rewardTitle)).exists();
});

test('it should display the download button', async function (assert) {
const result = [
{
reward: { key: 'SIXTH_GRADE' },
obtained: true,
},
];

const screen = await render(<template><AttestationResult @results={{result}} /></template>);
const downloadButtonTitle = t('common.actions.download');
assert.dom(screen.getByRole('button', { name: downloadButtonTitle })).exists();
});

test('it should download the attestation on download button click', async function (assert) {
// given
const result = [
{
reward: { key: 'SIXTH_GRADE' },
obtained: true,
},
];
class sessionService extends Service {
isAuthenticated = true;
data = {
authenticated: {
access_token: 'access_token',
user_id: 1,
source: 'pix',
},
};
}
const fileSaverSaveStub = sinon.stub();
class FileSaverStub extends Service {
save = fileSaverSaveStub;
}

this.owner.register('service:session', sessionService);
this.owner.register('service:fileSaver', FileSaverStub);

// when
const screen = await render(<template><AttestationResult @results={{result}} /></template>);
await click(screen.getByRole('button', { name: t('common.actions.download') }));

// then
assert.ok(
fileSaverSaveStub.calledWithExactly({
url: '/api/users/1/attestations/SIXTH_GRADE',
fileName: 'sensibilisation-au-numerique',
token: 'access_token',
}),
);
});

test('it should call onError method if the attestation can not be downloaded', async function (assert) {
// given
const result = [
{
reward: { key: 'SIXTH_GRADE' },
obtained: true,
},
];
class sessionService extends Service {
isAuthenticated = true;
data = {
authenticated: {
access_token: 'access_token',
user_id: 1,
source: 'pix',
},
};
}
const onErrorStub = sinon.stub();
const fileSaverSaveStub = sinon.stub().throws();
class FileSaverStub extends Service {
save = fileSaverSaveStub;
}

this.owner.register('service:session', sessionService);
this.owner.register('service:fileSaver', FileSaverStub);

// when
const screen = await render(
<template><AttestationResult @results={{result}} @onError={{onErrorStub}} /></template>,
);
await click(screen.getByRole('button', { name: t('common.actions.download') }));

// then
assert.ok(
fileSaverSaveStub.calledWithExactly({
url: '/api/users/1/attestations/SIXTH_GRADE',
fileName: 'sensibilisation-au-numerique',
token: 'access_token',
}),
);
assert.ok(onErrorStub.calledOnce);
});
});

module('when the attestation is not obtained', function () {
test('it should display the expected message', async function (assert) {
const result = [
{
reward: { key: 'SIXTH_GRADE' },
obtained: false,
},
];

const screen = await render(<template><AttestationResult @results={{result}} /></template>);
const notObtainedTitle = t('components.campaigns.attestation-result.not-obtained');
assert.dom(screen.getByText(notObtainedTitle)).exists();
});

test('it should display the attestation title', async function (assert) {
const result = [
{
reward: { key: 'SIXTH_GRADE' },
obtained: false,
},
];

const screen = await render(<template><AttestationResult @results={{result}} /></template>);
const rewardTitle = t(`components.campaigns.attestation-result.SIXTH_GRADE.title`);
assert.dom(screen.getByText(rewardTitle)).exists();
});

test('it should not display the download button', async function (assert) {
const result = [
{
reward: { key: 'SIXTH_GRADE' },
obtained: false,
},
];

const screen = await render(<template><AttestationResult @results={{result}} /></template>);
const downloadButton = t('common.actions.download');
assert.dom(screen.queryByRole('button', { name: downloadButton })).doesNotExist();
});
});

module('when the attestation is still computing', function () {
test('it should display the expected message', async function (assert) {
const result = [{ obtained: null }];

const screen = await render(<template><AttestationResult @results={{result}} /></template>);
const computingTitle = t('components.campaigns.attestation-result.computing');
assert.dom(screen.getByText(computingTitle)).exists();
});
});
});
5 changes: 5 additions & 0 deletions mon-pix/tests/unit/routes/campaigns/assessment/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ module('Unit | Route | Campaign | Assessment | Results', function (hooks) {

let route;
const campaign = { id: 123456, code: 'NEW_CODE' };
const questResults = [{ obtained: true, reward: { key: 'reward-key' } }];
const campaignParticipation = { id: 1212, isShared: true, hasMany: sinon.stub() };
const user = { id: 567890 };
const storeStub = {
queryRecord: sinon.stub(),
query: sinon.stub(),
};
const currentUserStub = { user };

Expand Down Expand Up @@ -41,6 +43,9 @@ module('Unit | Route | Campaign | Assessment | Results', function (hooks) {
storeStub.queryRecord
.withArgs('campaign-participation-result', { campaignId: campaign.id, userId: user.id })
.resolves(campaign);
storeStub.query
.withArgs('quest-result', { campaignParticipationId: campaignParticipation.id })
.resolves(questResults);
});

test('should not redirect', async function (assert) {
Expand Down

0 comments on commit 0e9a1bd

Please sign in to comment.