Skip to content

Commit f2b2235

Browse files
committed
Add LocalTimeFormWithDefault component and corresponding test for date-time input handling
1 parent 3ff117e commit f2b2235

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<LocalTimeForm Value="Dt" ValueChanged="DtChanged" Context="dtf">
2+
<InputDate Type="InputDateType.DateTimeLocal" @bind-Value="dtf.ValueOrNow" />
3+
<InputDate Type="InputDateType.Date" @bind-Value="dtf.DateOrToday" />
4+
<InputDate Type="InputDateType.Time" @bind-Value="dtf.TimeOrNow" />
5+
</LocalTimeForm>
6+
7+
@code {
8+
[Parameter]
9+
public DateTime? Dt { get; set; }
10+
11+
[Parameter]
12+
public EventCallback<DateTime?> DtChanged { get; set; }
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
@using BlazorLocalTimeTest.Component
2+
@using Microsoft.Extensions.DependencyInjection.Extensions
3+
@using Shouldly
4+
@inherits TestContext
5+
@code
6+
{
7+
[Fact]
8+
public void TestFormUpdated1()
9+
{
10+
// add a mock time provider to control the current time
11+
var mockTimeProvider = new MockTimeProvider(new(2020, 1, 1, 12, 34, 56, TimeSpan.Zero));
12+
Services.AddScoped<ILocalTimeService, LocalTimeMockService>(); // Asia/Tokyo (UTC+9)
13+
Services.TryAddSingleton<TimeProvider>(mockTimeProvider);
14+
15+
DateTime? dt = null;
16+
var cut = RenderComponent<LocalTimeFormWithDefault>(parameters =>
17+
parameters.Bind(p => p.Dt, dt, n => dt = n));
18+
var input_dt = cut.Find("input[type='datetime-local']");
19+
var input_date = cut.Find("input[type='date']");
20+
var input_time = cut.Find("input[type='time']");
21+
// value is set to the local time in the input
22+
input_dt.GetAttribute("value").ShouldBe("2020-01-01T21:34:56");
23+
input_date.GetAttribute("value").ShouldBe("2020-01-01");
24+
input_time.GetAttribute("value").ShouldBe("21:34:56");
25+
26+
// call update
27+
input_dt.Change("2020-10-01T12:00:00");
28+
dt.ShouldBe(new DateTime(2020, 10, 1, 3, 00, 0, DateTimeKind.Utc));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)