Skip to content

Commit 63b0a7a

Browse files
committed
test: add HtmxorComponentRequestHost tests
1 parent 28b37a1 commit 63b0a7a

19 files changed

+564
-511
lines changed

test/Htmxor.TestApp/Components/App.razor test/Htmxor.TestApp/App.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!DOCTYPE html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />

test/Htmxor.TestApp/Components/Layout/HtmxorLayout.razor

-5
This file was deleted.

test/Htmxor.TestApp/Components/Layout/MainLayout.razor

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
@page "/AllRouteParams/boolValue/{boolValue:bool?}"
2+
@page "/AllRouteParams/datetimeValue/{datetimeValue:datetime?}"
3+
@page "/AllRouteParams/decimalValue/{decimalValue:decimal?}"
4+
@page "/AllRouteParams/doubleValue/{doubleValue:double?}"
5+
@page "/AllRouteParams/floatValue/{floatValue:float?}"
6+
@page "/AllRouteParams/intValue/{intValue:int?}"
7+
@page "/AllRouteParams/longValue/{longValue:long?}"
8+
@page "/AllRouteParams/guidValue/{guidValue:guid?}"
9+
@page "/AllRouteParams/stringValue/{stringValue}"
10+
@using System.Globalization
11+
12+
@code {
13+
[Parameter]
14+
public bool? BoolValue { get; set; }
15+
16+
[Parameter]
17+
public DateTime? DatetimeValue { get; set; }
18+
19+
[Parameter]
20+
public decimal? DecimalValue { get; set; }
21+
22+
[Parameter]
23+
public double? DoubleValue { get; set; }
24+
25+
[Parameter]
26+
public float? FloatValue { get; set; }
27+
28+
[Parameter]
29+
public int? IntValue { get; set; }
30+
31+
[Parameter]
32+
public long? LongValue { get; set; }
33+
34+
[Parameter]
35+
public Guid? GuidValue { get; set; }
36+
37+
[Parameter]
38+
public string? StringValue { get; set; }
39+
}
40+
<div id="boolValue">@BoolValue</div>
41+
<div id="datetimeValue">@DatetimeValue?.ToString("O")</div>
42+
<div id="decimalValue">@DecimalValue?.ToString(CultureInfo.InvariantCulture)</div>
43+
<div id="doubleValue">@DoubleValue?.ToString(CultureInfo.InvariantCulture)</div>
44+
<div id="floatValue">@FloatValue?.ToString(CultureInfo.InvariantCulture)</div>
45+
<div id="intValue">@IntValue</div>
46+
<div id="longValue">@LongValue</div>
47+
<div id="guidValue">@GuidValue</div>
48+
<div id="stringValue">@StringValue</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@inherits HtmxorLayoutComponentBase
2+
<HeadOutlet />
3+
@Body
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@inherits LayoutComponentBase
2+
@Body

test/Htmxor.TestApp/Program.cs

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
1-
using Htmxor.TestApp.Components;
2-
31
var builder = WebApplication.CreateBuilder(args);
42

53
// Add services to the container.
6-
builder.Services.AddRazorComponents().AddHtmx(options =>
7-
{
8-
});
4+
builder.Services.AddRazorComponents().AddHtmx();
95
builder.Services.AddSingleton<TimeProvider>(TimeProvider.System);
106

117
var app = builder.Build();
128

13-
// Configure the HTTP request pipeline.
14-
if (!app.Environment.IsDevelopment())
15-
{
16-
app.UseExceptionHandler("/Error", createScopeForErrors: true);
17-
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
18-
app.UseHsts();
19-
}
20-
21-
app.UseHttpsRedirection();
229
app.UseStaticFiles();
2310
app.UseAntiforgery()
2411
.UseHtmxAntiforgery();
2512

26-
app.MapRazorComponents<App>().AddHtmxorComponentEndpoints(app);
13+
app.MapRazorComponents<Htmxor.TestApp.App>()
14+
.AddHtmxorComponentEndpoints(app);
2715

