@@ -10,7 +10,7 @@ import { describe, expect, it } from 'vitest';
1010import { StepType } from '../types.js' ;
1111import { type Step } from '../index.js' ;
1212
13- import { createJourneyObject , handleJourneyResponse } from './journey.utils.js' ;
13+ import { createJourneyObject , parseJourneyResponse } from './journey.utils.js' ;
1414import type { JourneyLoginFailure } from './login-failure.utils.js' ;
1515
1616describe ( 'createJourneyObject' , ( ) => {
@@ -63,85 +63,99 @@ describe('createJourneyObject', () => {
6363 } ) ;
6464} ) ;
6565
66- describe ( 'handleJourneyResponse ' , ( ) => {
67- it ( 'returns Step data when FetchBaseQueryError has numeric status and object body' , ( ) => {
66+ describe ( 'parseJourneyResponse ' , ( ) => {
67+ it ( 'returns right( Step) when FetchBaseQueryError has numeric status and object body' , ( ) => {
6868 const body = { code : 401 , message : 'Access Denied' , reason : 'Unauthorized' } ;
6969 const error = { status : 401 , data : body } ;
7070
71- const result = handleJourneyResponse ( undefined , error ) ;
71+ const result = parseJourneyResponse ( { data : undefined , error } ) ;
7272
73- expect ( result ) . toBe ( body ) ;
73+ expect ( result . _tag ) . toBe ( 'Right' ) ;
74+ expect ( ( result as { right : unknown } ) . right ) . toBe ( body ) ;
7475 } ) ;
7576
76- it ( 'returns GenericError when FetchBaseQueryError has numeric status but non-object body' , ( ) => {
77+ it ( 'returns left( GenericError) when FetchBaseQueryError has numeric status but non-object body' , ( ) => {
7778 const error = { status : 500 , data : 'Internal Server Error' } ;
7879
79- const result = handleJourneyResponse ( undefined , error ) ;
80+ const result = parseJourneyResponse ( { data : undefined , error } ) ;
8081
81- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
82+ expect ( result . _tag ) . toBe ( 'Left' ) ;
83+ expect ( ( result as { left : unknown } ) . left ) . toMatchObject ( {
84+ error : 'request_failed' ,
85+ type : 'unknown_error' ,
86+ } ) ;
8287 } ) ;
8388
84- it ( 'returns GenericError for FETCH_ERROR' , ( ) => {
89+ it ( 'returns left( GenericError) for FETCH_ERROR' , ( ) => {
8590 const error = { status : 'FETCH_ERROR' as const , error : 'Network error' } ;
8691
87- const result = handleJourneyResponse ( undefined , error ) ;
92+ const result = parseJourneyResponse ( { data : undefined , error } ) ;
8893
89- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
90- expect ( ( result as { message : string } ) . message ) . toContain ( 'Network error' ) ;
94+ expect ( result . _tag ) . toBe ( 'Left' ) ;
95+ expect ( ( result as { left : { message : string } } ) . left . message ) . toContain ( 'Network error' ) ;
9196 } ) ;
9297
93- it ( 'returns GenericError for PARSING_ERROR' , ( ) => {
98+ it ( 'returns left( GenericError) for PARSING_ERROR' , ( ) => {
9499 const error = {
95100 status : 'PARSING_ERROR' as const ,
96101 originalStatus : 200 ,
97102 data : '<html>Not JSON</html>' ,
98103 error : 'JSON parse error' ,
99104 } ;
100105
101- const result = handleJourneyResponse ( undefined , error ) ;
106+ const result = parseJourneyResponse ( { data : undefined , error } ) ;
102107
103- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
104- expect ( ( result as { message : string } ) . message ) . toContain ( 'JSON parse error' ) ;
108+ expect ( result . _tag ) . toBe ( 'Left' ) ;
109+ expect ( ( result as { left : { message : string } } ) . left . message ) . toContain ( 'JSON parse error' ) ;
105110 } ) ;
106111
107- it ( 'returns GenericError for TIMEOUT_ERROR' , ( ) => {
112+ it ( 'returns left( GenericError) for TIMEOUT_ERROR' , ( ) => {
108113 const error = { status : 'TIMEOUT_ERROR' as const , error : 'Request timed out' } ;
109114
110- const result = handleJourneyResponse ( undefined , error ) ;
115+ const result = parseJourneyResponse ( { data : undefined , error } ) ;
111116
112- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
113- expect ( ( result as { message : string } ) . message ) . toContain ( 'Request timed out' ) ;
117+ expect ( result . _tag ) . toBe ( 'Left' ) ;
118+ expect ( ( result as { left : { message : string } } ) . left . message ) . toContain ( 'Request timed out' ) ;
114119 } ) ;
115120
116- it ( 'returns GenericError for CUSTOM_ERROR' , ( ) => {
121+ it ( 'returns left( GenericError) for CUSTOM_ERROR' , ( ) => {
117122 const error = { status : 'CUSTOM_ERROR' as const , error : 'Custom error occurred' } ;
118123
119- const result = handleJourneyResponse ( undefined , error ) ;
124+ const result = parseJourneyResponse ( { data : undefined , error } ) ;
120125
121- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
122- expect ( ( result as { message : string } ) . message ) . toContain ( 'Custom error occurred' ) ;
126+ expect ( result . _tag ) . toBe ( 'Left' ) ;
127+ expect ( ( result as { left : { message : string } } ) . left . message ) . toContain (
128+ 'Custom error occurred' ,
129+ ) ;
123130 } ) ;
124131
125- it ( 'returns GenericError for SerializedError' , ( ) => {
132+ it ( 'returns left( GenericError) for SerializedError' , ( ) => {
126133 const error = { name : 'Error' , message : 'Something went wrong' , stack : '...' } ;
127134
128- const result = handleJourneyResponse ( undefined , error ) ;
135+ const result = parseJourneyResponse ( { data : undefined , error } ) ;
129136
130- expect ( result ) . toMatchObject ( { error : 'request_failed' , type : 'unknown_error' } ) ;
131- expect ( ( result as { message : string } ) . message ) . toContain ( 'Something went wrong' ) ;
137+ expect ( result . _tag ) . toBe ( 'Left' ) ;
138+ expect ( ( result as { left : { message : string } } ) . left . message ) . toContain (
139+ 'Something went wrong' ,
140+ ) ;
132141 } ) ;
133142
134- it ( 'returns GenericError when no data and no error' , ( ) => {
135- const result = handleJourneyResponse ( undefined , undefined ) ;
143+ it ( 'returns left( GenericError) when no data and no error' , ( ) => {
144+ const result = parseJourneyResponse ( { data : undefined , error : undefined } ) ;
136145
137- expect ( result ) . toMatchObject ( { error : 'no_response_data' , type : 'unknown_error' } ) ;
146+ expect ( result . _tag ) . toBe ( 'Left' ) ;
147+ expect ( ( result as { left : unknown } ) . left ) . toMatchObject ( {
148+ error : 'no_response_data' ,
149+ type : 'unknown_error' ,
150+ } ) ;
138151 } ) ;
139152
140- it ( 'returns data when no error and data is present' , ( ) => {
153+ it ( 'returns right(Step) when no error and data is present' , ( ) => {
141154 const data : Step = { authId : 'test-auth-id' , callbacks : [ ] } ;
142155
143- const result = handleJourneyResponse ( data , undefined ) ;
156+ const result = parseJourneyResponse ( { data, error : undefined } ) ;
144157
145- expect ( result ) . toBe ( data ) ;
158+ expect ( result . _tag ) . toBe ( 'Right' ) ;
159+ expect ( ( result as { right : unknown } ) . right ) . toBe ( data ) ;
146160 } ) ;
147161} ) ;
0 commit comments