-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: WhenLanguageOnGetIsCalled * test: WhenWhatLanguageOnPostIsCalled.cs * test: post add test Also added more descriptive method names
- Loading branch information
Showing
4 changed files
with
202 additions
and
11 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
...rviceDirectory.Admin.Web.UnitTests/Pages/manage-services/WhenWhatLanguageOnGetIsCalled.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System.Text.Json; | ||
using FamilyHubs.ServiceDirectory.Admin.Core.DistributedCache; | ||
using FamilyHubs.ServiceDirectory.Admin.Core.Models.ServiceJourney; | ||
using FamilyHubs.ServiceDirectory.Admin.Web.Pages.manage_services; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace FamilyHubs.ServiceDirectory.Admin.Web.UnitTests.Pages.manage_services; | ||
|
||
public class WhenWhatLanguageOnGetIsCalled | ||
{ | ||
private readonly IRequestDistributedCache _mockCache; | ||
|
||
public WhenWhatLanguageOnGetIsCalled() | ||
{ | ||
_mockCache = Substitute.For<IRequestDistributedCache>(); | ||
} | ||
|
||
[Fact] | ||
public async Task OnGetWithModel_NoUserInputOrPreviousEntry_PopulatesUserLanguageOptionsWithDefault() | ||
{ | ||
// Arrange | ||
var cachedData = new ServiceModel<WhatLanguageViewModel>(); | ||
_mockCache.GetAsync<ServiceModel<WhatLanguageViewModel>>(Arg.Any<string>()).Returns(cachedData); | ||
|
||
var model = new What_LanguageModel(_mockCache); | ||
|
||
// Act | ||
await model.OnGetAsync("add", null, false); | ||
|
||
// Assert | ||
var expectedOptions = new List<SelectListItem> | ||
{ | ||
new("", ""), | ||
}; | ||
|
||
Assert.Equal(expectedOptions.Select(o => o.Value), model.UserLanguageOptions.Select(o => o.Value)); | ||
Assert.Equal(expectedOptions.Select(o => o.Text), model.UserLanguageOptions.Select(o => o.Text)); | ||
} | ||
|
||
[Fact] | ||
public async Task OnGetWithModel_NoUserInputWithPreviousEntry_PopulatesUserLanguageOptionsFromServiceModel() | ||
{ | ||
// Arrange | ||
var cachedData = new ServiceModel<WhatLanguageViewModel> | ||
{ | ||
LanguageCodes = new List<string> { "en", "fr" } | ||
}; | ||
_mockCache.GetAsync<ServiceModel<WhatLanguageViewModel>>(Arg.Any<string>()).Returns(cachedData); | ||
|
||
var model = new What_LanguageModel(_mockCache); | ||
|
||
// Act | ||
await model.OnGetAsync("add", null, false); | ||
|
||
// Assert | ||
var expectedOptions = new List<SelectListItem> | ||
{ | ||
new("English", "en"), | ||
new("French", "fr") | ||
}; | ||
|
||
Assert.Equal(expectedOptions.Select(o => o.Value), model.UserLanguageOptions.Select(o => o.Value)); | ||
Assert.Equal(expectedOptions.Select(o => o.Text), model.UserLanguageOptions.Select(o => o.Text)); | ||
} | ||
|
||
[Fact] | ||
public async Task OnGetWithModel_RedirectingToSelf_PopulatesUserLanguageOptionsFromUserInput() | ||
{ | ||
// Arrange | ||
var mockVm = new WhatLanguageViewModel | ||
{ | ||
Languages = ["English", "French"] | ||
}; | ||
var cachedData = new ServiceModel<WhatLanguageViewModel> | ||
{ | ||
UserInput = mockVm, | ||
UserInputType = typeof(WhatLanguageViewModel).FullName, | ||
UserInputJson = JsonSerializer.Serialize(mockVm), | ||
LanguageCodes = new List<string> { "de" } // <- Here to prove it only gets values from UserInput | ||
}; | ||
_mockCache.GetAsync<ServiceModel<WhatLanguageViewModel>>(Arg.Any<string>()).Returns(cachedData); | ||
|
||
var model = new What_LanguageModel(_mockCache); | ||
|
||
// Act | ||
await model.OnGetAsync("add", null, true); | ||
|
||
// Assert | ||
var expectedOptions = new List<SelectListItem> | ||
{ | ||
new("English", "en"), | ||
new("French", "fr") | ||
}; | ||
|
||
Assert.Equal(expectedOptions.Select(o => o.Value), model.UserLanguageOptions.Select(o => o.Value)); | ||
Assert.Equal(expectedOptions.Select(o => o.Text), model.UserLanguageOptions.Select(o => o.Text)); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...viceDirectory.Admin.Web.UnitTests/Pages/manage-services/WhenWhatLanguageOnPostIsCalled.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System.Text.Json; | ||
using FamilyHubs.ServiceDirectory.Admin.Core.DistributedCache; | ||
using FamilyHubs.ServiceDirectory.Admin.Core.Models.ServiceJourney; | ||
using FamilyHubs.ServiceDirectory.Admin.Web.Pages.manage_services; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace FamilyHubs.ServiceDirectory.Admin.Web.UnitTests.Pages.manage_services; | ||
|
||
public class WhenWhatLanguageOnPostIsCalled | ||
{ | ||
private readonly IRequestDistributedCache _mockCache; | ||
|
||
public WhenWhatLanguageOnPostIsCalled() | ||
{ | ||
_mockCache = Substitute.For<IRequestDistributedCache>(); | ||
} | ||
|
||
[Fact] | ||
public async Task OnPostWithModel_RemoveButton_RemovesLanguage() | ||
{ | ||
// Arrange | ||
var cachedData = new ServiceModel<WhatLanguageViewModel>(); | ||
var model = new What_LanguageModel(_mockCache) | ||
{ | ||
ServiceModel = cachedData | ||
}; | ||
_mockCache.GetAsync<ServiceModel<WhatLanguageViewModel>>(Arg.Any<string>()).Returns(cachedData); | ||
|
||
var formCollection = new FormCollection(new Dictionary<string, Microsoft.Extensions.Primitives.StringValues> | ||
{ | ||
{ "button", "remove-1" }, // Simulate remove button click for the second language | ||
{ "language", new Microsoft.Extensions.Primitives.StringValues(["en", "fr", "de"]) } | ||
}); | ||
|
||
var httpContext = new DefaultHttpContext | ||
{ | ||
Request = { Form = formCollection } | ||
}; | ||
|
||
model.PageContext.HttpContext = httpContext; | ||
|
||
// Act | ||
await model.OnPostAsync("add", null, default); | ||
|
||
// Assert | ||
// Necessary deserialization of UserInputJson as the model does not retain the UserInput | ||
var userInput = JsonSerializer.Deserialize<WhatLanguageViewModel>(model.ServiceModel.UserInputJson!); | ||
var updatedLanguages = userInput!.Languages.ToList(); | ||
Assert.NotNull(updatedLanguages); | ||
Assert.DoesNotContain("fr", updatedLanguages); | ||
Assert.Equal(2, updatedLanguages!.Count); | ||
} | ||
|
||
[Fact] | ||
public async Task OnPostWithModel_AddButton_AddsLanguage() | ||
{ | ||
// Arrange | ||
var cachedData = new ServiceModel<WhatLanguageViewModel>(); | ||
var model = new What_LanguageModel(_mockCache) | ||
{ | ||
ServiceModel = cachedData | ||
}; | ||
_mockCache.GetAsync<ServiceModel<WhatLanguageViewModel>>(Arg.Any<string>()).Returns(cachedData); | ||
|
||
var formCollection = new FormCollection(new Dictionary<string, Microsoft.Extensions.Primitives.StringValues> | ||
{ | ||
{ "button", "add" }, // Simulate add button click | ||
{ "language", new Microsoft.Extensions.Primitives.StringValues(["en", "fr", "de"]) } | ||
}); | ||
|
||
var httpContext = new DefaultHttpContext | ||
{ | ||
Request = { Form = formCollection } | ||
}; | ||
|
||
model.PageContext.HttpContext = httpContext; | ||
|
||
// Act | ||
await model.OnPostAsync("add"); | ||
|
||
// Assert | ||
// Necessary deserialization of UserInputJson as the model does not retain the UserInput | ||
var userInput = JsonSerializer.Deserialize<WhatLanguageViewModel>(model.ServiceModel.UserInputJson!); | ||
var updatedLanguages = userInput!.Languages.ToList(); | ||
Assert.NotNull(updatedLanguages); | ||
Assert.Contains("English", updatedLanguages); | ||
Assert.Contains("French", updatedLanguages); | ||
Assert.Contains("German", updatedLanguages); | ||
Assert.Contains("", updatedLanguages); // <- Adds the new field | ||
Assert.Equal(4, updatedLanguages!.Count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters