Skip to content

Commit

Permalink
feat: Update resources menu item & feature control the whole of resou…
Browse files Browse the repository at this point in the history
…rces

Merge pull request #312 from DFE-Digital/feat/update-resources-menu-item
  • Loading branch information
killij authored Nov 1, 2023
2 parents b8f74fa + bc87e08 commit c4b9dd7
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Childrens_Social_Care_CPD.Controllers;
using Childrens_Social_Care_CPD.Configuration;
using Childrens_Social_Care_CPD.Controllers;
using Childrens_Social_Care_CPD.Core.Resources;
using Childrens_Social_Care_CPD.Models;
using FluentAssertions;
Expand All @@ -14,6 +15,7 @@ namespace Childrens_Social_Care_CPD_Tests.Controllers;

public class ResourcesControllerTests
{
private IFeaturesConfig _featuresConfig;
private IResourcesSearchStrategy _searchStrategy;
private ResourcesController _resourcesController;
private IRequestCookieCollection _cookies;
Expand All @@ -32,8 +34,10 @@ public void SetUp()
_httpContext.Request.Returns(_httpRequest);
controllerContext.HttpContext = _httpContext;

_featuresConfig = Substitute.For<IFeaturesConfig>();
_featuresConfig.IsEnabled(Features.ResourcesAndLearning).Returns(true);
_searchStrategy = Substitute.For<IResourcesSearchStrategy>();
_resourcesController = new ResourcesController(_searchStrategy)
_resourcesController = new ResourcesController(_featuresConfig, _searchStrategy)
{
ControllerContext = controllerContext,
TempData = Substitute.For<ITempDataDictionary>()
Expand All @@ -53,4 +57,19 @@ public async Task Search_Returns_Strategy_Model()
// assert
actual.Model.Should().Be(model);
}

[Test]
public async Task Disabling_Resources_Feature_Returns_NotFoundResult()
{
// arrange
_featuresConfig.IsEnabled(Features.ResourcesAndLearning).Returns(false);
var model = new ResourcesListViewModel(null, null, null, null);
_searchStrategy.SearchAsync(Arg.Any<ResourcesQuery>(), Arg.Any<CancellationToken>()).Returns(model);

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

// assert
actual.Should().BeOfType<NotFoundResult>();
}
}
1 change: 1 addition & 0 deletions Childrens-Social-Care-CPD/Configuration/Features.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public static class Features
{
public const string ResourcesAndLearning = "resources-learning";
public const string ResourcesUseDynamicTags = "resources-search-use-dynamic-tags";
}
15 changes: 11 additions & 4 deletions Childrens-Social-Care-CPD/Controllers/ResourcesController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Childrens_Social_Care_CPD.Core.Resources;
using Childrens_Social_Care_CPD.Configuration;
using Childrens_Social_Care_CPD.Core.Resources;
using Childrens_Social_Care_CPD.Models;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -17,19 +18,25 @@ public ResourcesQuery()

