@@ -3,132 +3,93 @@ import path from 'path'
3
3
import * as Vite from 'vite'
4
4
import FastifyRenderer , { build } from '../src/node'
5
5
import { FastifyRendererPlugin } from '../src/node/Plugin'
6
- import { ReactRenderer } from '../src/node/renderers/react/ReactRenderer'
7
6
import { kRenderOptions } from '../src/node/symbols'
8
7
import { newFastify } from './helpers'
9
8
9
+ const testComponent = require . resolve ( path . join ( __dirname , 'fixtures' , 'test-module.tsx' ) )
10
+ const testLayoutComponent = require . resolve ( path . join ( __dirname , 'fixtures' , 'test-layout.tsx' ) )
11
+ const options = { vite : { root : __dirname , logLevel : ( process . env . LOG_LEVEL ?? 'info' ) as any } }
12
+
10
13
describe ( 'FastifyRenderer' , ( ) => {
11
- beforeEach ( ( ) => {
14
+ let server
15
+ beforeEach ( async ( ) => {
12
16
jest . clearAllMocks ( )
17
+
18
+ server = await newFastify ( )
19
+ await server . register ( FastifyRenderer , options )
20
+ server . setRenderConfig ( {
21
+ base : '' ,
22
+ layout : testLayoutComponent ,
23
+ } )
13
24
} )
14
25
15
26
test ( 'should throw if the fastify-renderer plugin was already registered in the same fastify context' , async ( ) => {
16
- const server = await newFastify ( )
27
+ server = await newFastify ( )
17
28
18
- await server . register ( FastifyRenderer )
29
+ await server . register ( FastifyRenderer , options )
19
30
await expect ( server . register ( FastifyRenderer ) ) . rejects . toThrow ( "The decorator 'vite' has already been added!" )
20
31
} )
21
32
22
33
test ( 'should throw if the fastify-renderer plugin was already registered in a different context' , async ( ) => {
23
- const server = await newFastify ( )
34
+ server = await newFastify ( )
24
35
25
- await server . register ( FastifyRenderer )
36
+ await server . register ( FastifyRenderer , options )
26
37
await expect ( server . register ( async ( instance ) => instance . register ( FastifyRenderer ) ) ) . rejects . toThrow ( )
27
38
} )
28
39
29
40
test ( 'should decorate the fastify instance with a "setRenderConfig" method' , async ( ) => {
30
- const server = await newFastify ( )
31
- await server . register ( FastifyRenderer )
41
+ server = await newFastify ( )
42
+ await server . register ( FastifyRenderer , options )
32
43
expect ( server . setRenderConfig ) . toBeInstanceOf ( Function )
44
+ server . setRenderConfig ( {
45
+ base : '/foo' ,
46
+ } )
33
47
} )
34
48
35
49
test ( 'should set default render config for the instance' , async ( ) => {
36
- const server = await newFastify ( )
37
-
38
- expect ( server [ kRenderOptions ] ) . toBeUndefined ( )
39
- await server . register ( FastifyRenderer )
40
50
expect ( server [ kRenderOptions ] ) . toBeInstanceOf ( Object )
41
51
} )
42
52
43
53
test ( 'should set the renderOptions on the new fastify instance context' , async ( ) => {
44
- const server = await newFastify ( )
45
-
46
- await server . register ( FastifyRenderer )
47
54
await server . register ( async ( instance ) => {
48
55
expect ( server [ kRenderOptions ] ) . toEqual ( instance [ kRenderOptions ] )
49
56
} )
50
57
} )
51
58
52
59
test ( 'should mount vite routes at a prefix to avoid collision with user routes' , async ( ) => {
53
- const server = await newFastify ( )
54
-
55
- await server . register ( FastifyRenderer )
56
60
expect ( server . printRoutes ( ) ) . toMatch ( '/.vite/' )
57
61
} )
58
62
59
- // test.skip('should use config from vite dev server in dev mode', async () => {
60
- // });
61
-
62
63
test ( 'should close vite devServer when fastify server is closing in dev mode' , async ( ) => {
63
64
const devServer = await Vite . createServer ( )
64
65
const closeSpy = jest . spyOn ( devServer , 'close' )
65
66
jest . spyOn ( Vite , 'createServer' ) . mockImplementation ( async ( ) => devServer )
66
67
67
- const server = await newFastify ( )
68
- await server . register ( FastifyRenderer , { devMode : true } )
68
+ server = await newFastify ( )
69
+ await server . register ( FastifyRenderer , { ... options , devMode : true } )
69
70
await server . listen ( 0 )
70
71
await server . close ( )
71
72
72
73
expect ( closeSpy ) . toHaveBeenCalled ( )
73
74
} )
74
75
75
76
test ( 'should do nothing if the registered route is not renderable' , async ( ) => {
76
- const server = await newFastify ( )
77
77
const registerRouteSpy = jest . spyOn ( FastifyRendererPlugin . prototype , 'registerRoute' )
78
78
79
- await server . register ( FastifyRenderer )
80
79
server . get ( '/' , async ( _request , reply ) => reply . send ( 'Hello' ) )
81
80
await server . inject ( { method : 'GET' , url : '/' } )
82
81
83
82
expect ( registerRouteSpy ) . toHaveBeenCalledTimes ( 0 )
84
83
} )
85
84
86
85
test ( "should register the route in the plugin if it's renderable" , async ( ) => {
87
- const server = await newFastify ( )
88
86
const registerRouteSpy = jest . spyOn ( FastifyRendererPlugin . prototype , 'registerRoute' ) . mockImplementation ( jest . fn ( ) )
89
87
90
- await server . register ( FastifyRenderer )
91
- server . get ( '/' , { render : 'renderable-module-path' } , async ( request , reply ) => reply . send ( 'Hello' ) )
88
+ server . get ( '/' , { render : testComponent } , async ( request , reply ) => reply . send ( 'Hello' ) )
92
89
await server . inject ( { method : 'GET' , url : '/' } )
93
90
94
91
expect ( registerRouteSpy ) . toHaveBeenCalledTimes ( 1 )
95
92
} )
96
-
97
- test ( 'should return the route props if content-type is application/json' , async ( ) => {
98
- const server = await newFastify ( )
99
- jest . spyOn ( FastifyRendererPlugin . prototype , 'registerRoute' ) . mockImplementation ( jest . fn ( ) )
100
-
101
- await server . register ( FastifyRenderer )
102
- server . get ( '/' , { render : 'renderable-module-path' } , async ( _request , _reply ) => ( { a : 1 , b : 2 } ) )
103
- const response = await server . inject ( { method : 'GET' , url : '/' , headers : { Accept : 'application/json' } } )
104
-
105
- expect ( response . body ) . toEqual ( JSON . stringify ( { props : { a : 1 , b : 2 } } ) )
106
- } )
107
-
108
- test ( 'should render and return html if content-type is text/html' , async ( ) => {
109
- const htmlContent = '<html><body>test content</body></html>'
110
-
111
- jest . spyOn ( ReactRenderer . prototype , 'render' ) . mockImplementation ( async ( render ) => render . reply . send ( htmlContent ) )
112
- jest . spyOn ( FastifyRendererPlugin . prototype , 'registerRoute' ) . mockImplementation ( jest . fn ( ) )
113
-
114
- const server = await newFastify ( )
115
- await server . register ( FastifyRenderer )
116
- server . get ( '/' , { render : 'renderable-module-path' } , async ( _request , _reply ) => ( { a : 1 , b : 2 } ) )
117
- const response = await server . inject ( { method : 'GET' , url : '/' , headers : { Accept : 'text/html' } } )
118
-
119
- expect ( response . body ) . toEqual ( htmlContent )
120
- } )
121
-
122
- test ( 'should set content-type to text/plain and return a message' , async ( ) => {
123
- const server = await newFastify ( )
124
- jest . spyOn ( FastifyRendererPlugin . prototype , 'registerRoute' ) . mockImplementation ( jest . fn ( ) )
125
-
126
- await server . register ( FastifyRenderer )
127
- server . get ( '/' , { render : 'renderable-module-path' } , async ( _request , _reply ) => ( { a : 1 , b : 2 } ) )
128
- const response = await server . inject ( { method : 'GET' , url : '/' , headers : { Accept : 'other' } } )
129
-
130
- expect ( response . body ) . toEqual ( 'Content type not supported' )
131
- } )
132
93
} )
133
94
134
95
describe ( 'build()' , ( ) => {
@@ -139,7 +100,7 @@ describe('build()', () => {
139
100
140
101
test ( 'should build client and server side assets' , async ( ) => {
141
102
const server = await newFastify ( )
142
- await server . register ( FastifyRenderer )
103
+ await server . register ( FastifyRenderer , options )
143
104
await server . listen ( 0 )
144
105
145
106
jest . spyOn ( fs , 'writeFile' ) . mockImplementation ( jest . fn ( ) )
0 commit comments