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

feat: Add Resource search page #286

Merged
merged 18 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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 @@ -10,7 +10,7 @@ namespace Childrens_Social_Care_CPD_Tests.Contentful.Renderers;

public class HyperlinkRendererTests
{
private HyperlinkRenderer _sut = new HyperlinkRenderer();
private readonly HyperlinkRenderer _sut = new();

[Test]
public void HyperlinkToHtml_Returns_Anchor()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
using Castle.Core.Logging;
using Childrens_Social_Care_CPD.Contentful;
using Childrens_Social_Care_CPD.Contentful.Models;
using Childrens_Social_Care_CPD.Controllers;
using Childrens_Social_Care_CPD.Models;
using Contentful.Core.Models;
using Contentful.Core.Search;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace Childrens_Social_Care_CPD_Tests.Controllers;

public class ResourcesControllerTests
{
private ResourcesController _resourcesController;
private IRequestCookieCollection _cookies;
private HttpContext _httpContext;
private HttpRequest _httpRequest;
private ICpdContentfulClient _contentfulClient;
private ILogger<ResourcesController> _logger;

private void SetContent(Content content, ContentfulCollection<Resource> resourceCollection)
{
resourceCollection ??= new ();

_contentfulClient
.GetEntries(Arg.Any<QueryBuilder<Resource>>(), default)
.Returns(resourceCollection);

var contentCollection = new ContentfulCollection<Content>();

contentCollection.Items = content == null
? new List<Content>()
: contentCollection.Items = new List<Content> { content };

_contentfulClient
.GetEntries(Arg.Any<QueryBuilder<Content>>(), default)
.Returns(contentCollection);
}

[SetUp]
public void SetUp()
{
_logger = Substitute.For<ILogger<ResourcesController>>();
_cookies = Substitute.For<IRequestCookieCollection>();
_httpContext = Substitute.For<HttpContext>();
_httpRequest = Substitute.For<HttpRequest>();
var controllerContext = Substitute.For<ControllerContext>();

_httpRequest.Cookies.Returns(_cookies);
_httpContext.Request.Returns(_httpRequest);
controllerContext.HttpContext = _httpContext;

_contentfulClient = Substitute.For<ICpdContentfulClient>();

_resourcesController = new ResourcesController(_logger, _contentfulClient)
{
ControllerContext = controllerContext,
TempData = Substitute.For<ITempDataDictionary>()
};
}

[Test]
public async Task Search_With_Empty_Query_Returns_View()
{
// arrange
SetContent(null, null);

// act
var actual = await _resourcesController.Search(query: null) as ViewResult;

// assert
actual.Should().BeOfType<ViewResult>();
actual.Model.Should().BeOfType<ResourcesListViewModel>();
}

[Test]
public async Task Search_Page_Resource_Is_Passed_To_View()
{
// arrange
var content = new Content();
SetContent(content, null);
var query = new ResourcesQuery
{
Page = 1,
Tags = Array.Empty<int>()
};

// act
var actual = (await _resourcesController.Search(query: null) as ViewResult)?.Model as ResourcesListViewModel;

// assert
actual.Content.Should().Be(content);
}

[Test]
public async Task Search_Sets_The_ViewState_ContextModel()
{
// arrange
SetContent(null, null);

// act
await _resourcesController.Search(null);
var actual = _resourcesController.ViewData["ContextModel"] as ContextModel;

// assert
actual.Should().NotBeNull();
actual.Id.Should().Be(string.Empty);
actual.Title.Should().Be("Resources");
actual.Category.Should().Be("Resources");
}

[Test]
public async Task Search_Selected_Tags_Are_Passed_Into_View()
{
// arrange
SetContent(null, null);
var query = new ResourcesQuery
{
Page = 1,
Tags = new int[] { 1, 2 }
};

// act
var actual = (await _resourcesController.Search(query) as ViewResult)?.Model as ResourcesListViewModel;

// assert
actual.SelectedTags.Should().Equal(query.Tags);
}

[Test]
public async Task Search_Page_Set_To_Be_In_Bounds()
{
// arrange
SetContent(null, new ContentfulCollection<Resource> {
Items = new List<Resource> {
new Resource(),
new Resource(),
new Resource(),
},
Total = 3
});
var query = new ResourcesQuery
{
Page = 2,
Tags = new int[] { 1, 2 }
};

// act
var actual = (await _resourcesController.Search(query) as ViewResult)?.Model as ResourcesListViewModel;

// assert
actual.CurrentPage.Should().Be(1);
}

[Test]
public async Task Search_Invalid_Tags_Logs_Warning()
{
// arrange
SetContent(null, null);
var tags = new int[] { -1 };
var query = new ResourcesQuery
{
Page = 2,
Tags = tags
};

// act
await _resourcesController.Search(query);

//assert
_logger.ReceivedWithAnyArgs(1).LogWarning(default, args: default);
}
}
Loading