public class ResourcesController : Controller
{
private readonly IFeaturesConfig _featuresConfig;
private readonly IResourcesSearchStrategy _strategy;

public ResourcesController(IResourcesSearchStrategy strategy)
public ResourcesController(IFeaturesConfig featuresConfig, IResourcesSearchStrategy strategy)
{
ArgumentNullException.ThrowIfNull(strategy);

_featuresConfig = featuresConfig;
_strategy = strategy;
}

[Route("resources", Name = "Resource")]
[Route("resources-learning")]
[HttpGet]
public async Task<IActionResult> Search([FromQuery] ResourcesQuery query, bool preferencesSet = false, CancellationToken cancellationToken = default)
{
if (!_featuresConfig.IsEnabled(Features.ResourcesAndLearning))
{
return NotFound();
}

var contextModel = new ContextModel(string.Empty, "Resources", "Resources", "Resources", true, preferencesSet);
ViewData["ContextModel"] = contextModel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ private static string GetPagingFormatString(IEnumerable<string> tags)
{
var tagStrings = tags.Select(x => $"tags={x}");
var allTags = string.Join("&", tagStrings);
return $"/resources?page={{0}}&{allTags}";
return $"/resources-learning?page={{0}}&{allTags}";
}

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

public async Task<ResourcesListViewModel> SearchAsync(ResourcesQuery query, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ private static string GetPagingFormatString(int[] tags)
{
var tagStrings = tags.Select(x => $"tags={x}");
var allTags = string.Join("&", tagStrings);
return $"/resources?page={{0}}&{allTags}";
return $"/resources-learning?page={{0}}&{allTags}";
}

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

public async Task<ResourcesListViewModel> SearchAsync(ResourcesQuery query, CancellationToken cancellationToken = default)
Expand Down
6 changes: 3 additions & 3 deletions Childrens-Social-Care-CPD/Views/Resources/Search.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@if (featuresConfig.IsEnabled(Features.ResourcesUseDynamicTags))
{
<gds-filter clear-filters-uri="/resources">
<gds-filter clear-filters-uri="/resources-learning">
@{
var groups = Model.TagInfos.GroupBy(x => x.Category);
var index = 1; // gov uk js requires starting at 1 for this control
Expand All @@ -41,7 +41,7 @@
else
{
var tagsDict = new Dictionary<int, TagInfo>(Model.TagInfos.Select((tagInfo, index) => KeyValuePair.Create(index, tagInfo)));
<gds-filter clear-filters-uri="/resources">
<gds-filter clear-filters-uri="/resources-learning">
@{
var groupsx = tagsDict.GroupBy(kvp => kvp.Value.Category);
var index = 1; // gov uk js requires starting at 1 for this control
Expand All @@ -65,7 +65,7 @@

@{
var resultString = Model.TotalResults == 1 ? "result" : "results";
<h2 class="gem-c-heading govuk-heading-s govuk-!-margin-bottom-2">
<h2 class="gem-c-heading govuk-heading-s govuk-!-margin-bottom-2" data-testid="results-count">
@Model.TotalResults @resultString
</h2>
<hr />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@model SearchResourcesByTags.SearchResult

<div>
<h2><a href="@Model.LinkedFrom.ContentCollection.Items.First().Id">@Model.Title</a></h2>
<h2><a href="/@Model.LinkedFrom.ContentCollection.Items.First().Id">@Model.Title</a></h2>
<p>@Model.SearchSummary</p>
<ul class="govuk-list govuk-body-s" style="display:flex; gap:15px; flex-flow:row">
<li>From: @Model.From</li>
Expand Down
26 changes: 16 additions & 10 deletions Childrens-Social-Care-CPD/Views/Shared/_ErrorLayout.cshtml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
@inject ICookieHelper _cookieHelper;
@using Childrens_Social_Care_CPD.Configuration;

@inject ICookieHelper cookieHelper;
@inject IFeaturesConfig featuresConfig;

<!DOCTYPE html>
<html lang="en" class="govuk-template">
<head>
<title>@ViewBag.Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<link href="~/css/application.min.css" rel="stylesheet" />
@if (_cookieHelper.GetRequestAnalyticsCookieState(Context) == AnalyticsConsentState.Accepted)
@if (cookieHelper.GetRequestAnalyticsCookieState(Context) == AnalyticsConsentState.Accepted)
{
<partial name="_GoogleAnalyticsPartial" />
}
Expand Down Expand Up @@ -78,14 +81,17 @@
</svg>
</a>
</li>
<li class="dfe-header__navigation-item" id="mmi-resources">
<a class="dfe-header__navigation-link" href="/resources">
Resources
<svg class="dfe-icon dfe-icon__chevron-right" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" width="34" height="34">
<path d="M15.5 12a1 1 0 0 1-.29.71l-5 5a1 1 0 0 1-1.42-1.42l4.3-4.29-4.3-4.29a1 1 0 0 1 1.42-1.42l5 5a1 1 0 0 1 .29.71z"></path>
</svg>
</a>
</li>
@if (featuresConfig.IsEnabled(Features.ResourcesAndLearning))
{
<li class="dfe-header__navigation-item" id="mmi-resources">
<a class="dfe-header__navigation-link" href="/resources-learning">
Resources and learning
<svg class="dfe-icon dfe-icon__chevron-right" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" width="34" height="34">
<path d="M15.5 12a1 1 0 0 1-.29.71l-5 5a1 1 0 0 1-1.42-1.42l4.3-4.29-4.3-4.29a1 1 0 0 1 1.42-1.42l5 5a1 1 0 0 1 .29.71z"></path>
</svg>
</a>
</li>
}
</ul>
</div>
</nav>
Expand Down
13 changes: 10 additions & 3 deletions Childrens-Social-Care-CPD/Views/Shared/_Header.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
@{
@using Childrens_Social_Care_CPD.Configuration;

@inject IFeaturesConfig featuresConfig

@{
ContextModel viewModel = ViewBag.ContextModel;
var category = viewModel.Category ?? "Home";
}
Expand Down Expand Up @@ -53,8 +57,11 @@
RenderMenuItem("career", "Career stages", "career-stages", category == "Career information");
RenderMenuItem("developmentProgrammes", "Development programmes", "development-programmes", category == "Development programmes");
RenderMenuItem("exploreRoles", "Explore roles", "explore-roles", category == "Explore roles");
RenderMenuItem("resources", "Resources", "resources", category == "Resources");


if (featuresConfig.IsEnabled(Features.ResourcesAndLearning))
{
RenderMenuItem("resources", "Resources and learning", "resources-learning", category == "Resources");
}
}
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test.describe('Header', () => {
['Career stages', '/career-stages'],
['Development programmes', '/development-programmes'],
['Explore roles', '/explore-roles'],
['Resources', '/training-resources'],
['Resources and learning', '/resources-learning'],
]

for (const link of links) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { test, expect } from '@playwright/test'

test.describe('Resources and learning', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/resources-learning')
})

test.describe('Filter accordian @accordian', () => {
test('All sections are collapsed when you arrive on page' , async ({ page }) => {
const sections = await page.locator('.govuk-accordion__section-content').all()
expect(sections.length).toBeGreaterThan(1)
for (const section of sections)
{
await expect(section).toHaveAttribute('hidden', 'until-found')
}
})

test('Clicking expand all, expands the sections' , async ({ page }) => {
await page.getByRole('button', { name: 'Show all sections', exact: true }).click()

const sections = await page.locator('.govuk-accordion__section-content').all()
expect(sections.length).toBeGreaterThan(1)
for (const section of sections)
{
await expect(section).not.toHaveAttribute('hidden', 'until-found')
}
})
})

test('Filter by tags performs search' , async ({ page }) => {
await page.getByRole('button', { name: 'Show all sections', exact: true }).click()
const beforeText = await page.getByTestId("results-count").textContent()
await page.locator('.govuk-checkboxes__label').first().click()
await page.getByRole('button', { name: 'Apply filter', exact: true }).click()
const afterText = await page.getByTestId("results-count").textContent()
expect(beforeText).not.toEqual(afterText)
})
})

0 comments on commit c4b9dd7

Please sign in to comment.