Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Bloquer le test de certif si l’extension n’est pas détectée (PIX-12780) #10276

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mon-pix/app/components/assessments/assessments.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import CompanionBlocker from '../companion/blocker';

<template>
{{#if @assessment.isCertification}}
<CompanionBlocker>
{{yield}}
</CompanionBlocker>
{{else}}
{{yield}}
{{/if}}
</template>
7 changes: 3 additions & 4 deletions mon-pix/app/components/companion/blocker.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { service } from '@ember/service';
import Component from '@glimmer/component';
import { t } from 'ember-intl';
import ENV from 'mon-pix/config/environment';

import ShieldIcon from './shield-icon';

Expand All @@ -11,16 +10,16 @@ export default class CompanionBlocker extends Component {

constructor(...args) {
super(...args);
if (ENV.APP.FT_IS_PIX_COMPANION_MANDATORY) this.pixCompanion.startCheckingExtensionIsEnabled();
this.pixCompanion.startCheckingExtensionIsEnabled();
}

willDestroy(...args) {
super.willDestroy(...args);
if (ENV.APP.FT_IS_PIX_COMPANION_MANDATORY) this.pixCompanion.stopCheckingExtensionIsEnabled();
this.pixCompanion.stopCheckingExtensionIsEnabled();
}

get isBlocked() {
return ENV.APP.FT_IS_PIX_COMPANION_MANDATORY && !this.pixCompanion.isExtensionEnabled;
return !this.pixCompanion.isExtensionEnabled;
}

<template>
Expand Down
24 changes: 16 additions & 8 deletions mon-pix/app/services/pix-companion.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';
import ENV from 'mon-pix/config/environment';

export default class PixCompanion extends Service {
@tracked isExtensionEnabled = true;
@tracked _isExtensionEnabled = true;

#checkExtensionIsEnabledInterval;

Expand All @@ -16,13 +17,15 @@ export default class PixCompanion extends Service {
windowRef.postMessage({ event: 'pix:certification:stop' }, windowRef.location.origin);
}

startCheckingExtensionIsEnabled() {
this.checkExtensionIsEnabled();
this.#checkExtensionIsEnabledInterval = setInterval(() => this.checkExtensionIsEnabled(), 1000);
startCheckingExtensionIsEnabled(windowRef = window) {
if (!ENV.APP.FT_IS_PIX_COMPANION_MANDATORY) return;
this.checkExtensionIsEnabled(windowRef);
this.#checkExtensionIsEnabledInterval = windowRef.setInterval(() => this.checkExtensionIsEnabled(windowRef), 1000);
}

stopCheckingExtensionIsEnabled() {
clearInterval(this.#checkExtensionIsEnabledInterval);
stopCheckingExtensionIsEnabled(windowRef = window) {
if (!ENV.APP.FT_IS_PIX_COMPANION_MANDATORY) return;
windowRef.clearInterval(this.#checkExtensionIsEnabledInterval);
}

checkExtensionIsEnabled(windowRef = window) {
Expand All @@ -38,13 +41,18 @@ export default class PixCompanion extends Service {

pong.promise
.then(() => {
this.isExtensionEnabled = true;
this._isExtensionEnabled = true;
})
.catch(() => {
this.isExtensionEnabled = false;
this._isExtensionEnabled = false;
windowRef.removeEventListener('pix:companion:pong', pongListener);
});
}

get isExtensionEnabled() {
if (!ENV.APP.FT_IS_PIX_COMPANION_MANDATORY) return true;
return this._isExtensionEnabled;
}
}

function pingCompanion(windowRef = window) {
Expand Down
4 changes: 3 additions & 1 deletion mon-pix/app/templates/assessments.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{{page-title @model.title}}

{{outlet}}
<Assessments::Assessments @assessment={{@model}}>
{{outlet}}
</Assessments::Assessments>
2 changes: 1 addition & 1 deletion mon-pix/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ module.exports = function (environment) {
ENV.APP.isTimerCountdownEnabled = false;
ENV.APP.LOAD_EXTERNAL_SCRIPT = false;
ENV.APP.FT_FOCUS_CHALLENGE_ENABLED = true;
ENV.APP.FT_IS_PIX_COMPANION_MANDATORY = true;
ENV.APP.FT_IS_PIX_COMPANION_MANDATORY = false;
ENV.APP.AUTONOMOUS_COURSES_ORGANIZATION_ID = 999;
ENV.metrics.enabled = false;
}
Expand Down
144 changes: 144 additions & 0 deletions mon-pix/tests/integration/components/assessments/assessment-test.gjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { render } from '@1024pix/ember-testing-library';
import Service from '@ember/service';
import Assessments from 'mon-pix/components/assessments/assessments';
import { module, test } from 'qunit';
import sinon from 'sinon';

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

module('Integration | Component | Assessments | assessments', function (hooks) {
setupIntlRenderingTest(hooks);

module('when extension is enabled', function () {
test('it displays assessment page', async function (assert) {
// given
const startCheckingExtensionIsEnabledStub = sinon.stub();
const stopCheckingExtensionIsEnabledStub = sinon.stub();

class PixCompanionStub extends Service {
startCheckingExtensionIsEnabled = startCheckingExtensionIsEnabledStub;
stopCheckingExtensionIsEnabled = stopCheckingExtensionIsEnabledStub;
isExtensionEnabled = true;
}

this.owner.register('service:pix-companion', PixCompanionStub);

const assessment = { isCertification: true };
const title = 'Première question';

// when
const screen = await render(
<template>
<Assessments @assessment={{assessment}}>
<h1>{{title}}</h1>
</Assessments>
</template>,
);

// then
assert.dom(screen.getByRole('heading', { name: title })).exists();
nlepage marked this conversation as resolved.
Show resolved Hide resolved
});

module("when assessment's type is not certification", function () {
test('it displays assessment page', async function (assert) {
// given
const startCheckingExtensionIsEnabledStub = sinon.stub();
const stopCheckingExtensionIsEnabledStub = sinon.stub();

class PixCompanionStub extends Service {
startCheckingExtensionIsEnabled = startCheckingExtensionIsEnabledStub;
stopCheckingExtensionIsEnabled = stopCheckingExtensionIsEnabledStub;
isExtensionEnabled = true;
}

this.owner.register('service:pix-companion', PixCompanionStub);

const assessment = { isCertification: false };
const title = 'Première question';

// when
const screen = await render(
<template>
<Assessments @assessment={{assessment}}>
<h1>{{title}}</h1>
</Assessments>
</template>,
);

// then
assert.dom(screen.getByRole('heading', { name: title })).exists();
nlepage marked this conversation as resolved.
Show resolved Hide resolved
assert
.dom(screen.queryByRole('heading', { name: 'L’extension Pix Companion n’est pas détectée' }))
.doesNotExist();
});
});
});

module('when extension is disabled', function () {
test('it displays companion blocker page', async function (assert) {
// given
const startCheckingExtensionIsEnabledStub = sinon.stub();
const stopCheckingExtensionIsEnabledStub = sinon.stub();

class PixCompanionStub extends Service {
startCheckingExtensionIsEnabled = startCheckingExtensionIsEnabledStub;
stopCheckingExtensionIsEnabled = stopCheckingExtensionIsEnabledStub;
isExtensionEnabled = false;
}

this.owner.register('service:pix-companion', PixCompanionStub);

const assessment = { isCertification: true };
const title = 'Première question';

// when
const screen = await render(
<template>
<Assessments @assessment={{assessment}}>
<h1>{{title}}</h1>
</Assessments>
</template>,
);

// then
assert.dom(screen.queryByRole('heading', { name: title })).doesNotExist();
assert
.dom(screen.getByRole('heading', { level: 1, name: 'L’extension Pix Companion n’est pas détectée' }))
nlepage marked this conversation as resolved.
Show resolved Hide resolved
.exists();
});

module("when assessment's type is not certification", function () {
test('it displays assessment page', async function (assert) {
// given
const startCheckingExtensionIsEnabledStub = sinon.stub();
const stopCheckingExtensionIsEnabledStub = sinon.stub();

class PixCompanionStub extends Service {
startCheckingExtensionIsEnabled = startCheckingExtensionIsEnabledStub;
stopCheckingExtensionIsEnabled = stopCheckingExtensionIsEnabledStub;
isExtensionEnabled = false;
}

this.owner.register('service:pix-companion', PixCompanionStub);

const assessment = { isCertification: false };
const title = 'Première question';

// when
const screen = await render(
<template>
<Assessments @assessment={{assessment}}>
<h1>{{title}}</h1>
</Assessments>
</template>,
);

// then
assert.dom(screen.getByRole('heading', { name: title })).exists();
nlepage marked this conversation as resolved.
Show resolved Hide resolved
assert
.dom(screen.queryByRole('heading', { name: 'L’extension Pix Companion n’est pas détectée' }))
.doesNotExist();
});
});
});
});
37 changes: 0 additions & 37 deletions mon-pix/tests/integration/components/companion/blocker-test.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { render } from '@1024pix/ember-testing-library';
import Service from '@ember/service';
import { t } from 'ember-intl/test-support';
import CompanionBlocker from 'mon-pix/components/companion/blocker';
import ENV from 'mon-pix/config/environment';
import { module, test } from 'qunit';
import sinon from 'sinon';

Expand Down Expand Up @@ -73,40 +72,4 @@ module('Integration | Component | Companion | blocker', function (hooks) {

// assert.dom(screen.queryByRole('link', { name: t('common.companion.not-detected.link') })).exists();
});

module('FT_IS_PIX_COMPANION_MANDATORY feature toggle', function (hooks) {
const ORIGINAL_FT_IS_PIX_COMPANION_MANDATORY = ENV.APP.FT_IS_PIX_COMPANION_MANDATORY;

hooks.afterEach(() => {
ENV.APP.FT_IS_PIX_COMPANION_MANDATORY = ORIGINAL_FT_IS_PIX_COMPANION_MANDATORY;
});

test('when FT is false should always display children content', async function (assert) {
// given
ENV.APP.FT_IS_PIX_COMPANION_MANDATORY = false;

const startCheckingExtensionIsEnabledStub = sinon.stub();
const stopCheckingExtensionIsEnabledStub = sinon.stub();

class PixCompanionStub extends Service {
startCheckingExtensionIsEnabled = startCheckingExtensionIsEnabledStub;
stopCheckingExtensionIsEnabled = stopCheckingExtensionIsEnabledStub;
isExtensionEnabled = false;
}

this.owner.register('service:pix-companion', PixCompanionStub);

const title = 'Companion activé';

// when
const screen = await render(
<template>
<CompanionBlocker><h1>{{title}}</h1></CompanionBlocker>
</template>,
);

sinon.assert.notCalled(startCheckingExtensionIsEnabledStub);
assert.dom(screen.queryByRole('heading', { level: 1, name: title })).exists();
});
});
});
Loading
Loading