-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathUnitTestVite.cs
More file actions
255 lines (207 loc) · 9.86 KB
/
UnitTestVite.cs
File metadata and controls
255 lines (207 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System.IO.Abstractions.TestingHelpers;
using InertiaCore.Extensions;
using InertiaCore.Models;
using InertiaCore.Utils;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Moq;
namespace InertiaCoreTests;
public partial class Tests
{
[Test]
[Description("Test if the Vite configuration registers properly the service.")]
public void TestViteConfiguration()
{
var builder = WebApplication.CreateBuilder();
builder.Services.AddInertia();
Assert.Multiple(() =>
{
Assert.DoesNotThrow(() => builder.Services.AddViteHelper());
Assert.That(builder.Services.Any(s => s.ServiceType == typeof(IViteBuilder)), Is.True);
});
var app = builder.Build();
Assert.DoesNotThrow(() => app.UseInertia());
Assert.DoesNotThrow(() => Vite.ReactRefresh());
}
[Test]
[Description("Test if the Vite Helper handles hot module reloading properly.")]
public void TestHot()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ @"/wwwroot/hot", new MockFileData("http://127.0.0.1:5174") },
});
var options = new Mock<IOptions<ViteOptions>>();
options.SetupGet(x => x.Value).Returns(new ViteOptions());
var mock = new Mock<ViteBuilder>(options.Object);
mock.Object.UseFileSystem(fileSystem);
var htmlResult = mock.Object.ReactRefresh();
const string inner =
"<script type=\"module\">import RefreshRuntime from 'http://127.0.0.1:5174/@react-refresh';" +
"RefreshRuntime.injectIntoGlobalHook(window);" +
"window.$RefreshReg$ = () => { };" +
"window.$RefreshSig$ = () => (type) => type;" +
"window.__vite_plugin_react_preamble_installed__ = true;</script>";
Assert.That(htmlResult.ToString(), Is.EqualTo(inner));
}
[Test]
[Description("Test if the Vite Helper handles HMR disabled properly.")]
public void TestNotHot()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
var options = new Mock<IOptions<ViteOptions>>();
options.SetupGet(x => x.Value).Returns(new ViteOptions());
var mock = new Mock<ViteBuilder>(options.Object);
mock.Object.UseFileSystem(fileSystem);
Assert.That(mock.Object.ReactRefresh().ToString(), Is.EqualTo("<!-- no hot -->"));
}
[Test]
[Description(
"Test if the Vite Helper handles generating HTML tags for both JS and CSS from HMR and the manifest properly.")]
public void TestViteInput()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
var options = new Mock<IOptions<ViteOptions>>();
options.SetupGet(x => x.Value).Returns(new ViteOptions());
var mock = new Mock<ViteBuilder>(options.Object);
mock.Object.UseFileSystem(fileSystem);
// Missing manifest exception
Assert.Throws<Exception>(() => mock.Object.Input("app.tsx"));
fileSystem.AddFile(@"/wwwroot/build/manifest.json", new MockFileData("null"));
// Null manifest exception
Assert.Throws<Exception>(() => mock.Object.Input("app.tsx"));
// Missing info exception
fileSystem.AddFile(@"/wwwroot/build/manifest.json", new MockFileData("{\"main.tsx\": {}}"));
Assert.Throws<Exception>(() => mock.Object.Input("app.tsx"));
// Basic JS File
fileSystem.AddFile(@"/wwwroot/build/manifest.json",
new MockFileData("{\"app.tsx\": {\"file\": \"assets/main-19038c6a.js\"}}"));
var result = mock.Object.Input("app.tsx");
Assert.That(result.ToString(),
Is.EqualTo("<script src=\"/build/assets/main-19038c6a.js\" type=\"module\"></script>\n\t"));
// Basic JS File with CSS import
fileSystem.AddFile(@"/wwwroot/build/manifest.json",
new MockFileData("{\"app.tsx\": {\"file\": \"assets/main.js\",\"css\": [\"assets/index.css\"]}}"));
result = mock.Object.Input("app.tsx");
Assert.That(result.ToString(),
Is.EqualTo(
"<script src=\"/build/assets/main.js\" type=\"module\"></script>\n\t<link href=\"/build/assets/index.css\" rel=\"stylesheet\" />\n\t"));
// Basic CSS file
fileSystem.AddFile(@"/wwwroot/build/manifest.json",
new MockFileData("{\"index.scss\": {\"file\": \"assets/index.css\"}}"));
result = mock.Object.Input("index.scss");
Assert.That(result.ToString(), Is.EqualTo("<link href=\"/build/assets/index.css\" rel=\"stylesheet\" />\n\t"));
// Basic CSS file with custom builder dir
fileSystem.AddFile(@"/wwwroot/manifest.json",
new MockFileData("{\"index.scss\": {\"file\": \"assets/index.css\"}}"));
options.SetupGet(x => x.Value).Returns(new ViteOptions
{
BuildDirectory = null
});
result = mock.Object.Input("index.scss");
Assert.That(result.ToString(), Is.EqualTo("<link href=\"/assets/index.css\" rel=\"stylesheet\" />\n\t"));
// Hot file with css import
options.SetupGet(x => x.Value).Returns(new ViteOptions
{
BuildDirectory = "build"
});
fileSystem.AddFile(@"/wwwroot/hot", new MockFileData("http://127.0.0.1:5174"));
result = mock.Object.Input("index.scss");
Assert.That(result.ToString(), Is.EqualTo(
"<script src=\"http://127.0.0.1:5174/@vite/client\" type=\"module\"></script>\n\t" +
"<script src=\"http://127.0.0.1:5174/index.scss\" type=\"module\"></script>\n\t"));
// Test null build directory
fileSystem.AddFile(@"/wwwroot/hot", new MockFileData("http://127.0.0.1:5174"));
options.SetupGet(x => x.Value).Returns(new ViteOptions
{
BuildDirectory = null
});
result = mock.Object.Input("index.scss");
Assert.That(result.ToString(), Is.EqualTo(
"<script src=\"http://127.0.0.1:5174/@vite/client\" type=\"module\"></script>\n\t" +
"<script src=\"http://127.0.0.1:5174/index.scss\" type=\"module\"></script>\n\t"));
// Test empty build directory
options.SetupGet(x => x.Value).Returns(new ViteOptions
{
BuildDirectory = ""
});
result = mock.Object.Input("index.scss");
Assert.That(result.ToString(), Is.EqualTo(
"<script src=\"http://127.0.0.1:5174/@vite/client\" type=\"module\"></script>\n\t" +
"<script src=\"http://127.0.0.1:5174/index.scss\" type=\"module\"></script>\n\t"));
// Basic JS File via hot
result = mock.Object.Input("app.tsx");
Assert.That(result.ToString(), Is.EqualTo(
"<script src=\"http://127.0.0.1:5174/@vite/client\" type=\"module\"></script>\n\t" +
"<script src=\"http://127.0.0.1:5174/app.tsx\" type=\"module\"></script>\n\t"));
}
[Test]
[Description("Test if the Vite Facade behaves correctly with different builder configurations.")]
public void TestViteFacade()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
var options = new Mock<IOptions<ViteOptions>>();
options.SetupGet(x => x.Value).Returns(new ViteOptions
{
BuildDirectory = "build2",
ManifestFilename = "manifest-test.json",
HotFile = "cold",
PublicDirectory = "public"
});
var mock = new Mock<ViteBuilder>(options.Object);
mock.Object.UseFileSystem(fileSystem);
Vite.UseBuilder(mock.Object);
// Basic Manifest with CSS file
fileSystem.AddFile(@"/public/build2/manifest-test.json",
new MockFileData("{\"index.scss\": {\"file\": \"assets/index.css\"}}"));
var result = Vite.Input("index.scss");
Assert.That(result.ToString(), Is.EqualTo("<link href=\"/build2/assets/index.css\" rel=\"stylesheet\" />\n\t"));
// Hot file with css import
options.SetupGet(x => x.Value).Returns(new ViteOptions
{
BuildDirectory = null,
ManifestFilename = "manifest-test.json",
HotFile = "cold",
PublicDirectory = "public"
});
fileSystem.AddFile(@"/public/cold", new MockFileData("http://127.0.0.1:5174"));
result = Vite.Input("index.scss");
Assert.That(result.ToString(), Is.EqualTo(
"<script src=\"http://127.0.0.1:5174/@vite/client\" type=\"module\"></script>\n\t" +
"<script src=\"http://127.0.0.1:5174/index.scss\" type=\"module\"></script>\n\t"));
}
[Test]
[Description("Test if the vite version is read properly.")]
public async Task TestViteVersion()
{
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>());
var options = new Mock<IOptions<ViteOptions>>();
options.SetupGet(x => x.Value).Returns(new ViteOptions());
var mock = new Mock<ViteBuilder>(options.Object);
mock.Object.UseFileSystem(fileSystem);
Vite.UseBuilder(mock.Object);
fileSystem.AddFile(@"/wwwroot/build/manifest.json",
new MockFileData("{\"app.tsx\": {\"file\": \"assets/main-19038c6a.js\"}}"));
_factory.Version(Vite.GetManifestHash);
var response = _factory.Render("Test/Page", new
{
Test = "Test"
});
var headers = new HeaderDictionary
{
{ "X-Inertia", "true" }
};
var context = PrepareContext(headers);
response.SetContext(context);
await response.ProcessResponse();
var result = response.GetResult();
Assert.Multiple(() =>
{
Assert.That(result, Is.InstanceOf<JsonResult>());
var json = (result as JsonResult)?.Value;
Assert.That((json as Page)?.Version, Is.EqualTo("24a5fa185351b781e5eb6f813c433180"));
});
}
}