public void UpdateSectionStateTest(bool validSection, string stateJson, string expectedJson) { // Assign var section = new PreSearchFilterSection { Options = validSection ? new List <PreSearchFilterOption> { new PreSearchFilterOption { IsSelected = true, Name = nameof(PreSearchFilterOption.Name), OptionKey = nameof(PreSearchFilterOption.OptionKey) } } : new List <PreSearchFilterOption>() }; // Act var testObject = new PreSearchFilterStateManager(); testObject.RestoreState(stateJson); testObject.UpdateSectionState(section); // Assert testObject.GetStateJson().Should().BeEquivalentTo(expectedJson); }
public PreSearchFilterSection RestoreOptions(PreSearchFilterSection savedSection, IEnumerable <PreSearchFilter> filters) { // If the user choose to go back, then there should be a saved version of the page. var filterSection = new PreSearchFilterSection { SingleSelectedValue = savedSection?.SingleSelectedValue, Options = new List <PreSearchFilterOption>() }; var idCount = 0; if (filters != null) { foreach (var filter in filters) { var psfOptionKey = filter.UrlName; var savedStated = savedSection?.Options?.FirstOrDefault(o => o.OptionKey == psfOptionKey); filterSection.Options.Add(new PreSearchFilterOption { Id = (idCount++).ToString(), IsSelected = savedStated?.IsSelected ?? false, Name = filter.Title, Description = filter.Description, OptionKey = psfOptionKey, ClearOtherOptionsIfSelected = filter.NotApplicable ?? false }); } } return(filterSection); }
private void SetUpStateMangerFakesAndCalls(PreSearchFilterType filterType, bool shouldSaveState, bool addNotApplicable = true) { var dummyStateSection = new PreSearchFilterSection { SectionDataType = filterType, Options = GetDummyPreSearchFilterOption(addNotApplicable) }; fakePsfStateManager = A.Fake <IPreSearchFilterStateManager>(ops => ops.Strict()); A.CallTo(() => fakePsfStateManager.GetSavedSection(A <string> ._, A <PreSearchFilterType> ._)).Returns(dummyStateSection); A.CallTo(() => fakePsfStateManager.RestoreOptions(A <PreSearchFilterSection> ._, A <IEnumerable <PreSearchFilter> > ._)).Returns(dummyStateSection); A.CallTo(() => fakePsfStateManager.GetStateJson()).Returns("DummyStateJson"); A.CallTo(() => fakePsfStateManager.ShouldSaveState(A <int> ._, A <int> ._)).Returns(shouldSaveState); A.CallTo(() => fakePsfStateManager.RestoreState(A <string> ._)).DoesNothing(); A.CallTo(() => fakePsfStateManager.SaveState(A <PreSearchFilterSection> ._)).DoesNothing(); }
private static PreSearchFilterSection GetPreSearchFilterSection(PreSearchFilterType preSearchFilter, string sectionTitle) { var section = new PreSearchFilterSection { SectionDataType = preSearchFilter, Name = sectionTitle, Options = new List <PreSearchFilterOption> { new PreSearchFilterOption { Name = nameof(PreSearchFilterOption.Name), IsSelected = true, OptionKey = $"URL-{NumberDummyFilterOptions - 2}" } } }; return(section); }
public void UpdateSectionState(PreSearchFilterSection section) { var savedSectionIndex = StateModel?.Sections?.ToList().FindIndex(s => s.Name.Equals(section.Name, StringComparison.InvariantCultureIgnoreCase) && s.SectionDataType == section.SectionDataType); //just keep the selected options section.Options = section.Options?.Where(o => o.IsSelected == true).ToList(); if (savedSectionIndex > -1) { StateModel.Sections.ToList()[savedSectionIndex.Value].Options = section.Options; } else { StateModel.Sections.Add(section); } StateJson = JsonConvert.SerializeObject(StateModel); }
public void SaveState(PreSearchFilterSection previousSection) { if (previousSection != null) { if (previousSection.SingleSelectOnly) { //if we have a single select option just keep that if (previousSection.SingleSelectedValue != null) { previousSection.Options.First(o => o.OptionKey == previousSection.SingleSelectedValue).IsSelected = true; } } else { //If there is no JS client side we may have other options selected as well, clear out other options if NON applicable is selected if (previousSection.Options.Any(o => o.IsSelected && o.ClearOtherOptionsIfSelected)) { previousSection.Options = previousSection.Options.Where(o => o.IsSelected && o.ClearOtherOptionsIfSelected).Take(1).ToList(); } } UpdateSectionState(previousSection); } }