88import { callbackType } from '@forgerock/sdk-types' ;
99import { afterEach , describe , expect , test , vi } from 'vitest' ;
1010
11- import type { Step } from '@forgerock/sdk-types' ;
11+ import type { GenericError , Step } from '@forgerock/sdk-types' ;
1212
1313import { journey } from './client.store.js' ;
1414import { createJourneyStep } from './step.utils.js' ;
1515import { JourneyClientConfig } from './config.types.js' ;
1616
17+ /**
18+ * Type guard to check if a result is a GenericError
19+ */
20+ function isGenericError ( result : unknown ) : result is GenericError {
21+ return typeof result === 'object' && result !== null && 'error' in result && 'type' in result ;
22+ }
23+
1724// Create a singleton mock instance for storage
1825const mockStorageInstance = {
1926 get : vi . fn ( ) ,
@@ -65,13 +72,16 @@ describe('journey-client', () => {
6572 const client = await journey ( { config : mockConfig } ) ;
6673 const step = await client . start ( ) ;
6774 expect ( step ) . toBeDefined ( ) ;
75+ expect ( isGenericError ( step ) ) . toBe ( false ) ;
6876
6977 expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
7078 const request = mockFetch . mock . calls [ 0 ] [ 0 ] as Request ;
7179 // TODO: This should be /journeys?_action=start, but the current implementation calls /authenticate
7280 expect ( request . url ) . toBe ( 'https://test.com/json/realms/root/authenticate' ) ;
7381 expect ( step ) . toHaveProperty ( 'type' , 'Step' ) ;
74- expect ( step && step . payload ) . toEqual ( mockStepResponse ) ;
82+ if ( ! isGenericError ( step ) ) {
83+ expect ( step . payload ) . toEqual ( mockStepResponse ) ;
84+ }
7585 } ) ;
7686
7787 test ( 'next() should send the current step and return the next step' , async ( ) => {
@@ -101,6 +111,7 @@ describe('journey-client', () => {
101111 const client = await journey ( { config : mockConfig } ) ;
102112 const nextStep = await client . next ( initialStep , { } ) ;
103113 expect ( nextStep ) . toBeDefined ( ) ;
114+ expect ( isGenericError ( nextStep ) ) . toBe ( false ) ;
104115
105116 expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
106117 const request = mockFetch . mock . calls [ 0 ] [ 0 ] as Request ;
@@ -109,7 +120,9 @@ describe('journey-client', () => {
109120 expect ( request . method ) . toBe ( 'POST' ) ;
110121 expect ( await request . json ( ) ) . toEqual ( initialStep . payload ) ;
111122 expect ( nextStep ) . toHaveProperty ( 'type' , 'Step' ) ;
112- expect ( nextStep && nextStep . payload ) . toEqual ( nextStepPayload ) ;
123+ if ( ! isGenericError ( nextStep ) ) {
124+ expect ( nextStep . payload ) . toEqual ( nextStepPayload ) ;
125+ }
113126 } ) ;
114127
115128 test ( 'redirect() should store the step and call location.assign' , async ( ) => {
@@ -169,7 +182,9 @@ describe('journey-client', () => {
169182 expect ( request . method ) . toBe ( 'POST' ) ;
170183 expect ( await request . json ( ) ) . toEqual ( previousStepPayload ) ;
171184 expect ( step ) . toHaveProperty ( 'type' , 'Step' ) ;
172- expect ( step && step . payload ) . toEqual ( nextStepPayload ) ;
185+ if ( ! isGenericError ( step ) ) {
186+ expect ( step . payload ) . toEqual ( nextStepPayload ) ;
187+ }
173188 } ) ;
174189
175190 test ( 'should correctly resume with a plain Step object from storage' , async ( ) => {
@@ -198,7 +213,9 @@ describe('journey-client', () => {
198213 expect ( request . method ) . toBe ( 'POST' ) ;
199214 expect ( await request . json ( ) ) . toEqual ( plainStepPayload ) ; // Expect the plain payload to be sent
200215 expect ( step ) . toHaveProperty ( 'type' , 'Step' ) ; // The returned step should still be an JourneyStep instance
201- expect ( step && step . payload ) . toEqual ( nextStepPayload ) ;
216+ if ( ! isGenericError ( step ) ) {
217+ expect ( step . payload ) . toEqual ( nextStepPayload ) ;
218+ }
202219 } ) ;
203220
204221 test ( 'should throw an error if a previous step is required but not found' , async ( ) => {
@@ -234,7 +251,9 @@ describe('journey-client', () => {
234251 const url = new URL ( request . url ) ;
235252 expect ( url . origin + url . pathname ) . toBe ( 'https://test.com/json/realms/root/authenticate' ) ;
236253 expect ( step ) . toHaveProperty ( 'type' , 'Step' ) ;
237- expect ( step && step . payload ) . toEqual ( mockStepResponse ) ;
254+ if ( ! isGenericError ( step ) ) {
255+ expect ( step . payload ) . toEqual ( mockStepResponse ) ;
256+ }
238257 } ) ;
239258 } ) ;
240259
0 commit comments