Skip to content

Commit

Permalink
Params (#65)
Browse files Browse the repository at this point in the history
* adding params to deals

* Adding parameters for happy hour and day of the week
  • Loading branch information
eebbesen authored Jan 4, 2025
1 parent 5ced0d9 commit e2cbda2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<option value="">All</option>
@foreach (var dow in daysOfWeek)
{
<option value="@dow">@dow</option>
<option value="@dow.ToString().ToLower()">@dow</option>
}
</select>
<div class="form-check-inline ms-3 d-flex flex-row-reverse">
Expand Down
13 changes: 12 additions & 1 deletion StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 44 additions & 0 deletions StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
37 changes: 35 additions & 2 deletions StpFoodBlazor/StpFoodBlazorTest/Pages/DealsTest.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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("<span class=\"col-12 rounded placeholder bg-white\">&nbsp;</span>",
Expand All @@ -44,6 +44,39 @@
elements.Children[1].InnerHtml);
}

[Fact]
public void DealsShouldAcceptDowParameterCaseInsensitiveSunday()
{
var navigationManager = ctx.Services.GetRequiredService<NavigationManager>();
var uri = navigationManager.GetUriWithQueryParameter("dOw", "sUndAY");
navigationManager.NavigateTo(uri);

var cut = ctx.Render(@<Deals />);
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<NavigationManager>();
var uri = navigationManager.GetUriWithQueryParameter("HH", true);
navigationManager.NavigateTo(uri);

var cut = ctx.Render(@<Deals />);
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()
{
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit e2cbda2

Please sign in to comment.