From a5b687879a1d69a18b2bdd8ab0ba21c427da4959 Mon Sep 17 00:00:00 2001 From: Asaf Shen Date: Mon, 30 Dec 2024 16:56:41 +0200 Subject: [PATCH] fix: return sdk script results after conditional UI --- .../src/lib/descope-wc/DescopeWc.ts | 2 + .../test/webauthnConditionalUi.test.ts | 43 +++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/packages/sdks/web-component/src/lib/descope-wc/DescopeWc.ts b/packages/sdks/web-component/src/lib/descope-wc/DescopeWc.ts index d96e9b5ab..1de90ea23 100644 --- a/packages/sdks/web-component/src/lib/descope-wc/DescopeWc.ts +++ b/packages/sdks/web-component/src/lib/descope-wc/DescopeWc.ts @@ -517,6 +517,7 @@ class DescopeWc extends BaseDescopeWc { flowConfig.version, projectConfig.componentsVersion, { + ...this.getComponentsContext(), transactionId: webauthnTransactionId, response, failure, @@ -937,6 +938,7 @@ class DescopeWc extends BaseDescopeWc { flowConfig.version, projectConfig.componentsVersion, { + ...this.getComponentsContext(), transactionId, response, }, diff --git a/packages/sdks/web-component/test/webauthnConditionalUi.test.ts b/packages/sdks/web-component/test/webauthnConditionalUi.test.ts index 7d531266a..943d21a38 100644 --- a/packages/sdks/web-component/test/webauthnConditionalUi.test.ts +++ b/packages/sdks/web-component/test/webauthnConditionalUi.test.ts @@ -4,9 +4,12 @@ import { fireEvent, waitFor } from '@testing-library/dom'; import { screen } from 'shadow-dom-testing-library'; import { generateSdkResponse, invokeScriptOnload } from './testUtils'; import '../src/lib/descope-wc'; - import { isConditionalLoginSupported } from '../src/lib/helpers/webauthn'; -import { RESPONSE_ACTIONS } from '../src/lib/constants'; +import { RESPONSE_ACTIONS, SDK_SCRIPT_RESULTS_KEY } from '../src/lib/constants'; +// We load forter script in the test because we mock it and ensure it is called properly +import loadForter from '../src/lib/descope-wc/sdkScripts/forter'; + +jest.mock('../src/lib/descope-wc/sdkScripts/forter', () => jest.fn()); jest.mock('../src/lib/helpers/webauthn', () => ({ isConditionalLoginSupported: jest.fn(), @@ -48,7 +51,7 @@ globalThis.DescopeUI = {}; // this is for mocking the pages/theme/config const themeContent = {}; let pageContent = ''; -const configContent = { +let configContent: any = { componentsVersion: '1.2.3', }; @@ -252,6 +255,29 @@ describe('webauthnConditionalUi', () => { }); it('should call next with the correct params when user is logging in using autofill', async () => { + // We use specific connector which exists to test it all end to end + // but we override it above + const scriptId = 'forter'; + const resultKey = 'some-result-key'; + const resultValue = 'some-value'; + + configContent = { + componentsVersion: '1.2.3', + flows: { + otpSignInEmail: { + sdkScripts: [ + { + id: scriptId, + initArgs: { + siteId: 'some-site-id', + }, + resultKey, + }, + ], + }, + }, + }; + startMock.mockReturnValue(generateSdkResponse()); isConditionalLoginSupportedMock.mockReturnValueOnce(true); isWebauthnSupportedMock.mockReturnValueOnce(true); @@ -265,9 +291,20 @@ describe('webauthnConditionalUi', () => { document.body.innerHTML = `

Custom element test

`; + // wait for loadForter to be called + await waitFor(() => expect(loadForter).toHaveBeenCalled(), { + timeout: 3000, + }); + + // trigger the callback, to simulate the script loaded + // get the 3rd argument of the first call to loadForter + const callback = (loadForter as jest.Mock).mock.calls[0][2]; + callback(resultValue); + await waitFor( () => expect(nextMock).toHaveBeenCalledWith('0', '0', 'id', 0, '1.2.3', { + [`${SDK_SCRIPT_RESULTS_KEY}.${scriptId}_${resultKey}`]: resultValue, // should be called with the result of the script response: 'response', transactionId: 'transactionId', }),