1- import mfaMachine from '@components/MultifactorAuthentication/machine/mfaMachine' ;
2- import type { MfaContext , MfaEvent } from '@components/MultifactorAuthentication/machine/types' ;
3-
41import { getDeviceBiometricsOnyxKey } from '@libs/actions/MultifactorAuthentication' ;
52
63import CONST from '@src/CONST' ;
74import ONYXKEYS from '@src/ONYXKEYS' ;
85
96import Onyx from 'react-native-onyx' ;
107import getOnyxValue from 'tests/utils/getOnyxValue' ;
11- import createInitEvent from 'tests/utils/mfa/flowFixtures' ;
12- import { VALIDATE_DEVICE_DONE_EVENT_TYPE } from 'tests/utils/mfa/flowPaths' ;
8+ import { createActorAtState , sendValidateDeviceDone } from 'tests/utils/mfa/flowActors' ;
139import waitForBatchedUpdates from 'tests/utils/waitForBatchedUpdates' ;
14- import { createActor } from 'xstate' ;
1510
1611const MFA_STATE = CONST . MULTIFACTOR_AUTHENTICATION . MFA_STATE ;
1712const TEST_ACCOUNT_ID = 12345 ;
@@ -20,51 +15,17 @@ const TEST_ACCOUNT_ID = 12345;
2015// a wrong target adjusts those expectations and still passes. This suite pins the soft-prompt hops by
2116// hand: eligible device -> soft prompt, and approval -> success outcome plus the persisted acceptance.
2217
23- function createCleanContext ( ) : MfaContext {
24- const initEvent = createInitEvent ( ) ;
25- return {
26- error : undefined ,
27- scenarioName : initEvent . scenarioName ,
28- scenario : initEvent . scenario ,
29- payload : initEvent . payload ,
30- softPromptApproved : false ,
31- isCancelConfirmVisible : false ,
32- } ;
33- }
34-
35- function createAwaitingSoftPromptActor ( ) {
36- const awaitingSoftPromptSnapshot = mfaMachine . resolveState ( {
37- value : { [ MFA_STATE . OPEN ] : { [ MFA_STATE . PROMPT ] : MFA_STATE . AWAITING_SOFT_PROMPT } } ,
38- context : createCleanContext ( ) ,
39- } ) ;
40- return createActor ( mfaMachine , { snapshot : awaitingSoftPromptSnapshot } ) ;
41- }
42-
43- function createValidatingDeviceActor ( ) {
44- const validatingDeviceSnapshot = mfaMachine . resolveState ( {
45- value : { [ MFA_STATE . OPEN ] : { [ MFA_STATE . PREPARING ] : MFA_STATE . VALIDATING_DEVICE } } ,
46- context : createCleanContext ( ) ,
47- } ) ;
48- return createActor ( mfaMachine , { snapshot : validatingDeviceSnapshot } ) ;
49- }
50-
51- function settleEligibleDevice ( actor : ReturnType < typeof createValidatingDeviceActor > ) {
52- // Framework actor events are not part of the application's MfaEvent union.
53- // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
54- actor . send ( { type : VALIDATE_DEVICE_DONE_EVENT_TYPE , output : { success : true } } as unknown as MfaEvent ) ;
55- }
56-
5718describe ( 'MFA soft prompt' , ( ) => {
5819 afterEach ( async ( ) => {
5920 await Onyx . clear ( ) ;
6021 await waitForBatchedUpdates ( ) ;
6122 } ) ;
6223
6324 it ( 'moves an eligible device to the soft prompt without approving it' , ( ) => {
64- const actor = createValidatingDeviceActor ( ) ;
25+ const actor = createActorAtState ( { [ MFA_STATE . OPEN ] : { [ MFA_STATE . PREPARING ] : MFA_STATE . VALIDATING_DEVICE } } ) ;
6526
6627 actor . start ( ) ;
67- settleEligibleDevice ( actor ) ;
28+ sendValidateDeviceDone ( actor , { success : true } ) ;
6829
6930 const result = actor . getSnapshot ( ) ;
7031 expect ( result . matches ( { [ MFA_STATE . OPEN ] : { [ MFA_STATE . PROMPT ] : MFA_STATE . AWAITING_SOFT_PROMPT } } ) ) . toBe ( true ) ;
@@ -79,10 +40,10 @@ describe('MFA soft prompt', () => {
7940 await Onyx . merge ( ONYXKEYS . SESSION , { accountID : TEST_ACCOUNT_ID } ) ;
8041 await Onyx . merge ( getDeviceBiometricsOnyxKey ( TEST_ACCOUNT_ID ) , { hasAcceptedSoftPrompt : true } ) ;
8142 await waitForBatchedUpdates ( ) ;
82- const actor = createValidatingDeviceActor ( ) ;
43+ const actor = createActorAtState ( { [ MFA_STATE . OPEN ] : { [ MFA_STATE . PREPARING ] : MFA_STATE . VALIDATING_DEVICE } } ) ;
8344
8445 actor . start ( ) ;
85- settleEligibleDevice ( actor ) ;
46+ sendValidateDeviceDone ( actor , { success : true } ) ;
8647
8748 const result = actor . getSnapshot ( ) ;
8849 expect ( result . matches ( { [ MFA_STATE . OPEN ] : { [ MFA_STATE . OUTCOME ] : MFA_STATE . SUCCESS } } ) ) . toBe ( true ) ;
@@ -93,7 +54,7 @@ describe('MFA soft prompt', () => {
9354 } ) ;
9455
9556 it ( 'reaches the success outcome when the user approves the soft prompt' , ( ) => {
96- const actor = createAwaitingSoftPromptActor ( ) ;
57+ const actor = createActorAtState ( { [ MFA_STATE . OPEN ] : { [ MFA_STATE . PROMPT ] : MFA_STATE . AWAITING_SOFT_PROMPT } } ) ;
9758
9859 actor . start ( ) ;
9960 actor . send ( { type : 'SOFT_PROMPT_APPROVED' } ) ;
@@ -110,7 +71,7 @@ describe('MFA soft prompt', () => {
11071 // the session must settle before the event fires.
11172 await Onyx . merge ( ONYXKEYS . SESSION , { accountID : TEST_ACCOUNT_ID } ) ;
11273 await waitForBatchedUpdates ( ) ;
113- const actor = createAwaitingSoftPromptActor ( ) ;
74+ const actor = createActorAtState ( { [ MFA_STATE . OPEN ] : { [ MFA_STATE . PROMPT ] : MFA_STATE . AWAITING_SOFT_PROMPT } } ) ;
11475
11576 actor . start ( ) ;
11677 actor . send ( { type : 'SOFT_PROMPT_APPROVED' } ) ;
0 commit comments