-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathUnitTestComponentValidation.cs
More file actions
131 lines (110 loc) · 5.19 KB
/
UnitTestComponentValidation.cs
File metadata and controls
131 lines (110 loc) · 5.19 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
using InertiaCore;
using InertiaCore.Models;
using InertiaCore.Ssr;
using InertiaCore.Utils;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Moq;
namespace InertiaCoreTests;
public partial class Tests
{
[Test]
[Description("Test component exists validation is disabled by default")]
public void TestComponentValidationDisabledByDefault()
{
var contextAccessor = new Mock<IHttpContextAccessor>();
var httpClientFactory = new Mock<IHttpClientFactory>();
var environment = new Mock<IWebHostEnvironment>();
environment.SetupGet(x => x.ContentRootPath).Returns(Path.GetTempPath());
var gateway = new Gateway(httpClientFactory.Object);
var options = new Mock<IOptions<InertiaOptions>>();
options.SetupGet(x => x.Value).Returns(new InertiaOptions { EnsurePagesExist = false });
var factory = new ResponseFactory(contextAccessor.Object, gateway, options.Object, environment.Object);
Assert.DoesNotThrow(() => factory.Render("NonexistentComponent"));
}
[Test]
[Description("Test component validation throws exception when enabled and component doesn't exist")]
public void TestComponentValidationThrowsWhenComponentMissing()
{
var contextAccessor = new Mock<IHttpContextAccessor>();
var httpClientFactory = new Mock<IHttpClientFactory>();
var environment = new Mock<IWebHostEnvironment>();
environment.SetupGet(x => x.ContentRootPath).Returns(Path.GetTempPath());
var gateway = new Gateway(httpClientFactory.Object);
var options = new Mock<IOptions<InertiaOptions>>();
options.SetupGet(x => x.Value).Returns(new InertiaOptions { EnsurePagesExist = true });
var factory = new ResponseFactory(contextAccessor.Object, gateway, options.Object, environment.Object);
var ex = Assert.Throws<ComponentNotFoundException>(() => factory.Render("NonexistentComponent"));
Assert.That(ex.Component, Is.EqualTo("NonexistentComponent"));
Assert.That(ex.Message, Contains.Substring("NonexistentComponent"));
}
[Test]
[Description("Test component validation passes when component exists")]
public void TestComponentValidationPassesWhenComponentExists()
{
var tempDir = Path.GetTempPath();
var pagesDir = Path.Combine(tempDir, "src", "Pages");
Directory.CreateDirectory(pagesDir);
var testComponent = Path.Combine(pagesDir, "TestComponent.tsx");
File.WriteAllText(testComponent, "export default function TestComponent() { return <div>Test</div>; }");
try
{
var contextAccessor = new Mock<IHttpContextAccessor>();
var httpClientFactory = new Mock<IHttpClientFactory>();
var environment = new Mock<IWebHostEnvironment>();
environment.SetupGet(x => x.ContentRootPath).Returns(tempDir);
var gateway = new Gateway(httpClientFactory.Object);
var options = new Mock<IOptions<InertiaOptions>>();
options.SetupGet(x => x.Value).Returns(new InertiaOptions
{
EnsurePagesExist = true,
PagePaths = new[] { "~/src/Pages" },
PageExtensions = new[] { ".tsx" }
});
var factory = new ResponseFactory(contextAccessor.Object, gateway, options.Object, environment.Object);
Assert.DoesNotThrow(() => factory.Render("TestComponent"));
}
finally
{
if (File.Exists(testComponent))
File.Delete(testComponent);
if (Directory.Exists(pagesDir))
Directory.Delete(pagesDir, true);
}
}
[Test]
[Description("Test component validation works with nested paths")]
public void TestComponentValidationWithNestedPaths()
{
var tempDir = Path.GetTempPath();
var pagesDir = Path.Combine(tempDir, "ClientApp", "src", "Pages", "Auth");
Directory.CreateDirectory(pagesDir);
var testComponent = Path.Combine(pagesDir, "Login.vue");
File.WriteAllText(testComponent, "<template><div>Login</div></template>");
try
{
var contextAccessor = new Mock<IHttpContextAccessor>();
var httpClientFactory = new Mock<IHttpClientFactory>();
var environment = new Mock<IWebHostEnvironment>();
environment.SetupGet(x => x.ContentRootPath).Returns(tempDir);
var gateway = new Gateway(httpClientFactory.Object);
var options = new Mock<IOptions<InertiaOptions>>();
options.SetupGet(x => x.Value).Returns(new InertiaOptions
{
EnsurePagesExist = true,
PagePaths = new[] { "~/ClientApp/src/Pages" },
PageExtensions = new[] { ".vue" }
});
var factory = new ResponseFactory(contextAccessor.Object, gateway, options.Object, environment.Object);
Assert.DoesNotThrow(() => factory.Render("Auth/Login"));
}
finally
{
if (File.Exists(testComponent))
File.Delete(testComponent);
if (Directory.Exists(pagesDir))
Directory.Delete(pagesDir, true);
}
}
}