2816
app.Run();
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@using System.Net.Http
1+
@using System.Net.Http
22
@using System.Net.Http.Json
33
@using Microsoft.AspNetCore.Components.Forms
44
@using Microsoft.AspNetCore.Components.Routing
@@ -7,9 +7,9 @@
77
@using Microsoft.AspNetCore.Components.Web.Virtualization
88
@using Microsoft.JSInterop
99
@using Htmxor.TestApp
10+
@using Htmxor.TestApp.Layout
1011
@using Htmxor.TestApp.Components
1112
@using Htmxor.TestApp.Components.Pages.Examples
1213
@using Htmxor.Components
1314
@using Htmxor.Http
1415
@using Htmxor
15-
@using Htmxor.TestApp.Components.Layout

test/Htmxor.TestApp/wwwroot/app.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ h1:focus {
4141
tr.htmx-swapping td {
4242
opacity: 0;
4343
transition: opacity 1s ease-out;
44-
}
44+
}

test/Htmxor.TestApp/wwwroot/js/_hyperscript-0.9.7.js

-1
This file was deleted.

test/Htmxor.TestApp/wwwroot/js/htmx.min.js

-1
This file was deleted.

test/Htmxor.TestApp/wwwroot/js/rsjs-menu.js

-68
This file was deleted.

