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

fix: Update paging format string to include the sort order #326

Merged
merged 2 commits into from
Nov 16, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,45 @@ public async Task Invalid_Tags_Are_Sanitised(string value)
//assert
actual.SelectedTags.Should().NotContain(value);
}

[TestCase(ResourceSortOrder.UpdatedNewest)]
[TestCase(ResourceSortOrder.UpdatedOldest)]
public async Task SearchAsync_Paging_Should_Respect_SortOrder(ResourceSortOrder sortOrder)
{
// arrange
var results = new ResponseType()
{
ContentCollection = new ContentCollection()
{
Total = 11,
Items = new Collection<SearchResult>()
{
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
}
}
};
SetSearchResults(results);
var query = new ResourcesQuery
{
Page = 2,
Tags = new string[] { "tag1" },
SortOrder = sortOrder
};

// act
var actual = await _sut.SearchAsync(query);

// assert
actual.PagingFormatString.Should().Contain($"sortOrder={(int)sortOrder}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,44 @@ public async Task Invalid_Tags_Are_Sanitised(string value)
//assert
actual.SelectedTags.Should().NotContain(value);
}

[TestCase(ResourceSortOrder.UpdatedNewest)]
[TestCase(ResourceSortOrder.UpdatedOldest)]
public async Task SearchAsync_Paging_Should_Respect_SortOrder(ResourceSortOrder sortOrder)
{
// arrange
var results = new ResponseType()
{
ContentCollection = new ContentCollection()
{
Total = 11,
Items = new Collection<SearchResult>()
{
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
new SearchResult(),
}
}
};
SetSearchResults(results);
var query = new ResourcesQuery
{
Page = 2,
SortOrder = sortOrder
};

// act
var actual = await _sut.SearchAsync(query);

// assert
actual.PagingFormatString.Should().Contain($"sortOrder={(int)sortOrder}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ private static Tuple<int, int, int> CalculatePageStats(SearchResourcesByTags.Res
return Tuple.Create(totalResults, totalPages, Math.Min(page, totalPages));
}

private static string GetPagingFormatString(IEnumerable<string> tags)
private static string GetPagingFormatString(IEnumerable<string> tags, ResourceSortOrder sortOrder)
{
var result = $"/resources-learning?page={{0}}&sortOrder={(int)sortOrder}";

if (tags.Any())
{
var tagStrings = tags.Select(x => $"tags={x}");
var allTags = string.Join("&", tagStrings);
return $"/resources-learning?page={{0}}&{allTags}";
result += $"&{allTags}";
}

return $"/resources-learning?page={{0}}";
return result;
}

public async Task<ResourcesListViewModel> SearchAsync(ResourcesQuery query, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -82,7 +84,7 @@ public async Task<ResourcesListViewModel> SearchAsync(ResourcesQuery query, Canc
currentPage,
totalPages,
totalResults,
GetPagingFormatString(query.Tags)
GetPagingFormatString(query.Tags, query.SortOrder)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ private static Tuple<int, int, int> CalculatePageStats(SearchResourcesByTags.Res
return Tuple.Create(totalResults, totalPages, Math.Min(page, totalPages));
}

private static string GetPagingFormatString(int[] tags)
private static string GetPagingFormatString(int[] tags, ResourceSortOrder sortOrder)
{
var result = $"/resources-learning?page={{0}}&sortOrder={(int)sortOrder}";

if (tags.Any())
{
var tagStrings = tags.Select(x => $"tags={x}");
var allTags = string.Join("&", tagStrings);
return $"/resources-learning?page={{0}}&{allTags}";
result += $"&{allTags}";
}

return $"/resources-learning?page={{0}}";
return result;
}

public async Task<ResourcesListViewModel> SearchAsync(ResourcesQuery query, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -91,7 +93,7 @@ public async Task<ResourcesListViewModel> SearchAsync(ResourcesQuery query, Canc
currentPage,
totalPages,
totalResults,
GetPagingFormatString(queryTags)
GetPagingFormatString(queryTags, query.SortOrder)
);
}
}
Loading