From bb33e649801842a6491ded1ff05aaaedc72c6c29 Mon Sep 17 00:00:00 2001 From: Ben Sampica Date: Thu, 7 Jul 2022 22:40:44 -0500 Subject: [PATCH 1/5] Add bid countdown --- src/Client/DynamoLeagueBlazor.Client.csproj | 1 + .../Features/FreeAgents/BidCountdown.razor | 3 ++ .../Features/FreeAgents/BidCountdown.razor.cs | 42 +++++++++++++++++++ src/Client/Features/FreeAgents/Detail.razor | 6 +-- src/Server/Features/FreeAgents/Detail.cs | 2 +- src/Shared/Features/FreeAgents/Detail.cs | 2 +- src/Tests/Features/FreeAgents/DetailTests.cs | 2 +- 7 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/Client/Features/FreeAgents/BidCountdown.razor create mode 100644 src/Client/Features/FreeAgents/BidCountdown.razor.cs diff --git a/src/Client/DynamoLeagueBlazor.Client.csproj b/src/Client/DynamoLeagueBlazor.Client.csproj index 83959c3..fce0355 100644 --- a/src/Client/DynamoLeagueBlazor.Client.csproj +++ b/src/Client/DynamoLeagueBlazor.Client.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Client/Features/FreeAgents/BidCountdown.razor b/src/Client/Features/FreeAgents/BidCountdown.razor new file mode 100644 index 0000000..9f3a727 --- /dev/null +++ b/src/Client/Features/FreeAgents/BidCountdown.razor @@ -0,0 +1,3 @@ + + @_remainingTime + diff --git a/src/Client/Features/FreeAgents/BidCountdown.razor.cs b/src/Client/Features/FreeAgents/BidCountdown.razor.cs new file mode 100644 index 0000000..afd8d53 --- /dev/null +++ b/src/Client/Features/FreeAgents/BidCountdown.razor.cs @@ -0,0 +1,42 @@ +using Humanizer; +using Humanizer.Localisation; +using Microsoft.AspNetCore.Components; +using MudBlazor; +using System.Timers; +using Timer = System.Timers.Timer; + +namespace DynamoLeagueBlazor.Client.Features.FreeAgents; + +public partial class BidCountdown +{ + [Parameter, EditorRequired] public DateTime DateTime { get; set; } + + private Timer _timer = new(1000); + private string _remainingTime = string.Empty; + private Color _color = Color.Warning; + + protected override void OnInitialized() + { + _timer.Elapsed += CountDown; + _timer.Enabled = true; + } + + private void CountDown(object? source, ElapsedEventArgs e) + { + var remainingTime = (DateTime - DateTime.Now); + + if (remainingTime <= TimeSpan.FromDays(1)) + { + _color = Color.Error; + + if (remainingTime == TimeSpan.Zero) + { + _timer.Enabled = false; + } + } + + _remainingTime = remainingTime.Humanize(4, maxUnit: TimeUnit.Day, minUnit: TimeUnit.Second); + + StateHasChanged(); + } +} diff --git a/src/Client/Features/FreeAgents/Detail.razor b/src/Client/Features/FreeAgents/Detail.razor index fd84514..e464f95 100644 --- a/src/Client/Features/FreeAgents/Detail.razor +++ b/src/Client/Features/FreeAgents/Detail.razor @@ -35,11 +35,9 @@ else @_result.Position - Bidding Ends on - - - @_result.EndOfFreeAgency + Bidding Ends In + diff --git a/src/Server/Features/FreeAgents/Detail.cs b/src/Server/Features/FreeAgents/Detail.cs index 35c5fdd..ce622f2 100644 --- a/src/Server/Features/FreeAgents/Detail.cs +++ b/src/Server/Features/FreeAgents/Detail.cs @@ -59,7 +59,7 @@ public class DetailMappingProfile : Profile public DetailMappingProfile() { CreateMap() - .ForMember(d => d.EndOfFreeAgency, mo => mo.MapFrom(s => s.EndOfFreeAgency!.Value.ToShortDateString())) + .ForMember(d => d.EndOfFreeAgency, mo => mo.MapFrom(s => s.EndOfFreeAgency!.Value)) .ForMember(d => d.Team, mo => mo.MapFrom(s => s.Team.Name)) .ForMember(d => d.Bids, mo => mo.MapFrom(s => s.Bids.OrderByDescending(b => b.CreatedOn))); CreateMap() diff --git a/src/Shared/Features/FreeAgents/Detail.cs b/src/Shared/Features/FreeAgents/Detail.cs index 7635e33..8fa983d 100644 --- a/src/Shared/Features/FreeAgents/Detail.cs +++ b/src/Shared/Features/FreeAgents/Detail.cs @@ -6,7 +6,7 @@ public class FreeAgentDetailResult public string Position { get; set; } = null!; public string HeadShotUrl { get; set; } = null!; public string Team { get; set; } = null!; - public string EndOfFreeAgency { get; set; } = null!; + public DateTime EndOfFreeAgency { get; set; } public IEnumerable Bids { get; set; } = Enumerable.Empty(); diff --git a/src/Tests/Features/FreeAgents/DetailTests.cs b/src/Tests/Features/FreeAgents/DetailTests.cs index 185a065..f9ba51a 100644 --- a/src/Tests/Features/FreeAgents/DetailTests.cs +++ b/src/Tests/Features/FreeAgents/DetailTests.cs @@ -49,7 +49,7 @@ public async Task GivenAnyAuthenticatedUser_WhenGivenValidPlayerId_ThenReturnsEx response.Position.Should().Be(mockFreeAgent.Position); response.Team.Should().Be(mockTeam.Name); response.HeadShotUrl.Should().Be(mockFreeAgent.HeadShotUrl); - response.EndOfFreeAgency.Should().Be(mockFreeAgent.EndOfFreeAgency!.Value.ToShortDateString()); + response.EndOfFreeAgency.Should().Be(mockFreeAgent.EndOfFreeAgency!.Value); response!.Bids.Should().HaveCount(1); var bid = response.Bids.First(); From 1c9896bfd77d3a2a9bd7df6d2d1676220e8c740f Mon Sep 17 00:00:00 2001 From: Ben Sampica Date: Thu, 7 Jul 2022 22:52:35 -0500 Subject: [PATCH 2/5] add tests --- .../Features/FreeAgents/BidCountdown.razor.cs | 4 +- .../Features/FreeAgents/BidCountdownTests.cs | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/Tests/Features/FreeAgents/BidCountdownTests.cs diff --git a/src/Client/Features/FreeAgents/BidCountdown.razor.cs b/src/Client/Features/FreeAgents/BidCountdown.razor.cs index afd8d53..8106207 100644 --- a/src/Client/Features/FreeAgents/BidCountdown.razor.cs +++ b/src/Client/Features/FreeAgents/BidCountdown.razor.cs @@ -11,7 +11,7 @@ public partial class BidCountdown { [Parameter, EditorRequired] public DateTime DateTime { get; set; } - private Timer _timer = new(1000); + private readonly Timer _timer = new(1000); private string _remainingTime = string.Empty; private Color _color = Color.Warning; @@ -37,6 +37,6 @@ private void CountDown(object? source, ElapsedEventArgs e) _remainingTime = remainingTime.Humanize(4, maxUnit: TimeUnit.Day, minUnit: TimeUnit.Second); - StateHasChanged(); + InvokeAsync(StateHasChanged); } } diff --git a/src/Tests/Features/FreeAgents/BidCountdownTests.cs b/src/Tests/Features/FreeAgents/BidCountdownTests.cs new file mode 100644 index 0000000..e0e844a --- /dev/null +++ b/src/Tests/Features/FreeAgents/BidCountdownTests.cs @@ -0,0 +1,39 @@ +using DynamoLeagueBlazor.Client.Features.FreeAgents; + +namespace DynamoLeagueBlazor.Tests.Features.FreeAgents; + +public class BidCountdownTests : UITestBase +{ + [Fact] + public void DisplaysAHumanReadableTime() + { + var cut = RenderComponent(parameters => + { + parameters.Add(p => p.DateTime, DateTime.Now.AddSeconds(1)); + }); + + cut.WaitForState(() => cut.Markup.Contains("seconds")); + } + + [Fact] + public void GivenLessThanADayAway_ThenShowsRedText() + { + var cut = RenderComponent(parameters => + { + parameters.Add(p => p.DateTime, DateTime.Now.AddSeconds(1)); + }); + + cut.Markup.Contains("mud-text-error"); + } + + [Fact] + public void GivenMoreThanADayAway_ThenShowsRedText() + { + var cut = RenderComponent(parameters => + { + parameters.Add(p => p.DateTime, DateTime.Now.AddDays(1)); + }); + + cut.Markup.Contains("mud-text-warning"); + } +} From 4c00c5d9ec6032d4065493b70c59f192fbdbe6b6 Mon Sep 17 00:00:00 2001 From: Ben Sampica Date: Thu, 7 Jul 2022 23:01:27 -0500 Subject: [PATCH 3/5] Add tests --- .../Features/FreeAgents/BidCountdown.razor.cs | 5 +++-- src/Tests/Features/FreeAgents/BidCountdownTests.cs | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Client/Features/FreeAgents/BidCountdown.razor.cs b/src/Client/Features/FreeAgents/BidCountdown.razor.cs index 8106207..9476198 100644 --- a/src/Client/Features/FreeAgents/BidCountdown.razor.cs +++ b/src/Client/Features/FreeAgents/BidCountdown.razor.cs @@ -23,15 +23,16 @@ protected override void OnInitialized() private void CountDown(object? source, ElapsedEventArgs e) { - var remainingTime = (DateTime - DateTime.Now); + var remainingTime = DateTime - DateTime.Now; if (remainingTime <= TimeSpan.FromDays(1)) { _color = Color.Error; - if (remainingTime == TimeSpan.Zero) + if (remainingTime <= TimeSpan.Zero) { _timer.Enabled = false; + remainingTime = TimeSpan.Zero; } } diff --git a/src/Tests/Features/FreeAgents/BidCountdownTests.cs b/src/Tests/Features/FreeAgents/BidCountdownTests.cs index e0e844a..a458294 100644 --- a/src/Tests/Features/FreeAgents/BidCountdownTests.cs +++ b/src/Tests/Features/FreeAgents/BidCountdownTests.cs @@ -5,14 +5,19 @@ namespace DynamoLeagueBlazor.Tests.Features.FreeAgents; public class BidCountdownTests : UITestBase { [Fact] - public void DisplaysAHumanReadableTime() + public void CountsDownEverySecondUntilZero() { var cut = RenderComponent(parameters => { - parameters.Add(p => p.DateTime, DateTime.Now.AddSeconds(1)); + parameters.Add(p => p.DateTime, DateTime.Now.AddSeconds(4)); }); - cut.WaitForState(() => cut.Markup.Contains("seconds")); + cut.WaitForState(() => cut.Markup.Contains("2 seconds")); + cut.WaitForState(() => cut.Markup.Contains("1 second")); + cut.WaitForState(() => cut.Markup.Contains("0 seconds")); + + cut.Render(); + cut.Markup.Should().Contain("0 seconds"); } [Fact] @@ -27,7 +32,7 @@ public void GivenLessThanADayAway_ThenShowsRedText() } [Fact] - public void GivenMoreThanADayAway_ThenShowsRedText() + public void GivenMoreThanADayAway_ThenShowsYellowText() { var cut = RenderComponent(parameters => { From b826d0c8c9599b531c685804962ad4e1eff4fd4c Mon Sep 17 00:00:00 2001 From: Ben Sampica Date: Thu, 7 Jul 2022 23:10:27 -0500 Subject: [PATCH 4/5] Update BidCountdownTests.cs --- src/Tests/Features/FreeAgents/BidCountdownTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tests/Features/FreeAgents/BidCountdownTests.cs b/src/Tests/Features/FreeAgents/BidCountdownTests.cs index a458294..7921bd2 100644 --- a/src/Tests/Features/FreeAgents/BidCountdownTests.cs +++ b/src/Tests/Features/FreeAgents/BidCountdownTests.cs @@ -9,7 +9,7 @@ public void CountsDownEverySecondUntilZero() { var cut = RenderComponent(parameters => { - parameters.Add(p => p.DateTime, DateTime.Now.AddSeconds(4)); + parameters.Add(p => p.DateTime, DateTime.Now.AddSeconds(5)); }); cut.WaitForState(() => cut.Markup.Contains("2 seconds")); From fd0c1acc763b997c8d36c9a2137874c2eda2a1e0 Mon Sep 17 00:00:00 2001 From: Ben Sampica Date: Thu, 7 Jul 2022 23:19:59 -0500 Subject: [PATCH 5/5] update test --- src/Tests/Features/FreeAgents/BidCountdownTests.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Tests/Features/FreeAgents/BidCountdownTests.cs b/src/Tests/Features/FreeAgents/BidCountdownTests.cs index 7921bd2..7079675 100644 --- a/src/Tests/Features/FreeAgents/BidCountdownTests.cs +++ b/src/Tests/Features/FreeAgents/BidCountdownTests.cs @@ -9,11 +9,10 @@ public void CountsDownEverySecondUntilZero() { var cut = RenderComponent(parameters => { - parameters.Add(p => p.DateTime, DateTime.Now.AddSeconds(5)); + parameters.Add(p => p.DateTime, DateTime.Now.AddSeconds(4)); }); - cut.WaitForState(() => cut.Markup.Contains("2 seconds")); - cut.WaitForState(() => cut.Markup.Contains("1 second")); + cut.WaitForState(() => cut.Markup.Contains("1 second"), TimeSpan.FromSeconds(4)); cut.WaitForState(() => cut.Markup.Contains("0 seconds")); cut.Render();