From 155791b00d2213b0338efa677fe35ed00b9bfe25 Mon Sep 17 00:00:00 2001 From: Geoffroy Begouaussel Date: Tue, 8 Oct 2024 16:52:15 +0200 Subject: [PATCH] refactor(mon-pix): add hasCustomResultPageButton getter --- .../custom-organization-block.gjs | 58 +++---- .../evaluation-results-hero/index.gjs | 13 +- mon-pix/app/models/campaign.js | 4 + .../hero/custom-organization-block-test.js | 163 ++++++++++++------ .../hero/evaluation-results-hero-test.js | 86 +++++++-- mon-pix/tests/unit/models/campaign-test.js | 53 ++++++ 6 files changed, 268 insertions(+), 109 deletions(-) create mode 100644 mon-pix/tests/unit/models/campaign-test.js diff --git a/mon-pix/app/components/campaigns/assessment/skill-review/evaluation-results-hero/custom-organization-block.gjs b/mon-pix/app/components/campaigns/assessment/skill-review/evaluation-results-hero/custom-organization-block.gjs index 13471e04a0c..fb4c8c8d10a 100644 --- a/mon-pix/app/components/campaigns/assessment/skill-review/evaluation-results-hero/custom-organization-block.gjs +++ b/mon-pix/app/components/campaigns/assessment/skill-review/evaluation-results-hero/custom-organization-block.gjs @@ -1,41 +1,29 @@ import PixButtonLink from '@1024pix/pix-ui/components/pix-button-link'; -import Component from '@glimmer/component'; import { t } from 'ember-intl'; +import { and } from 'ember-truth-helpers'; import MarkdownToHtml from '../../../../markdown-to-html'; -export default class EvaluationResultsHeroCustomOrganizationBlock extends Component { - get hasCustomOrganizationLink() { - return this.args.customResultPageButtonUrl && this.args.customResultPageButtonText; - } - - get showOrganizationContent() { - return this.args.customResultPageText || this.hasCustomOrganizationLink; - } - - diff --git a/mon-pix/app/components/campaigns/assessment/skill-review/evaluation-results-hero/index.gjs b/mon-pix/app/components/campaigns/assessment/skill-review/evaluation-results-hero/index.gjs index 050c8af7fe8..de3899614fd 100644 --- a/mon-pix/app/components/campaigns/assessment/skill-review/evaluation-results-hero/index.gjs +++ b/mon-pix/app/components/campaigns/assessment/skill-review/evaluation-results-hero/index.gjs @@ -5,6 +5,7 @@ import PixStars from '@1024pix/pix-ui/components/pix-stars'; import { service } from '@ember/service'; import Component from '@glimmer/component'; import { t } from 'ember-intl'; +import { or } from 'ember-truth-helpers'; import ENV from 'mon-pix/config/environment'; import MarkdownToHtml from '../../../../markdown-to-html'; @@ -129,11 +130,13 @@ export default class EvaluationResultsHero extends Component { {{/if}} - + {{#if (or @campaign.customResultPageText @campaign.hasCustomResultPageButton)}} + + {{/if}} {{#if @campaignParticipationResult.canRetry}} {{/if}} diff --git a/mon-pix/app/models/campaign.js b/mon-pix/app/models/campaign.js index bb50758d7ec..9938a813f3c 100644 --- a/mon-pix/app/models/campaign.js +++ b/mon-pix/app/models/campaign.js @@ -46,6 +46,10 @@ export default class Campaign extends Model { return this.organizationType === 'SUP'; } + get hasCustomResultPageButton() { + return Boolean(this.customResultPageButtonUrl) && Boolean(this.customResultPageButtonText); + } + isRestrictedByIdentityProvider(identityProviderCode) { return this.identityProvider === identityProviderCode; } diff --git a/mon-pix/tests/integration/components/campaigns/assessment/skill-review/hero/custom-organization-block-test.js b/mon-pix/tests/integration/components/campaigns/assessment/skill-review/hero/custom-organization-block-test.js index 63823c7c519..ee66a8a7960 100644 --- a/mon-pix/tests/integration/components/campaigns/assessment/skill-review/hero/custom-organization-block-test.js +++ b/mon-pix/tests/integration/components/campaigns/assessment/skill-review/hero/custom-organization-block-test.js @@ -10,62 +10,123 @@ module( function (hooks) { setupIntlRenderingTest(hooks); - module('when organization custom text is defined', function () { - test('it should display organization custom text', async function (assert) { - // given - this.set('customResultPageText', 'My custom result page text'); - - // when - const screen = await render( - hbs``, - ); - - // then - assert.dom(screen.getByText(t('pages.skill-review.organization-message'))).exists(); - assert.dom(screen.getByText('My custom result page text')).exists(); - }); + test('displays the block title', async function (assert) { + // when + const screen = await render( + hbs` + `, + ); + + // then + assert.dom(screen.getByText(t('pages.skill-review.organization-message'))).exists(); }); - module('when organization custom link url and label is defined', function () { - test('it should display organization custom link', async function (assert) { - // given - this.set('customResultPageButtonUrl', 'https://pix.org/'); - this.set('customResultPageButtonText', 'My custom button'); - - // when - const screen = await render( - hbs``, - ); - - // then - assert.dom(screen.getByText(t('pages.skill-review.organization-message'))).exists(); - assert.strictEqual(screen.getByRole('link', { name: 'My custom button' }).href, 'https://pix.org/'); + module('custom text', function () { + module('when organization custom text is defined', function () { + test('displays organization custom text', async function (assert) { + // given + const customResultPageText = 'My custom result page text'; + this.set('customResultPageText', customResultPageText); + + // when + const screen = await render( + hbs` + `, + ); + + // then + assert.dom(screen.getByText(customResultPageText)).exists(); + }); + }); + + module('when organization custom text is not defined', function () { + test('not display organization custom text', async function (assert) { + // given + this.set('customResultPageText', null); + + // when + const screen = await render( + hbs` + `, + ); + + // then + assert.dom(screen.queryByRole('paragraph')).doesNotExist(); + }); }); }); - module('when no custom organization content is defined', function () { - test('should not display the block', async function (assert) { - // given - this.set('customResultPageText', null); - this.set('customResultPageButtonUrl', null); - this.set('customResultPageButtonText', null); - - // when - const screen = await render( - hbs``, - ); - - // then - assert.dom(screen.queryByText(t('pages.skill-review.organization-message'))).doesNotExist(); + module('custom button', function () { + module('when organization custom link url and label are defined', function () { + test('displays organization custom link', async function (assert) { + // given + const customResultPageButtonUrl = 'https://pix.org/'; + this.set('customResultPageButtonUrl', customResultPageButtonUrl); + + const customResultPageButtonText = 'My custom button'; + this.set('customResultPageButtonText', customResultPageButtonText); + + // when + const screen = await render( + hbs` + `, + ); + + // then + assert.strictEqual( + screen.getByRole('link', { name: customResultPageButtonText }).href, + customResultPageButtonUrl, + ); + }); + }); + + module('when organization custom link url is defined but label is not', function () { + test('not display organization custom link', async function (assert) { + // given + const customResultPageButtonUrl = 'https://pix.org/'; + this.set('customResultPageButtonUrl', customResultPageButtonUrl); + + this.set('customResultPageButtonText', null); + + // when + const screen = await render( + hbs` + `, + ); + + // then + assert.dom(screen.queryByRole('link')).doesNotExist(); + }); + }); + + module('when organization custom link label is defined but url is not', function () { + test('not display organization custom link', async function (assert) { + // given + this.set('customResultPageButtonUrl', null); + this.set('customResultPageButtonText', 'Some custom button text'); + + // when + const screen = await render( + hbs` + `, + ); + + // then + assert.dom(screen.queryByRole('link')).doesNotExist(); + }); }); }); }, diff --git a/mon-pix/tests/integration/components/campaigns/assessment/skill-review/hero/evaluation-results-hero-test.js b/mon-pix/tests/integration/components/campaigns/assessment/skill-review/hero/evaluation-results-hero-test.js index 24eac1386cb..0000202d7df 100644 --- a/mon-pix/tests/integration/components/campaigns/assessment/skill-review/hero/evaluation-results-hero-test.js +++ b/mon-pix/tests/integration/components/campaigns/assessment/skill-review/hero/evaluation-results-hero-test.js @@ -346,28 +346,78 @@ module('Integration | Components | Campaigns | Assessment | Skill Review | Evalu }); }); - module('when there is some custom organization content', function () { - test('it should display the organization block', async function (assert) { - //given - this.set('campaign', { - customResultPageText: 'My custom result page text', - organizationId: 1, + module('custom organization block', function () { + module('when customResultPageText if defined', function () { + test('displays the organization block with the text', async function (assert) { + // given + this.set('campaign', { + customResultPageText: 'My custom result page text', + organizationId: 1, + }); + + this.set('campaignParticipationResult', { masteryRate: 0.75 }); + + // when + const screen = await render( + hbs` + `, + ); + + // then + assert.dom(screen.getByText(t('pages.skill-review.organization-message'))).exists(); + assert.dom(screen.getByText('My custom result page text')).exists(); }); + }); - this.set('campaignParticipationResult', { masteryRate: 0.75 }); + module('when campaign has customResultPageButton', function () { + test('displays the organization block with the custom button', async function (assert) { + // given + const store = this.owner.lookup('service:store'); + const campaign = await store.createRecord('campaign', { + customResultPageButtonUrl: 'https://example.net', + customResultPageButtonText: 'Custom result page button text', + organizationId: 1, + }); + this.set('campaign', campaign); + this.set('campaignParticipationResult', { masteryRate: 0.75 }); - // when - const screen = await render( - hbs` - `, - ); + // when + const screen = await render( + hbs` + `, + ); - // then - assert.dom(screen.getByText(t('pages.skill-review.organization-message'))).exists(); - assert.dom(screen.getByText('My custom result page text')).exists(); + // then + assert.dom(screen.getByText(t('pages.skill-review.organization-message'))).exists(); + assert.dom(screen.getByRole('link', { name: 'Custom result page button text' })).exists(); + }); + }); + + module('when campaign has no custom result page button or text', function () { + test('no display the organization block', async function (assert) { + // given + this.set('campaign', { organizationId: 1 }); + this.set('campaignParticipationResult', { masteryRate: 0.75 }); + + // when + const screen = await render( + hbs` + `, + ); + + // then + assert.dom(screen.queryByText(t('pages.skill-review.organization-message'))).doesNotExist(); + assert.dom(screen.queryByText('My custom result page text')).doesNotExist(); + }); }); }); diff --git a/mon-pix/tests/unit/models/campaign-test.js b/mon-pix/tests/unit/models/campaign-test.js new file mode 100644 index 00000000000..e8e5405f74d --- /dev/null +++ b/mon-pix/tests/unit/models/campaign-test.js @@ -0,0 +1,53 @@ +import { setupTest } from 'ember-qunit'; +import { module, test } from 'qunit'; + +module('Unit | Model | campaign', function (hooks) { + setupTest(hooks); + + let store; + + hooks.beforeEach(function () { + store = this.owner.lookup('service:store'); + }); + + module('#hasCustomResultPageButton', function () { + test('returns true when there are a button url and button text', function (assert) { + // given + const campaignParams = { + customResultPageButtonUrl: 'https://example.net', + customResultPageButtonText: 'a result page button text', + }; + + // when + const campaign = store.createRecord('campaign', campaignParams); + + // then + assert.true(campaign.hasCustomResultPageButton); + }); + test('returns false when there is a button url but no button text', function (assert) { + // given + const campaignParams = { + customResultPageButtonUrl: 'https://example.net', + customResultPageButtonText: null, + }; + + // when + const campaign = store.createRecord('campaign', campaignParams); + + // then + assert.false(campaign.hasCustomResultPageButton); + }); + test('returns false when there is a button text but no button url', function (assert) { + const campaignParams = { + customResultPageButtonUrl: null, + customResultPageButtonText: 'a result button text', + }; + + // when + const campaign = store.createRecord('campaign', campaignParams); + + // then + assert.false(campaign.hasCustomResultPageButton); + }); + }); +});