Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting #11

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ else
allDeals = await DealServiceImpl.GetDealsAsync();
selectedDayOfWeek = TimeServiceImpl.GetDayOfWeek();
FilterDeals();
SortDeals();
StateHasChanged();
}
}
Expand All @@ -75,6 +76,7 @@ else
{
selectedDayOfWeek = (string?)e.Value;
FilterDeals();
SortDeals();
}

private void FilterDeals()
Expand All @@ -84,4 +86,9 @@ else
filter.Deals = allDeals;
deals = filter.Filter();
}

private void SortDeals()
{
deals = DealSorter.SortDealEventsByName(deals);
}
}
19 changes: 19 additions & 0 deletions StpFoodBlazor/StpFoodBlazor/Helpers/DealSorter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using StpFoodBlazor.Models;

namespace StpFoodBlazor.Helpers
{
public static class DealSorter
{
public static DealEvent[] SortDealEventsByName(DealEvent[]? DealEvents)
{
if (DealEvents == null)
{
return [];
}

Array.Sort(DealEvents, static (x, y) =>
string.Compare(x.Name, y.Name, StringComparison.Ordinal));
return [.. DealEvents];
}
}
}
60 changes: 60 additions & 0 deletions StpFoodBlazor/StpFoodBlazorTest/Helpers/DealSorterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections.Generic;
using StpFoodBlazor.Helpers;
using StpFoodBlazor.Models;

namespace StpFoodBlazorTest.Helpers
{
public class DealSorterTest
{
[Fact]
public void SortDealEventsByName_SortsCorrectly()
{
var dealEvents = new DealEvent[]
{
new() { Name = "Sawatdee Saint Paul" },
new() { Name = "Asian Express" },
new() { Name = "Afro Deli" }
};
var expectedOrder = new DealEvent[]
{
new() { Name = "Afro Deli" },
new() { Name = "Asian Express" },
new() { Name = "Sawatdee Saint Paul" }
};

var sortedDealEvents = DealSorter.SortDealEventsByName(dealEvents);

for (int i = 0; i < expectedOrder.Length; i++)
{
Assert.Equal(expectedOrder[i].Name, sortedDealEvents[i].Name);
}
}

[Fact]
public void SortDealEventsByName_EmptyArray()
{
// Arrange
var dealEvents = new DealEvent[]{};

// Act
var sortedDealEvents = DealSorter.SortDealEventsByName(dealEvents);

// Assert
Assert.Empty(sortedDealEvents);
}

[Fact]
public void SortDealEventsByName_SingleElement()
{
var dealEvents = new DealEvent[]
{
new() { Name = "Afro Deli" }
};

var sortedDealEvents = DealSorter.SortDealEventsByName(dealEvents);

Assert.Single(sortedDealEvents);
Assert.Equal("Afro Deli", sortedDealEvents[0].Name);
}
}
}
36 changes: 18 additions & 18 deletions StpFoodBlazor/StpFoodBlazorTest/Pages/DealsTest.razor
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,34 @@
{
timeService.DayOfWeek = DayOfWeek.Wednesday.ToString();
var cut = ctx.Render(@<Deals />);
var tbody = getTbody(cut);
var elements = getElements(cut);

Assert.Equal("Wednesday", tbody.Children[0].Children[0].InnerHtml);
Assert.Equal("Asian Express", tbody.Children[0].Children[1].InnerHtml);
Assert.Equal("$12.95 combo", tbody.Children[0].Children[2].InnerHtml);
Assert.Equal("Wednesday", elements.Children[0].Children[0].InnerHtml);
Assert.Equal("1881 by Lake Elmo Inn", elements.Children[0].Children[1].InnerHtml);
Assert.Equal("Discounted apps https://1881bylei.com/wp-content/uploads/2024/08/1881_happy_hour.pdf", elements.Children[0].Children[2].InnerHtml);
}

[Fact]
public void DealsShouldDisplayAllColumnsThursday()
{
timeService.DayOfWeek = DayOfWeek.Thursday.ToString();
var cut = ctx.Render(@<Deals />);
var tbody = getTbody(cut);
var elements = getElements(cut);

Assert.Equal("Thursday", tbody.Children[0].Children[0].InnerHtml);
Assert.Equal("Asian Express", tbody.Children[0].Children[1].InnerHtml);
Assert.Equal("$12.95 combo", tbody.Children[0].Children[2].InnerHtml);
Assert.Equal("Thursday", elements.Children[0].Children[0].InnerHtml);
Assert.Equal("1881 by Lake Elmo Inn", elements.Children[0].Children[1].InnerHtml);
Assert.Equal("Discounted apps https://1881bylei.com/wp-content/uploads/2024/08/1881_happy_hour.pdf", elements.Children[0].Children[2].InnerHtml);
}

[Fact]
public void DealsShouldDisplayOnlyDealsForSelectedDay()
{
timeService.DayOfWeek = DayOfWeek.Wednesday.ToString();
var cut = ctx.Render(@<Deals />);
var tbody = getTbody(cut);
var elements = getElements(cut);

Assert.Equal(55, tbody.ChildElementCount);
foreach (var tr in tbody.Children)
Assert.Equal(55, elements.ChildElementCount);
foreach (var tr in elements.Children)
{
Assert.Equal("Wednesday", tr.Children[0].InnerHtml);
}
Expand All @@ -74,9 +74,9 @@

dowSelect.Change("Monday");

var tbody = getTbody(cut);
Assert.Equal(45, tbody.ChildElementCount);
foreach (var tr in tbody.Children)
var elements = getElements(cut);
Assert.Equal(45, elements.ChildElementCount);
foreach (var tr in elements.Children)
{
Assert.Equal("Monday", tr.Children[0].InnerHtml);
}
Expand All @@ -90,15 +90,15 @@

dowSelect.Change("Friday");

var tbody = getTbody(cut);
Assert.Equal(51, tbody.ChildElementCount);
foreach (var tr in tbody.Children)
var elements = getElements(cut);
Assert.Equal(51, elements.ChildElementCount);
foreach (var tr in elements.Children)
{
Assert.Equal("Friday", tr.Children[0].InnerHtml);
}
}

private IElement getTbody(IRenderedFragment cut)
private IElement getElements(IRenderedFragment cut)
{
cut.WaitForElement("#deals_table_body", TimeSpan.FromSeconds(5));
return cut.Find("#deals_table_body");
Expand Down