@@ -33,9 +33,9 @@ vi.mock("@/lib/common/utils", () => ({
3333} ) ) ;
3434
3535vi . mock ( "@/lib/common/api" , ( ) => ( {
36- ApiClient : vi . fn ( ) . mockImplementation ( ( ) => ( {
37- createOrUpdateUser : vi . fn ( ) ,
38- } ) ) ,
36+ ApiClient : vi . fn ( ) . mockImplementation ( function ( ) {
37+ return { createOrUpdateUser : vi . fn ( ) } ;
38+ } ) ,
3939} ) ) ;
4040
4141describe ( "sendUpdatesToBackend" , ( ) => {
@@ -56,9 +56,9 @@ describe("sendUpdatesToBackend", () => {
5656 } ,
5757 } ;
5858
59- ( ApiClient as Mock ) . mockImplementation ( ( ) => ( {
60- createOrUpdateUser : vi . fn ( ) . mockResolvedValue ( mockResponse ) ,
61- } ) ) ;
59+ ( ApiClient as Mock ) . mockImplementation ( function ( ) {
60+ return { createOrUpdateUser : vi . fn ( ) . mockResolvedValue ( mockResponse ) } ;
61+ } ) ;
6262
6363 const result = await sendUpdatesToBackend ( {
6464 appUrl : mockAppUrl ,
@@ -68,19 +68,31 @@ describe("sendUpdatesToBackend", () => {
6868
6969 expect ( result . ok ) . toBe ( true ) ;
7070 if ( result . ok ) {
71- expect ( result . data . state . data ) . toEqual ( { userId : mockUserId , attributes : mockAttributes } ) ;
71+ expect ( result . data . state . data ) . toEqual ( {
72+ userId : mockUserId ,
73+ attributes : mockAttributes ,
74+ } ) ;
7275 }
7376 } ) ;
7477
7578 test ( "returns network error if API call fails" , async ( ) => {
76- const mockUpdates : TUpdates = { userId : mockUserId , attributes : mockAttributes } ;
79+ const mockUpdates : TUpdates = {
80+ userId : mockUserId ,
81+ attributes : mockAttributes ,
82+ } ;
7783
78- ( ApiClient as Mock ) . mockImplementation ( ( ) => ( {
79- createOrUpdateUser : vi . fn ( ) . mockResolvedValue ( {
80- ok : false ,
81- error : { code : "network_error" , message : "Request failed" , status : 500 } ,
82- } ) ,
83- } ) ) ;
84+ ( ApiClient as Mock ) . mockImplementation ( function ( ) {
85+ return {
86+ createOrUpdateUser : vi . fn ( ) . mockResolvedValue ( {
87+ ok : false ,
88+ error : {
89+ code : "network_error" ,
90+ message : "Request failed" ,
91+ status : 500 ,
92+ } ,
93+ } ) ,
94+ } ;
95+ } ) ;
8496
8597 const result = await sendUpdatesToBackend ( {
8698 appUrl : mockAppUrl ,
@@ -91,16 +103,25 @@ describe("sendUpdatesToBackend", () => {
91103 expect ( result . ok ) . toBe ( false ) ;
92104 if ( ! result . ok ) {
93105 expect ( result . error . code ) . toBe ( "network_error" ) ;
94- expect ( result . error . message ) . toBe ( "Error updating user with userId user_123" ) ;
106+ expect ( result . error . message ) . toBe (
107+ "Error updating user with userId user_123"
108+ ) ;
95109 }
96110 } ) ;
97111
98112 test ( "throws error if network request fails" , async ( ) => {
99- const mockUpdates : TUpdates = { userId : mockUserId , attributes : { plan : "premium" } } ;
113+ const mockUpdates : TUpdates = {
114+ userId : mockUserId ,
115+ attributes : { plan : "premium" } ,
116+ } ;
100117
101- ( ApiClient as Mock ) . mockImplementation ( ( ) => ( {
102- createOrUpdateUser : vi . fn ( ) . mockRejectedValue ( new Error ( "Network error" ) ) ,
103- } ) ) ;
118+ ( ApiClient as Mock ) . mockImplementation ( function ( ) {
119+ return {
120+ createOrUpdateUser : vi
121+ . fn ( )
122+ . mockRejectedValue ( new Error ( "Network error" ) ) ,
123+ } ;
124+ } ) ;
104125
105126 await expect (
106127 sendUpdatesToBackend ( {
@@ -146,11 +167,13 @@ describe("sendUpdates", () => {
146167 } ,
147168 } ;
148169
149- ( ApiClient as Mock ) . mockImplementation ( ( ) => ( {
150- createOrUpdateUser : vi . fn ( ) . mockResolvedValue ( mockResponse ) ,
151- } ) ) ;
170+ ( ApiClient as Mock ) . mockImplementation ( function ( ) {
171+ return { createOrUpdateUser : vi . fn ( ) . mockResolvedValue ( mockResponse ) } ;
172+ } ) ;
152173
153- const result = await sendUpdates ( { updates : { userId : mockUserId , attributes : mockAttributes } } ) ;
174+ const result = await sendUpdates ( {
175+ updates : { userId : mockUserId , attributes : mockAttributes } ,
176+ } ) ;
154177
155178 expect ( result . ok ) . toBe ( true ) ;
156179 } ) ;
@@ -165,11 +188,15 @@ describe("sendUpdates", () => {
165188 } ,
166189 } ;
167190
168- ( ApiClient as Mock ) . mockImplementation ( ( ) => ( {
169- createOrUpdateUser : vi . fn ( ) . mockResolvedValue ( mockErrorResponse ) ,
170- } ) ) ;
191+ ( ApiClient as Mock ) . mockImplementation ( function ( ) {
192+ return {
193+ createOrUpdateUser : vi . fn ( ) . mockResolvedValue ( mockErrorResponse ) ,
194+ } ;
195+ } ) ;
171196
172- const result = await sendUpdates ( { updates : { userId : mockUserId , attributes : mockAttributes } } ) ;
197+ const result = await sendUpdates ( {
198+ updates : { userId : mockUserId , attributes : mockAttributes } ,
199+ } ) ;
173200
174201 expect ( result . ok ) . toBe ( false ) ;
175202 if ( ! result . ok ) {
@@ -178,11 +205,17 @@ describe("sendUpdates", () => {
178205 } ) ;
179206
180207 test ( "handles unexpected errors" , async ( ) => {
181- ( ApiClient as Mock ) . mockImplementation ( ( ) => ( {
182- createOrUpdate : vi . fn ( ) . mockRejectedValue ( new Error ( "Unexpected error" ) ) ,
183- } ) ) ;
208+ ( ApiClient as Mock ) . mockImplementation ( function ( ) {
209+ return {
210+ createOrUpdateUser : vi
211+ . fn ( )
212+ . mockRejectedValue ( new Error ( "Unexpected error" ) ) ,
213+ } ;
214+ } ) ;
184215
185- const result = await sendUpdates ( { updates : { userId : mockUserId , attributes : mockAttributes } } ) ;
216+ const result = await sendUpdates ( {
217+ updates : { userId : mockUserId , attributes : mockAttributes } ,
218+ } ) ;
186219
187220 expect ( result . ok ) . toBe ( false ) ;
188221 if ( ! result . ok ) {
0 commit comments