Skip to content

Commit 3cb861d

Browse files
committed
Add tag helper model binding integration tests for text input
1 parent 419668c commit 3cb861d

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

tests/GovUk.Frontend.AspNetCore.IntegrationTests/GovUk.Frontend.AspNetCore.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
1414
<PackageReference Include="Microsoft.Playwright" Version="1.21.0" />
15+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.23" />
1516
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
1617
<PrivateAssets>all</PrivateAssets>
1718
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace GovUk.Frontend.AspNetCore.IntegrationTests.TagHelperModelBindingTests;
2+
3+
public class TagHelperModelBindingTestsFixture : ServerFixture
4+
{
5+
protected override void Configure(IApplicationBuilder app)
6+
{
7+
base.Configure(app);
8+
9+
app.UseEndpoints(endpoints => endpoints.MapControllers());
10+
}
11+
12+
protected override void ConfigureServices(IServiceCollection services)
13+
{
14+
services.AddGovUkFrontend();
15+
16+
services
17+
.AddMvc()
18+
.AddRazorOptions(options =>
19+
{
20+
options.ViewLocationFormats.Add("/TagHelperModelBindingTests/{0}.cshtml");
21+
});
22+
}
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace GovUk.Frontend.AspNetCore.IntegrationTests.TagHelperModelBindingTests;
5+
6+
public class Tests(TagHelperModelBindingTestsFixture fixture) : IClassFixture<TagHelperModelBindingTestsFixture>
7+
{
8+
[Fact]
9+
public async Task TextInput_RendersCorrectly()
10+
{
11+
var page = await fixture.Browser!.NewPageAsync();
12+
await page.GotoAsync($"{ServerFixture.BaseUrl}/ModelBindingTests/TextInput");
13+
await page.PauseAsync();
14+
15+
var input = page.Locator("input").First;
16+
Assert.Equal("Text", await input.GetAttributeAsync("name"));
17+
Assert.Equal("Text", await input.GetAttributeAsync("id"));
18+
Assert.Equal("Model value", await input.GetAttributeAsync("value"));
19+
20+
var label = page.Locator("label").First;
21+
Assert.Equal("Text", await label.GetAttributeAsync("for"));
22+
Assert.Equal("ModelMetadata display name", await label.InnerTextAsync());
23+
24+
var hint = page.Locator(".govuk-hint").First;
25+
Assert.Equal("ModelMetadata description", await hint.InnerTextAsync());
26+
27+
var errorMessage = page.Locator(".govuk-error-message").First;
28+
Assert.Equal("Error: Model error message", await errorMessage.TextContentAsync());
29+
}
30+
}
31+
32+
[Route("/[controller]/[action]")]
33+
public class ModelBindingTestsController : Controller
34+
{
35+
[HttpGet]
36+
public IActionResult TextInput()
37+
{
38+
ModelState.AddModelError(nameof(TextInputTestsModel.Text), "Model error message");
39+
return View("TextInput", new TextInputTestsModel { Text = "Model value" });
40+
}
41+
}
42+
43+
public record TextInputTestsModel
44+
{
45+
[Display(Name = "ModelMetadata display name", Description = "ModelMetadata description")]
46+
public string? Text { get; set; }
47+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@model GovUk.Frontend.AspNetCore.IntegrationTests.TagHelperModelBindingTests.TextInputTestsModel
2+
3+
<govuk-input for="Text" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@addTagHelper *, GovUk.Frontend.AspNetCore
2+
@addTagHelper *, GovUk.Frontend.AspNetCore.IntegrationTests
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "_GovUkPageTemplate";
3+
}

0 commit comments

Comments
 (0)