From e2cbda2ebf9ee5ce1386f0adf1d311a5eb3375ef Mon Sep 17 00:00:00 2001 From: J2EEbbesen Date: Sat, 4 Jan 2025 16:07:03 -0600 Subject: [PATCH] Params (#65) * adding params to deals * Adding parameters for happy hour and day of the week --- .../Components/Layout/DealFilterBar.razor | 2 +- .../Components/Pages/Deals.razor | 13 +++++- .../StpFoodBlazor/Helpers/DealFilter.cs | 5 ++- .../Helpers/DealFilterTest.cs | 44 +++++++++++++++++++ .../StpFoodBlazorTest/Pages/DealsTest.razor | 37 +++++++++++++++- 5 files changed, 96 insertions(+), 5 deletions(-) diff --git a/StpFoodBlazor/StpFoodBlazor/Components/Layout/DealFilterBar.razor b/StpFoodBlazor/StpFoodBlazor/Components/Layout/DealFilterBar.razor index e696000..762b092 100644 --- a/StpFoodBlazor/StpFoodBlazor/Components/Layout/DealFilterBar.razor +++ b/StpFoodBlazor/StpFoodBlazor/Components/Layout/DealFilterBar.razor @@ -7,7 +7,7 @@ @foreach (var dow in daysOfWeek) { - + }
diff --git a/StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor b/StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor index 175149b..ae34e60 100644 --- a/StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor +++ b/StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor @@ -33,12 +33,16 @@ else private bool happyHour = false; + [SupplyParameterFromQuery] + public string? Dow { get; set; } + [SupplyParameterFromQuery] + public bool? Hh { get; set; } + protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { allDeals = await DealServiceImpl.GetDealsAsync(); - selectedDayOfWeek = TimeServiceImpl.GetDayOfWeek(); FilterDeals(); SortDeals(); StateHasChanged(); @@ -62,6 +66,13 @@ else SortDeals(); } + protected override void OnParametersSet() + { + selectedDayOfWeek = + String.IsNullOrEmpty(Dow) ? TimeServiceImpl.GetDayOfWeek().ToLower() : Dow.ToLower(); + happyHour = Hh == true; + } + private void FilterDeals() { DealFilter filter = new DealFilter(); diff --git a/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs b/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs index 455ad27..9193e5f 100644 --- a/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs +++ b/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs @@ -43,7 +43,10 @@ public DealEvent[] Filter() { } private static DealEvent[] FilterByDay(DealEvent[] deals, String day) { - return deals.Where(deal => deal.Day == day).ToArray(); + return deals.Where(deal => + !string.IsNullOrWhiteSpace(deal.Day) && + deal.Day.Equals(day, StringComparison.OrdinalIgnoreCase) + ).ToArray(); } private static DealEvent[] FilterByName(DealEvent[] deals, String name) { diff --git a/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs b/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs index e3746b1..fa22798 100644 --- a/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs +++ b/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs @@ -43,6 +43,50 @@ public void ShouldReturnFilteredByDay() { Array.ForEach(filteredDeals, deal => Assert.Equal(day, deal.Day)); } + [Fact] + public void ShouldReturnFilteredByDayDealMissingDay() { + String day = "MonDAY"; + filter.Day = day; + filter.Deals = new DealEvent[] { + new DealEvent { + Name = "Pino's Pizza", + Day = null, + Deal = "No day" + }, + new DealEvent { + Name = "Pino's Pizza", + Day = "", + Deal = "Empty string day" + }, + new DealEvent { + Name = "Pino's Pizza", + Day = " ", + Deal = "White space day" + }, + new DealEvent { + Name = "Pino's Pizza", + Day = "Monday", + Deal = "Monday deal" + } + }; + + DealEvent[] filteredDeals = filter.Filter(); + + Assert.Single(filteredDeals); + Assert.Equal("Monday deal", filteredDeals[0].Deal); + } + + [Fact] + public void ShouldReturnFilteredByDayCaseInsensitive() { + String day = "mondAy"; + filter.Day = day; + + DealEvent[] filteredDeals = filter.Filter(); + + Assert.Equal(45, filteredDeals.Length); + Array.ForEach(filteredDeals, deal => Assert.Equal("Monday", deal.Day)); + } + [Fact] public void ShouldReturnFilteredByName() { String name = "Pino's Pizza"; diff --git a/StpFoodBlazor/StpFoodBlazorTest/Pages/DealsTest.razor b/StpFoodBlazor/StpFoodBlazorTest/Pages/DealsTest.razor index e737b25..eb6e5ed 100644 --- a/StpFoodBlazor/StpFoodBlazorTest/Pages/DealsTest.razor +++ b/StpFoodBlazor/StpFoodBlazorTest/Pages/DealsTest.razor @@ -35,7 +35,7 @@ var header = cut.WaitForElement("#deals_table_header"); var elements = cut.WaitForElement("#deals_table_body_placeholder"); - Assert.Equal(timeService.DayOfWeek, select.GetAttribute("value")); + Assert.Equal(timeService.DayOfWeek.ToLower(), select.GetAttribute("value")); Assert.Equal("Place", header.Children[0].InnerHtml); Assert.Equal(30, elements.ChildElementCount); Assert.Equal(" ", @@ -44,6 +44,39 @@ elements.Children[1].InnerHtml); } + [Fact] + public void DealsShouldAcceptDowParameterCaseInsensitiveSunday() + { + var navigationManager = ctx.Services.GetRequiredService(); + var uri = navigationManager.GetUriWithQueryParameter("dOw", "sUndAY"); + navigationManager.NavigateTo(uri); + + var cut = ctx.Render(@); + var dowSelect = (IHtmlSelectElement)cut.Find("#day-of-week-select"); + var elements = getElements(cut); + + Assert.Equal(6, elements.ChildElementCount); + Assert.Equal("sunday", dowSelect.Value); + Assert.Equal("1881 by Lake Elmo Inn", elements.Children[0].Children[0].InnerHtml); + } + + [Fact] + public void DealsShouldAcceptHhParameterCaseInsensitiveSunday() + { + timeService.DayOfWeek = DayOfWeek.Sunday.ToString(); + var navigationManager = ctx.Services.GetRequiredService(); + var uri = navigationManager.GetUriWithQueryParameter("HH", true); + navigationManager.NavigateTo(uri); + + var cut = ctx.Render(@); + var dowSelect = (IHtmlSelectElement)cut.Find("#day-of-week-select"); + var elements = getElements(cut); + + Assert.Equal(24, elements.ChildElementCount); + Assert.Equal("sunday", dowSelect.Value); + Assert.Equal("1881 by Lake Elmo Inn", elements.Children[0].Children[0].InnerHtml); + } + [Fact] public void DealsShouldDisplayTwoColumnsWednesday() { @@ -106,7 +139,7 @@ var dowSelect = (IHtmlSelectElement)cut.Find("#day-of-week-select"); getElements(cut); // just to make sure table is rendered - Assert.Equal(DateTime.Today.DayOfWeek.ToString(), dowSelect.Value); + Assert.Equal(DateTime.Today.DayOfWeek.ToString().ToLower(), dowSelect.Value); } [Fact]