1
- import { describe , it , expect , vi , beforeEach , afterEach , Mock , type Mocked } from 'vitest ' ;
2
- import { actionHandler } from './action-handler ' ;
1
+ import type { QRL } from 'packages/qwik/public ' ;
2
+ import { afterEach , beforeEach , describe , expect , it , Mock , vi , type Mocked } from 'vitest ' ;
3
3
import type { ActionInternal , ActionStore } from '../../../runtime/src/types' ;
4
4
import type { RequestEventInternal } from '../request-event' ;
5
- import type { QwikSerializer } from '../types' ;
5
+ import { getRequestActions , getRequestMode } from '../request-event' ;
6
+ import { measure , verifySerializable } from '../resolve-request-handlers' ;
6
7
import { IsQAction , QActionId } from '../user-response' ;
7
- import { RequestEvQwikSerializer } from '../request-event' ;
8
- import type { QRL } from 'packages/qwik/public' ;
8
+ import { actionHandler } from './action-handler' ;
9
+ import { runValidators } from './validator-utils' ;
10
+ import { _serialize } from '@qwik.dev/core/internal' ;
9
11
10
12
// Mock dependencies
11
- vi . mock ( './loader-handler ' , ( ) => ( {
13
+ vi . mock ( './validator-utils ' , ( ) => ( {
12
14
runValidators : vi . fn ( ) ,
13
15
} ) ) ;
14
16
@@ -23,14 +25,32 @@ vi.mock('../request-event', () => ({
23
25
RequestEvQwikSerializer : Symbol ( 'RequestEvQwikSerializer' ) ,
24
26
} ) ) ;
25
27
26
- const { runValidators } = await import ( './loader-handler' ) ;
27
- const { measure, verifySerializable } = await import ( '../resolve-request-handlers' ) ;
28
- const { getRequestActions, getRequestMode } = await import ( '../request-event' ) ;
28
+ function createMockAction ( id : string , hash : string ) : Mocked < ActionInternal > {
29
+ const mockActionFunction = ( ) : Mocked < ActionStore < unknown , unknown > > => ( {
30
+ actionPath : `?action=${ id } ` ,
31
+ isRunning : false ,
32
+ status : undefined ,
33
+ value : undefined ,
34
+ formData : undefined ,
35
+ submit : vi . fn ( ) as any ,
36
+ submitted : false ,
37
+ } ) ;
38
+
39
+ return {
40
+ __brand : 'server_action' as const ,
41
+ __id : id ,
42
+ __qrl : {
43
+ call : vi . fn ( ) ,
44
+ getHash : vi . fn ( ) . mockReturnValue ( hash ) ,
45
+ } as unknown as Mocked < QRL < ( form : any , event : any ) => any > > ,
46
+ __validators : [ ] ,
47
+ ...mockActionFunction ,
48
+ } as unknown as Mocked < ActionInternal > ;
49
+ }
29
50
30
51
describe ( 'actionHandler' , ( ) => {
31
52
let mockRequestEvent : Mocked < RequestEventInternal > ;
32
53
let mockAction : Mocked < ActionInternal > ;
33
- let mockQwikSerializer : Mocked < QwikSerializer > ;
34
54
let mockActions : Record < string , any > ;
35
55
let consoleSpy : any ;
36
56
@@ -41,42 +61,11 @@ describe('actionHandler', () => {
41
61
// Reset all mocks
42
62
vi . clearAllMocks ( ) ;
43
63
44
- // Mock console.warn
45
64
consoleSpy = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
46
65
47
- // Create mock action
48
- const mockActionFunction = ( ) : Mocked < ActionStore < unknown , unknown > > => ( {
49
- actionPath : `?action=${ mockActionId } ` ,
50
- isRunning : false ,
51
- status : undefined ,
52
- value : undefined ,
53
- formData : undefined ,
54
- submit : vi . fn ( ) as any ,
55
- submitted : false ,
56
- } ) ;
66
+ mockAction = createMockAction ( mockActionId , mockActionHash ) ;
57
67
58
- mockAction = {
59
- __brand : 'server_action' as const ,
60
- __id : mockActionId ,
61
- __qrl : {
62
- call : vi . fn ( ) ,
63
- getHash : vi . fn ( ) . mockReturnValue ( mockActionHash ) ,
64
- } as unknown as Mocked < QRL < ( form : any , event : any ) => any > > ,
65
- __validators : [ ] ,
66
- ...mockActionFunction ,
67
- } as unknown as Mocked < ActionInternal > ;
68
-
69
- // Create mock serializer
70
- mockQwikSerializer = {
71
- _serialize : vi . fn ( ) ,
72
- _deserialize : vi . fn ( ) ,
73
- _verifySerializable : vi . fn ( ) ,
74
- } as Mocked < QwikSerializer > ;
75
-
76
- // Create mock actions record
77
68
mockActions = { } ;
78
-
79
- // Create mock request event
80
69
mockRequestEvent = {
81
70
sharedMap : new Map ( ) ,
82
71
headersSent : false ,
@@ -123,9 +112,8 @@ describe('actionHandler', () => {
123
112
} as unknown as Mocked < RequestEventInternal > ;
124
113
125
114
// Set up default mocks
126
- ( getRequestActions as Mock ) . mockReturnValue ( mockActions ) ;
127
- ( getRequestMode as Mock ) . mockReturnValue ( 'dev' ) ;
128
- mockRequestEvent [ RequestEvQwikSerializer ] = mockQwikSerializer ;
115
+ vi . mocked ( getRequestActions ) . mockReturnValue ( mockActions ) ;
116
+ vi . mocked ( getRequestMode ) . mockReturnValue ( 'dev' ) ;
129
117
} ) ;
130
118
131
119
afterEach ( ( ) => {
@@ -241,33 +229,30 @@ describe('actionHandler', () => {
241
229
242
230
const data = { test : 'data' } ;
243
231
244
- ( mockRequestEvent . parseBody as Mock ) . mockResolvedValue ( data ) ;
232
+ vi . mocked ( mockRequestEvent . parseBody ) . mockResolvedValue ( data ) ;
245
233
( runValidators as Mock ) . mockResolvedValue ( {
246
234
success : true ,
247
235
data,
248
236
} ) ;
249
237
250
- ( mockQwikSerializer . _serialize as Mock ) . mockResolvedValue ( data ) ;
251
-
252
238
const handler = actionHandler ( [ mockAction ] ) ;
253
239
254
240
await handler ( mockRequestEvent ) ;
255
241
256
- expect ( mockRequestEvent . send ) . toBeCalledWith ( 200 , data ) ;
242
+ expect ( mockRequestEvent . send ) . toBeCalledWith ( 200 , await _serialize ( [ undefined ] ) ) ;
257
243
} ) ;
258
244
} ) ;
259
245
260
246
describe ( 'when action is found and executed successfully' , ( ) => {
261
247
beforeEach ( ( ) => {
262
248
mockRequestEvent . sharedMap . set ( IsQAction , true ) ;
263
249
mockRequestEvent . sharedMap . set ( QActionId , mockActionId ) ;
264
- ( mockRequestEvent . parseBody as Mock ) . mockResolvedValue ( { test : 'data' } ) ;
265
- ( runValidators as Mock ) . mockResolvedValue ( {
250
+ vi . mocked ( mockRequestEvent . parseBody ) . mockResolvedValue ( { test : 'data' } ) ;
251
+ vi . mocked ( runValidators ) . mockResolvedValue ( {
266
252
success : true ,
267
253
data : { test : 'data' } ,
268
254
} ) ;
269
- ( mockAction . __qrl . call as Mock ) . mockResolvedValue ( { result : 'success' } ) ;
270
- ( mockQwikSerializer . _serialize as Mock ) . mockResolvedValue ( 'serialized-data' ) ;
255
+ vi . mocked ( mockAction . __qrl . call ) . mockResolvedValue ( { result : 'success' } ) ;
271
256
} ) ;
272
257
273
258
it ( 'should execute action and return serialized data' , async ( ) => {
@@ -286,29 +271,29 @@ describe('actionHandler', () => {
286
271
{ test : 'data' } ,
287
272
mockRequestEvent
288
273
) ;
289
- expect ( mockQwikSerializer . _serialize ) . toHaveBeenCalledWith ( [ { result : 'success' } ] ) ;
290
274
expect ( mockRequestEvent . headers . set ) . toHaveBeenCalledWith (
291
275
'Content-Type' ,
292
276
'application/json; charset=utf-8'
293
277
) ;
294
- expect ( mockRequestEvent . send ) . toHaveBeenCalledWith ( 200 , 'serialized-data' ) ;
278
+ expect ( mockRequestEvent . send ) . toHaveBeenCalledWith (
279
+ 200 ,
280
+ await _serialize ( [ { result : 'success' } ] )
281
+ ) ;
295
282
} ) ;
296
283
297
284
it ( 'should measure execution time in dev mode' , async ( ) => {
285
+ vi . mocked ( getRequestMode ) . mockReturnValue ( 'dev' ) ;
286
+
298
287
const handler = actionHandler ( [ mockAction ] ) ;
299
288
300
289
await handler ( mockRequestEvent ) ;
301
290
302
291
expect ( measure ) . toHaveBeenCalledWith ( mockRequestEvent , mockActionHash , expect . any ( Function ) ) ;
303
- expect ( verifySerializable ) . toHaveBeenCalledWith (
304
- mockQwikSerializer ,
305
- { result : 'success' } ,
306
- mockAction . __qrl
307
- ) ;
292
+ expect ( verifySerializable ) . toHaveBeenCalledWith ( { result : 'success' } , mockAction . __qrl ) ;
308
293
} ) ;
309
294
310
295
it ( 'should not measure execution time in production mode' , async ( ) => {
311
- ( getRequestMode as Mock ) . mockReturnValue ( 'prod ' ) ;
296
+ vi . mocked ( getRequestMode ) . mockReturnValue ( 'server ' ) ;
312
297
313
298
const handler = actionHandler ( [ mockAction ] ) ;
314
299
@@ -325,7 +310,6 @@ describe('actionHandler', () => {
325
310
326
311
await handler ( mockRequestEvent ) ;
327
312
328
- expect ( mockQwikSerializer . _serialize ) . not . toHaveBeenCalled ( ) ;
329
313
expect ( mockRequestEvent . send ) . not . toHaveBeenCalled ( ) ;
330
314
} ) ;
331
315
} ) ;
@@ -473,7 +457,6 @@ describe('actionHandler', () => {
473
457
data : { test : 'data' } ,
474
458
} ) ;
475
459
( mockAction . __qrl . call as Mock ) . mockResolvedValue ( { result : 'success' } ) ;
476
- ( mockQwikSerializer . _serialize as Mock ) . mockResolvedValue ( 'serialized-data' ) ;
477
460
478
461
const handler = actionHandler ( [ mockAction ] ) ;
479
462
0 commit comments