Skip to content

Commit

Permalink
✨ api: download results API call from hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Steph0 authored Dec 2, 2024
1 parent f35c2cf commit 5f126cb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
9 changes: 6 additions & 3 deletions mon-pix/app/components/download-session-results.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { t } from 'ember-intl';
import ENV from 'mon-pix/config/environment';
import PixWindow from 'mon-pix/utils/pix-window';

export default class DownloadSessionResults extends Component {
@tracked showErrorMessage = false;
@service requestManager;

@action
async downloadSessionResults() {
async downloadSessionResults(event) {
event.preventDefault();
this.showErrorMessage = false;

try {
const token = decodeURIComponent(PixWindow.getLocationHash().slice(1));
await this.requestManager.request({
url: `${ENV.APP.API_HOST}/api/xxx`,
url: `${ENV.APP.API_HOST}/api/sessions/download-all-results`,
method: 'POST',
body: { token: 'xxx' },
body: JSON.stringify({ token }),
});
} catch {
this.showErrorMessage = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { click } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { t } from 'ember-intl/test-support';
import DownloadSessionResults from 'mon-pix/components/download-session-results';
import ENV from 'mon-pix/config/environment';
import PixWindow from 'mon-pix/utils/pix-window';
import { module, test } from 'qunit';
import sinon from 'sinon';

Expand All @@ -18,6 +20,10 @@ module('Integration | Component | download-session-results', function (hooks) {
sinon.stub(requestManagerService, 'request');
});

hooks.afterEach(function () {
sinon.restore();
});

test('should display component', async function (assert) {
// given
// when
Expand All @@ -33,11 +39,31 @@ module('Integration | Component | download-session-results', function (hooks) {
requestManagerService.request.rejects({ status: 500 });

// when
const screen = await render(hbs`<DownloadSessionResults @showErrorMessage={{this.showErrorMessage}} />`);
const screen = await render(hbs`<DownloadSessionResults />`);
const downloadButton = screen.getByRole('button', { name: t('pages.download-session-results.button.label') });
await click(downloadButton);

// then
assert.dom(screen.getByText(t('pages.download-session-results.errors.expiration'))).exists();
});

test('should trigger the download', async function (assert) {
// given
requestManagerService.request.resolves({ status: 200 });
const tokenHash = 'mytoken';
sinon.stub(PixWindow, 'getLocationHash').returns(`#${tokenHash}`);

// when
const screen = await render(hbs`<DownloadSessionResults />`);
const downloadButton = screen.getByRole('button', { name: t('pages.download-session-results.button.label') });
await click(downloadButton);

// then
sinon.assert.calledWithExactly(requestManagerService.request, {
url: `${ENV.APP.API_HOST}/api/sessions/download-all-results`,
method: 'POST',
body: `{"token":"${tokenHash}"}`,
});
assert.ok(true);
});
});

0 comments on commit 5f126cb

Please sign in to comment.