test/Htmxor.Tests/Htmxor.Tests.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Razor">
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
@@ -12,7 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="Meziantou.Xunit.ParallelTestFramework" Version="2.1.0" />
1414
<PackageReference Include="Alba" Version="7.4.1" />
15-
<PackageReference Include="bunit" Version="1.27.17" />
15+
<PackageReference Include="bunit" Version="1.28.9" />
1616
<PackageReference Include="FluentAssertions" Version="6.12.0" />
1717
<PackageReference Include="FluentAssertions.Analyzers" Version="0.31.0">
1818
<PrivateAssets>all</PrivateAssets>
@@ -22,9 +22,9 @@
2222
<PackageReference Include="Microsoft.Playwright" Version="1.43.0" />
2323
<PackageReference Include="Microsoft.Playwright.TestAdapter" Version="1.43.0" />
2424
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
25-
<PackageReference Include="xunit" Version="2.7.1" />
25+
<PackageReference Include="xunit" Version="2.8.0" />
2626
<PackageReference Include="SystemTextJson.JsonDiffPatch.Xunit" Version="2.0.0" />
27-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.8">
27+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
2828
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2929
<PrivateAssets>all</PrivateAssets>
3030
</PackageReference>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Globalization;
2+
using System.Net;
3+
4+
namespace Htmxor;
5+
6+
public class HtmxorComponentRequestHostTest : TestAppTestBase
7+
{
8+
public HtmxorComponentRequestHostTest(TestAppFixture fixture) : base(fixture)
9+
{
10+
}
11+
12+
[Theory]
13+
[InlineData(false, "boolValue", true)]
14+
[InlineData(false, "boolValue", false)]
15+
[InlineData(false, "datetimeValue", "2024-05-03T09:07:54.5004759+00:00")]
16+
[InlineData(false, "decimalValue", "-79228162514264337593543950335")]
17+
[InlineData(false, "decimalValue", "123.45")]
18+
[InlineData(false, "decimalValue", "79228162514264337593543950335")]
19+
[InlineData(false, "doubleValue", double.E)]
20+
[InlineData(false, "doubleValue", double.MinValue)]
21+
[InlineData(false, "doubleValue", double.MaxValue)]
22+
[InlineData(false, "floatValue", float.Epsilon)]
23+
[InlineData(false, "floatValue", float.MinValue)]
24+
[InlineData(false, "floatValue", float.MaxValue)]
25+
[InlineData(false, "intValue", 42)]
26+
[InlineData(false, "intValue", int.MinValue)]
27+
[InlineData(false, "intValue", int.MaxValue)]
28+
[InlineData(false, "longValue", 72036854775807)]
29+
[InlineData(false, "longValue", long.MinValue)]
30+
[InlineData(false, "longValue", long.MaxValue)]
31+
[InlineData(false, "guidValue", "e46fb987-6eac-4a1d-9744-ec68878a7a2e")]
32+
[InlineData(false, "guidValue", "00000000-0000-0000-0000-000000000000")]
33+
[InlineData(false, "stringValue", "foo-bar")]
34+
[InlineData(true, "boolValue", true)]
35+
[InlineData(true, "boolValue", false)]
36+
[InlineData(true, "datetimeValue", "2024-05-03T09:07:54.5004759+00:00")]
37+
[InlineData(true, "decimalValue", "-79228162514264337593543950335")]
38+
[InlineData(true, "decimalValue", "123.45")]
39+
[InlineData(true, "decimalValue", "79228162514264337593543950335")]
40+
[InlineData(true, "doubleValue", double.E)]
41+
[InlineData(true, "doubleValue", double.MinValue)]
42+
[InlineData(true, "doubleValue", double.MaxValue)]
43+
[InlineData(true, "floatValue", float.Epsilon)]
44+
[InlineData(true, "floatValue", float.MinValue)]
45+
[InlineData(true, "floatValue", float.MaxValue)]
46+
[InlineData(true, "intValue", 42)]
47+
[InlineData(true, "intValue", int.MinValue)]
48+
[InlineData(true, "intValue", int.MaxValue)]
49+
[InlineData(true, "longValue", 72036854775807)]
50+
[InlineData(true, "longValue", long.MinValue)]
51+
[InlineData(true, "longValue", long.MaxValue)]
52+
[InlineData(true, "guidValue", "e46fb987-6eac-4a1d-9744-ec68878a7a2e")]
53+
[InlineData(true, "guidValue", "00000000-0000-0000-0000-000000000000")]
54+
[InlineData(true, "stringValue", "foo-bar")]
55+
public async Task Route_values_in_component<T>(bool isHtmxRequest, string id, T value)
56+
{
57+
await Host.Scenario(s =>
58+
{
59+
var invarientValue = Convert.ToString(value, CultureInfo.InvariantCulture);
60+
s.Get.Url($"/AllRouteParams/{id}/{invarientValue}");
61+
s.WithHxHeaders(isHtmxRequest: isHtmxRequest);
62+
63+
s.StatusCodeShouldBe(HttpStatusCode.OK);
64+
s.ContentShouldHaveElementsEqualTo(
65+
$"#{id}",
66+
$"<div id='{id}'>{invarientValue}</div>");
67+
});
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System.Diagnostics.CodeAnalysis;
22

33
namespace Htmxor.TestAssets;
44

55
public static class AblaAssertionExtensions
66
{
7-
/// <summary>
8-
/// Assert that the HTTP response body is parsable as JSON and is semantically equivalent to
9-
/// the <paramref name="expectedJson"/> JSON string.
10-
/// </summary>
11-
/// <param name="expectedJson">The expected JSON string</param>
12-
public static Scenario ContentShouldBeHtml(this Scenario scenario, [StringSyntax("Html")] string expected)
13-
{
14-
return scenario.AssertThat(new SemanticHtmlContentBodyAssertion(expected));
15-
}
7+
/// <summary>
8+
/// Assert that the HTTP response body is parsable as Html and is semantically equivalent to
9+
/// the <paramref name="expectedJson"/> JSON string.
10+
/// </summary>
11+
/// <param name="expectedJson">The expected JSON string</param>
12+
public static Scenario ContentShouldBeHtml(this Scenario scenario, [StringSyntax("Html")] string expected)
13+
{
14+
return scenario.AssertThat(new SemanticHtmlContentBodyAssertion(null, expected));
15+
}
16+
17+
/// <summary>
18+
/// Assert that the HTTP response body that matches the <paramref name="cssSelector"/> is parsable as Html and is semantically equivalent to
19+
/// the <paramref name="expectedJson"/> JSON string.
20+
/// </summary>
21+
/// <param name="expectedJson">The expected JSON string</param>
22+
public static Scenario ContentShouldHaveElementsEqualTo(
23+
this Scenario scenario,
24+
string cssSelector,
25+
[StringSyntax("Html")] string expected)
26+
{
27+
return scenario.AssertThat(new SemanticHtmlContentBodyAssertion(cssSelector, expected));
28+
}
1629
}

0 commit comments

Comments
 (0)