Skip to content

Commit

Permalink
✨ mon-pix: call companion-alert endpoint when companion is not detected
Browse files Browse the repository at this point in the history
Co-authored-by: Andreia Pena Ferreira <andreia.pena@pix.fr>
  • Loading branch information
nlepage and AndreiaPena committed Oct 14, 2024
1 parent 75625a4 commit 3a484e9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
5 changes: 5 additions & 0 deletions mon-pix/app/adapters/assessment.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ export default class Assessment extends ApplicationAdapter {
const payload = { data: { data: { attributes: { 'challenge-id': challengeId } } } };
return this.ajax(url, 'POST', payload);
}

createCompanionLiveAlert({ assessmentId }) {
const url = `${this.host}/${this.namespace}/assessments/${assessmentId}/companion-alert`;
return this.ajax(url, 'POST');
}
}
30 changes: 22 additions & 8 deletions mon-pix/app/components/assessments/assessments.gjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { action } from '@ember/object';
import { service } from '@ember/service';
import Component from '@glimmer/component';

import CompanionBlocker from '../companion/blocker';

<template>
{{#if @assessment.isCertification}}
<CompanionBlocker>
export default class Assessments extends Component {
@service store;

@action
async createLiveAlert() {
const adapter = this.store.adapterFor('assessment');
await adapter.createCompanionLiveAlert({ assessmentId: this.args.assessment.id });
}

<template>
{{#if @assessment.isCertification}}
<CompanionBlocker @onBlock={{this.createLiveAlert}}>
{{yield}}
</CompanionBlocker>
{{else}}
{{yield}}
</CompanionBlocker>
{{else}}
{{yield}}
{{/if}}
</template>
{{/if}}
</template>
}
16 changes: 16 additions & 0 deletions mon-pix/tests/unit/adapters/assessment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,20 @@ module('Unit | Adapters | assessment', function (hooks) {
);
});
});

module('#createCompanionLiveAlert', function () {
test('should call companion live alert endpoint', async function (assert) {
// given
adapter.ajax = sinon.stub();
const assessmentId = 123;

// when
await adapter.createCompanionLiveAlert({ assessmentId });

// then
assert.ok(
adapter.ajax.calledWith(`http://localhost:3000/api/assessments/${assessmentId}/companion-alert`, 'POST'),
);
});
});
});
30 changes: 30 additions & 0 deletions mon-pix/tests/unit/components/assessments/assessments-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { setupTest } from 'ember-qunit';
import createGlimmerComponent from 'mon-pix/tests/helpers/create-glimmer-component';
import { module, test } from 'qunit';
import sinon from 'sinon';

module('Unit | Component | Assessments | Assessments', function (hooks) {
setupTest(hooks);

module('#createLiveAlert', function () {
test('should create a companion live alert', async function (assert) {
// given
const component = createGlimmerComponent('assessments/assessments', {
assessment: {
id: 123,
isCertification: true,
},
});
const store = this.owner.lookup('service:store');
const adapter = store.adapterFor('assessment');
const createCompanionLiveAlertStub = sinon.stub(adapter, 'createCompanionLiveAlert');

// when
await component.createLiveAlert();

// then
sinon.assert.calledWithExactly(createCompanionLiveAlertStub, { assessmentId: 123 });
assert.ok(true);
});
});
});

0 comments on commit 3a484e9

Please sign in to comment.