1+ using InertiaCore . Models ;
2+ using InertiaCore . Ssr ;
3+ using Microsoft . AspNetCore . Hosting ;
4+ using Microsoft . Extensions . Options ;
5+ using Moq ;
6+
7+ namespace InertiaCoreTests ;
8+
9+ public partial class Tests
10+ {
11+ [ Test ]
12+ [ Description ( "Test SSR dispatch should not dispatch by default when no bundle exists" ) ]
13+ public void TestSsrDispatchDefaultBehaviorWithoutBundle ( )
14+ {
15+ var httpClientFactory = new Mock < IHttpClientFactory > ( ) ;
16+ var environment = new Mock < IWebHostEnvironment > ( ) ;
17+ environment . SetupGet ( x => x . ContentRootPath ) . Returns ( Path . GetTempPath ( ) ) ;
18+
19+ var options = new Mock < IOptions < InertiaOptions > > ( ) ;
20+ options . SetupGet ( x => x . Value ) . Returns ( new InertiaOptions { SsrDispatchWithoutBundle = false } ) ;
21+
22+ var gateway = new Gateway ( httpClientFactory . Object , options . Object , environment . Object ) ;
23+
24+ Assert . That ( gateway . ShouldDispatch ( ) , Is . False ) ;
25+ }
26+
27+ [ Test ]
28+ [ Description ( "Test SSR dispatch should dispatch when SsrDispatchWithoutBundle is enabled" ) ]
29+ public void TestSsrDispatchWithoutBundleEnabled ( )
30+ {
31+ var httpClientFactory = new Mock < IHttpClientFactory > ( ) ;
32+ var environment = new Mock < IWebHostEnvironment > ( ) ;
33+ environment . SetupGet ( x => x . ContentRootPath ) . Returns ( Path . GetTempPath ( ) ) ;
34+
35+ var options = new Mock < IOptions < InertiaOptions > > ( ) ;
36+ options . SetupGet ( x => x . Value ) . Returns ( new InertiaOptions { SsrDispatchWithoutBundle = true } ) ;
37+
38+ var gateway = new Gateway ( httpClientFactory . Object , options . Object , environment . Object ) ;
39+
40+ Assert . That ( gateway . ShouldDispatch ( ) , Is . True ) ;
41+ }
42+
43+ [ Test ]
44+ [ Description ( "Test SSR dispatch should dispatch when bundle exists" ) ]
45+ public void TestSsrDispatchWithBundleExists ( )
46+ {
47+ var tempDir = Path . GetTempPath ( ) ;
48+ var bundleDir = Path . Combine ( tempDir , "wwwroot" , "js" ) ;
49+ Directory . CreateDirectory ( bundleDir ) ;
50+
51+ var bundlePath = Path . Combine ( bundleDir , "ssr.js" ) ;
52+ File . WriteAllText ( bundlePath , "// SSR bundle" ) ;
53+
54+ try
55+ {
56+ var httpClientFactory = new Mock < IHttpClientFactory > ( ) ;
57+ var environment = new Mock < IWebHostEnvironment > ( ) ;
58+ environment . SetupGet ( x => x . ContentRootPath ) . Returns ( tempDir ) ;
59+
60+ var options = new Mock < IOptions < InertiaOptions > > ( ) ;
61+ options . SetupGet ( x => x . Value ) . Returns ( new InertiaOptions { SsrDispatchWithoutBundle = false } ) ;
62+
63+ var gateway = new Gateway ( httpClientFactory . Object , options . Object , environment . Object ) ;
64+
65+ Assert . That ( gateway . ShouldDispatch ( ) , Is . True ) ;
66+ }
67+ finally
68+ {
69+ if ( File . Exists ( bundlePath ) )
70+ File . Delete ( bundlePath ) ;
71+ if ( Directory . Exists ( bundleDir ) )
72+ Directory . Delete ( bundleDir , true ) ;
73+ }
74+ }
75+
76+ [ Test ]
77+ [ Description ( "Test SSR dispatch should dispatch when either bundle exists or dispatch without bundle is enabled" ) ]
78+ public void TestSsrDispatchWithBundleAndDispatchWithoutBundleEnabled ( )
79+ {
80+ var tempDir = Path . GetTempPath ( ) ;
81+ var bundleDir = Path . Combine ( tempDir , "build" ) ;
82+ Directory . CreateDirectory ( bundleDir ) ;
83+
84+ var bundlePath = Path . Combine ( bundleDir , "ssr.js" ) ;
85+ File . WriteAllText ( bundlePath , "// SSR bundle" ) ;
86+
87+ try
88+ {
89+ var httpClientFactory = new Mock < IHttpClientFactory > ( ) ;
90+ var environment = new Mock < IWebHostEnvironment > ( ) ;
91+ environment . SetupGet ( x => x . ContentRootPath ) . Returns ( tempDir ) ;
92+
93+ var options = new Mock < IOptions < InertiaOptions > > ( ) ;
94+ options . SetupGet ( x => x . Value ) . Returns ( new InertiaOptions { SsrDispatchWithoutBundle = true } ) ;
95+
96+ var gateway = new Gateway ( httpClientFactory . Object , options . Object , environment . Object ) ;
97+
98+ Assert . That ( gateway . ShouldDispatch ( ) , Is . True ) ;
99+ }
100+ finally
101+ {
102+ if ( File . Exists ( bundlePath ) )
103+ File . Delete ( bundlePath ) ;
104+ if ( Directory . Exists ( bundleDir ) )
105+ Directory . Delete ( bundleDir , true ) ;
106+ }
107+ }
108+
109+ [ Test ]
110+ [ Description ( "Test SSR dispatch checks multiple common bundle paths" ) ]
111+ public void TestSsrDispatchChecksMultipleBundlePaths ( )
112+ {
113+ var tempDir = Path . GetTempPath ( ) ;
114+ var bundleDir = Path . Combine ( tempDir , "dist" ) ;
115+ Directory . CreateDirectory ( bundleDir ) ;
116+
117+ var bundlePath = Path . Combine ( bundleDir , "ssr.js" ) ;
118+ File . WriteAllText ( bundlePath , "// SSR bundle in dist" ) ;
119+
120+ try
121+ {
122+ var httpClientFactory = new Mock < IHttpClientFactory > ( ) ;
123+ var environment = new Mock < IWebHostEnvironment > ( ) ;
124+ environment . SetupGet ( x => x . ContentRootPath ) . Returns ( tempDir ) ;
125+
126+ var options = new Mock < IOptions < InertiaOptions > > ( ) ;
127+ options . SetupGet ( x => x . Value ) . Returns ( new InertiaOptions { SsrDispatchWithoutBundle = false } ) ;
128+
129+ var gateway = new Gateway ( httpClientFactory . Object , options . Object , environment . Object ) ;
130+
131+ Assert . That ( gateway . ShouldDispatch ( ) , Is . True ) ;
132+ }
133+ finally
134+ {
135+ if ( File . Exists ( bundlePath ) )
136+ File . Delete ( bundlePath ) ;
137+ if ( Directory . Exists ( bundleDir ) )
138+ Directory . Delete ( bundleDir , true ) ;
139+ }
140+ }
141+ }
0 commit comments