Skip to content

Commit a40048a

Browse files
committed
Implement tests
1 parent e695ac3 commit a40048a

10 files changed

+331
-1
lines changed

InertiaCore.sln

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InertiaCore", "InertiaCore\InertiaCore.csproj", "{E7957EFC-8B8D-4E9B-8C6E-2C7542B3FB26}"
44
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InertiaCoreTests", "InertiaCoreTests\InertiaCoreTests.csproj", "{F9083DA4-AC82-478F-8191-694E3811307E}"
6+
EndProject
57
Global
68
GlobalSection(SolutionConfigurationPlatforms) = preSolution
79
Debug|Any CPU = Debug|Any CPU
@@ -12,5 +14,9 @@ Global
1214
{E7957EFC-8B8D-4E9B-8C6E-2C7542B3FB26}.Debug|Any CPU.Build.0 = Debug|Any CPU
1315
{E7957EFC-8B8D-4E9B-8C6E-2C7542B3FB26}.Release|Any CPU.ActiveCfg = Release|Any CPU
1416
{E7957EFC-8B8D-4E9B-8C6E-2C7542B3FB26}.Release|Any CPU.Build.0 = Release|Any CPU
17+
{F9083DA4-AC82-478F-8191-694E3811307E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{F9083DA4-AC82-478F-8191-694E3811307E}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{F9083DA4-AC82-478F-8191-694E3811307E}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{F9083DA4-AC82-478F-8191-694E3811307E}.Release|Any CPU.Build.0 = Release|Any CPU
1521
EndGlobalSection
1622
EndGlobal

InertiaCore/Inertia.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Microsoft.AspNetCore.Html;
1+
using System.Runtime.CompilerServices;
2+
using Microsoft.AspNetCore.Html;
23

4+
[assembly: InternalsVisibleTo("InertiaCoreTests")]
35
namespace InertiaCore;
46

57
public static class Inertia
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
13+
<PackageReference Include="Moq" Version="4.18.4" />
14+
<PackageReference Include="NUnit" Version="3.13.3" />
15+
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
16+
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
17+
<PackageReference Include="coverlet.collector" Version="3.1.2" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\InertiaCore\InertiaCore.csproj" />
22+
</ItemGroup>
23+
24+
</Project>

InertiaCoreTests/Setup.cs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using InertiaCore;
2+
using InertiaCore.Extensions;
3+
using InertiaCore.Ssr;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Http.Features;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.AspNetCore.Mvc.Abstractions;
8+
using Microsoft.AspNetCore.Routing;
9+
using Microsoft.Extensions.Options;
10+
using Moq;
11+
12+
namespace InertiaCoreTests;
13+
14+
public partial class Tests
15+
{
16+
private IResponseFactory _factory = null!;
17+
18+
/// <summary>
19+
/// Setups Inertia response factory for the tests.
20+
/// </summary>
21+
[SetUp]
22+
public void Setup()
23+
{
24+
var contextAccessor = new Mock<IHttpContextAccessor>();
25+
var httpClientFactory = new Mock<IHttpClientFactory>();
26+
27+
var gateway = new Gateway(httpClientFactory.Object);
28+
var options = new Mock<IOptions<InertiaOptions>>();
29+
options.SetupGet(x => x.Value).Returns(new InertiaOptions());
30+
31+
_factory = new ResponseFactory(contextAccessor.Object, gateway, options.Object);
32+
}
33+
34+
/// <summary>
35+
/// Prepares ActionContext for usage in tests.
36+
/// </summary>
37+
/// <param name="headers">Optional request headers.</param>
38+
/// <param name="sharedData">Optional Inertia shared data.</param>
39+
/// <param name="modelState">Optional validation errors dictionary.</param>
40+
private static ActionContext PrepareContext(HeaderDictionary? headers = null, InertiaSharedData? sharedData = null,
41+
Dictionary<string, string>? modelState = null)
42+
{
43+
var request = new Mock<HttpRequest>();
44+
request.SetupGet(r => r.Headers).Returns(headers ?? new HeaderDictionary());
45+
46+
var response = new Mock<HttpResponse>();
47+
response.SetupGet(r => r.Headers).Returns(new HeaderDictionary());
48+
49+
var features = new FeatureCollection();
50+
if (sharedData != null)
51+
features.Set(sharedData);
52+
53+
var httpContext = new Mock<HttpContext>();
54+
httpContext.SetupGet(c => c.Request).Returns(request.Object);
55+
httpContext.SetupGet(c => c.Response).Returns(response.Object);
56+
httpContext.SetupGet(c => c.Features).Returns(features);
57+
58+
var context = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
59+
60+
if (modelState == null) return context;
61+
62+
foreach (var (key, value) in modelState)
63+
{
64+
context.ModelState.AddModelError(key, value);
65+
}
66+
67+
return context;
68+
}
69+
}

InertiaCoreTests/UnitTestHtml.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace InertiaCoreTests;
2+
3+
public partial class Tests
4+
{
5+
/// <summary>
6+
/// Tests if the generated HTML contains valid page data.
7+
/// </summary>
8+
[Test]
9+
public async Task TestHtml()
10+
{
11+
var html = await _factory.Html(new { Test = "Test" });
12+
13+
Assert.That(html.ToString(),
14+
Is.EqualTo("<div id=\"app\" data-page=\"{&quot;test&quot;:&quot;Test&quot;}\"></div>"));
15+
}
16+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using InertiaCore;
2+
using Microsoft.AspNetCore.Http;
3+
4+
namespace InertiaCoreTests;
5+
6+
public partial class Tests
7+
{
8+
/// <summary>
9+
/// Tests if the model state dictionary is passed to props correctly.
10+
/// </summary>
11+
[Test]
12+
public void TestModelState()
13+
{
14+
var response = _factory.Render("Test/Page", new
15+
{
16+
Test = "Test"
17+
});
18+
19+
var headers = new HeaderDictionary
20+
{
21+
{ "X-Inertia", "true" },
22+
};
23+
24+
var context = PrepareContext(headers, null, new Dictionary<string, string>
25+
{
26+
{ "Field", "Error" }
27+
});
28+
29+
response.SetContext(context);
30+
response.ProcessResponse();
31+
32+
var page = response.GetJson().Value as Page;
33+
34+
Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
35+
{
36+
{ "test", "Test" },
37+
{
38+
"errors", new Dictionary<string, object>
39+
{
40+
{ "field", "Error" }
41+
}
42+
}
43+
}));
44+
}
45+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using InertiaCore;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace InertiaCoreTests;
6+
7+
public partial class Tests
8+
{
9+
/// <summary>
10+
/// Tests if props contain only specified partial data.
11+
/// </summary>
12+
[Test]
13+
public void TestPartialData()
14+
{
15+
var response = _factory.Render("Test/Page", new
16+
{
17+
Test = "Test",
18+
TestPartial = "Partial"
19+
});
20+
21+
var headers = new HeaderDictionary
22+
{
23+
{ "X-Inertia", "true" },
24+
{ "X-Inertia-Partial-Data", "testPartial" },
25+
{ "X-Inertia-Partial-Component", "Test/Page" }
26+
};
27+
28+
var context = PrepareContext(headers);
29+
30+
response.SetContext(context);
31+
response.ProcessResponse();
32+
33+
var page = response.GetJson().Value as Page;
34+
35+
Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
36+
{
37+
{ "testPartial", "Partial" },
38+
{ "errors", new Dictionary<string, object?>(0) }
39+
}));
40+
}
41+
}

