From 5f126cb5a7d5eedcecd88e930e9c7a06d957d4e1 Mon Sep 17 00:00:00 2001 From: Steph0 Date: Fri, 29 Nov 2024 17:45:08 +0100 Subject: [PATCH] :sparkles: api: download results API call from hash --- .../components/download-session-results.gjs | 9 ++++-- .../download-session-results-test.gjs | 28 ++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/mon-pix/app/components/download-session-results.gjs b/mon-pix/app/components/download-session-results.gjs index 0b7ca23d692..ac81fa4b264 100644 --- a/mon-pix/app/components/download-session-results.gjs +++ b/mon-pix/app/components/download-session-results.gjs @@ -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; diff --git a/mon-pix/tests/integration/components/download-session-results-test.gjs b/mon-pix/tests/integration/components/download-session-results-test.gjs index 30ff4d0dcf7..0d2e309e54b 100644 --- a/mon-pix/tests/integration/components/download-session-results-test.gjs +++ b/mon-pix/tests/integration/components/download-session-results-test.gjs @@ -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'; @@ -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 @@ -33,11 +39,31 @@ module('Integration | Component | download-session-results', function (hooks) { requestManagerService.request.rejects({ status: 500 }); // when - const screen = await render(hbs``); + const screen = await render(hbs``); 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``); + 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); + }); });