Skip to content

Commit

Permalink
fix forms engine tests by mocking necessary services
Browse files Browse the repository at this point in the history
  • Loading branch information
andymantell committed Sep 27, 2024
1 parent 6cbfc66 commit 0922b9c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
7 changes: 6 additions & 1 deletion Frontend/CO.CDP.OrganisationApp.Tests/FormsEngineTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CO.CDP.OrganisationApp.Models;
using CO.CDP.OrganisationApp.Pages.Forms.ChoiceProviderStrategies;
using FluentAssertions;
using Moq;
using DataShareWebApiClient = CO.CDP.DataSharing.WebApiClient;
Expand All @@ -11,14 +12,16 @@ public class FormsEngineTests
private readonly Mock<WebApiClient.IFormsClient> _formsApiClientMock;
private readonly Mock<DataShareWebApiClient.IDataSharingClient> _dataSharingClientMock;
private readonly Mock<ITempDataService> _tempDataServiceMock;
private readonly Mock<IChoiceProviderService> _choiceProviderServiceMock;
private readonly FormsEngine _formsEngine;

public FormsEngineTests()
{
_formsApiClientMock = new Mock<WebApiClient.IFormsClient>();
_dataSharingClientMock = new Mock<DataShareWebApiClient.IDataSharingClient>();
_tempDataServiceMock = new Mock<ITempDataService>();
_formsEngine = new FormsEngine(_formsApiClientMock.Object, _tempDataServiceMock.Object, _dataSharingClientMock.Object);
_choiceProviderServiceMock = new Mock<IChoiceProviderService>();
_formsEngine = new FormsEngine(_formsApiClientMock.Object, _tempDataServiceMock.Object, _choiceProviderServiceMock.Object, _dataSharingClientMock.Object);
}

private static (Guid organisationId, Guid formId, Guid sectionId, string sessionKey) CreateTestGuids()
Expand Down Expand Up @@ -135,6 +138,8 @@ public async Task GetFormSectionAsync_ShouldFetchAndCacheResponse_WhenCachedResp

_tempDataServiceMock.Setup(t => t.Peek<SectionQuestionsResponse>(sessionKey))
.Returns((SectionQuestionsResponse?)null);
_choiceProviderServiceMock.Setup(t => t.GetStrategy(It.IsAny<string>()))
.Returns(new DefaultChoiceProviderStrategy());
_formsApiClientMock.Setup(c => c.GetFormSectionQuestionsAsync(formId, sectionId, organisationId))
.ReturnsAsync(apiResponse);

Expand Down
9 changes: 4 additions & 5 deletions Frontend/CO.CDP.OrganisationApp/FormsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace CO.CDP.OrganisationApp;
public class FormsEngine(
IFormsClient formsApiClient,
ITempDataService tempDataService,
IServiceProvider serviceProvider,
IChoiceProviderService choiceProviderService,
DataShareWebApiClient.IDataSharingClient dataSharingClient) : IFormsEngine
{
Expand All @@ -19,10 +18,10 @@ public async Task<SectionQuestionsResponse> GetFormSectionAsync(Guid organisatio
var sessionKey = $"Form_{organisationId}_{formId}_{sectionId}_Questions";
var cachedResponse = tempDataService.Peek<SectionQuestionsResponse>(sessionKey);

//if (cachedResponse != null)
//{
// return cachedResponse;
//}
if (cachedResponse != null)
{
return cachedResponse;
}

var response = await formsApiClient.GetFormSectionQuestionsAsync(formId, sectionId, organisationId);

Expand Down
2 changes: 1 addition & 1 deletion Frontend/CO.CDP.OrganisationApp/UserInfoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task<bool> UserHasScope(string scope)
return scopes.Contains(scope);
}

public async Task<UserOrganisation?> GetPersonOrganisation(Guid organisationId)
private async Task<UserOrganisation?> GetPersonOrganisation(Guid organisationId)
{
// Role checks may be made multiple times when building a page
// Therefore we cache the person's organisation details for the duration of the http request
Expand Down

0 comments on commit 0922b9c

Please sign in to comment.