InertiaCoreTests/UnitTestResult.cs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using InertiaCore;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace InertiaCoreTests;
6+
7+
public partial class Tests
8+
{
9+
/// <summary>
10+
/// Tests if the JSON result is created correctly.
11+
/// </summary>
12+
[Test]
13+
[Order(1)]
14+
public void TestJsonResult()
15+
{
16+
var response = _factory.Render("Test/Page", new
17+
{
18+
Test = "Test"
19+
});
20+
21+
var headers = new HeaderDictionary
22+
{
23+
{ "X-Inertia", "true" },
24+
};
25+
26+
var context = PrepareContext(headers);
27+
28+
response.SetContext(context);
29+
response.ProcessResponse();
30+
31+
var result = response.GetResult();
32+
33+
Assert.Multiple(() =>
34+
{
35+
Assert.That(result, Is.InstanceOf(typeof(JsonResult)));
36+
37+
var json = (result as JsonResult)?.Value;
38+
Assert.That(json, Is.InstanceOf(typeof(Page)));
39+
40+
Assert.That((json as Page)?.Component, Is.EqualTo("Test/Page"));
41+
Assert.That((json as Page)?.Props, Is.EqualTo(new Dictionary<string, object?>
42+
{
43+
{ "test", "Test" },
44+
{ "errors", new Dictionary<string, object?>(0) }
45+
}));
46+
});
47+
}
48+
49+
/// <summary>
50+
/// Tests if the view result is created correctly.
51+
/// </summary>
52+
[Test]
53+
[Order(2)]
54+
public void TestViewResult()
55+
{
56+
var response = _factory.Render("Test/Page", new
57+
{
58+
Test = "Test"
59+
});
60+
61+
var context = PrepareContext();
62+
63+
response.SetContext(context);
64+
response.ProcessResponse();
65+
66+
var result = response.GetResult();
67+
68+
Assert.Multiple(() =>
69+
{
70+
Assert.That(result, Is.InstanceOf(typeof(ViewResult)));
71+
Assert.That((result as ViewResult)?.ViewName, Is.EqualTo("~/Views/App.cshtml"));
72+
73+
var model = (result as ViewResult)?.Model;
74+
Assert.That(model, Is.InstanceOf(typeof(Page)));
75+
76+
Assert.That((model as Page)?.Component, Is.EqualTo("Test/Page"));
77+
Assert.That((model as Page)?.Props, Is.EqualTo(new Dictionary<string, object?>
78+
{
79+
{ "test", "Test" },
80+
{ "errors", new Dictionary<string, object?>(0) }
81+
}));
82+
});
83+
}
84+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using InertiaCore;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace InertiaCoreTests;
6+
7+
public partial class Tests
8+
{
9+
/// <summary>
10+
/// Test if shared data is merged with the props properly.
11+
/// </summary>
12+
[Test]
13+
public void TestSharedData()
14+
{
15+
var response = _factory.Render("Test/Page", new
16+
{
17+
Test = "Test"
18+
});
19+
20+
var headers = new HeaderDictionary
21+
{
22+
{ "X-Inertia", "true" },
23+
};
24+
25+
var sharedData = new InertiaSharedData();
26+
sharedData.Set("TestShared", "Shared");
27+
28+
var context = PrepareContext(headers, sharedData);
29+
30+
response.SetContext(context);
31+
response.ProcessResponse();
32+
33+
var page = response.GetJson().Value as Page;
34+
35+
Assert.That(page?.Props, Is.EqualTo(new Dictionary<string, object?>
36+
{
37+
{ "test", "Test" },
38+
{ "testShared", "Shared" },
39+
{ "errors", new Dictionary<string, object?>(0) }
40+
}));
41+
}
42+
}

InertiaCoreTests/Usings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using NUnit.Framework;

0 commit comments

Comments
 (0)