public async Task ProcessAsync_AddsFieldsetToContext() { // Arrange var radiosContext = new RadiosContext(name: null, aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-fieldset", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-fieldset", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var fieldsetContext = context.GetContextItem <RadiosFieldsetContext>(); fieldsetContext.SetLegend(isPageHeading: true, attributes: null, content: new HtmlString("Legend")); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosFieldsetTagHelper(); // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.True(radiosContext.Fieldset?.Legend?.IsPageHeading); Assert.Equal("Legend", radiosContext.Fieldset?.Legend?.Content?.RenderToString()); }
public async Task ProcessAsync_NoNameButParentHasName_DoesNotThrowInvalidOperationException() { // Arrange var radiosContext = new RadiosContext(name: "parent", aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper() { Value = "value" }; // Act var ex = await Record.ExceptionAsync(() => tagHelper.ProcessAsync(context, output)); // Assert Assert.Null(ex); }
public async Task ProcessAsync_ValueNotSpecifiedThrowsNotSupportedException() { // Arrange var radiosContext = new RadiosContext( idPrefix: "prefix", resolvedName: "r", viewContext: null, aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.AppendHtml(new HtmlString("Legend")); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper(new DefaultGovUkHtmlGenerator()); // Act & Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => tagHelper.ProcessAsync(context, output)); Assert.Equal("The 'value' attribute must be specified.", ex.Message); }
public async Task ProcessAsync_NoValue_ThrowsInvalidOperationException() { // Arrange var radiosContext = new RadiosContext(name: "test", aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper(); // Act var ex = await Record.ExceptionAsync(() => tagHelper.ProcessAsync(context, output)); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal("The 'value' attribute must be specified.", ex.Message); }
public async Task ProcessAsync_AddsItemToContext() { // Arrange var radiosContext = new RadiosContext( idPrefix: "prefix", resolvedName: "r", viewContext: null, aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var itemContext = (RadiosItemContext)context.Items[typeof(RadiosItemContext)]; itemContext.SetHint(attributes: null, content: new HtmlString("Hint")); itemContext.SetConditional(attributes: null, new HtmlString("Conditional")); var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.AppendHtml(new HtmlString("Label")); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper(new DefaultGovUkHtmlGenerator()) { IsChecked = true, Id = "id", Value = "V" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Contains( radiosContext.Items, item => item is RadiosItem i && i.IsChecked && !i.IsDisabled && i.Content.AsString() == "Label" && !i.IsDisabled && i.Id == "id" && i.Value == "V" && i.ConditionalContent.AsString() == "Conditional" && i.HintContent.AsString() == "Hint"); }
public void SetLabel_ThrowsNotSupportedException() { // Arrange var context = new RadiosContext(name: null, aspFor: null); // Act var ex = Record.Exception(() => context.SetLabel(isPageHeading: false, attributes: null, content: null)); // Assert Assert.IsType <NotSupportedException>(ex); }
public void CloseFieldset_FieldsetNotOpened_ThrowsInvalidOperationException() { // Arrange var context = new RadiosContext(name: null, aspFor: null); // Act var ex = Record.Exception(() => context.CloseFieldset(new RadiosFieldsetContext(attributes: null))); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal("Fieldset has not been opened.", ex.Message); }
public async Task ProcessAsync_WithModelExpression_DeducesCheckedFromModelExpression(string modelValue, bool expectedChecked) { // Arrange var model = new Model() { Foo = modelValue }; var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), model) .GetExplorerForProperty(nameof(Model.Foo)); var viewContext = new ViewContext(); var modelExpression = nameof(Model.Foo); var radiosContext = new RadiosContext(name: "test", aspFor: new ModelExpression(modelExpression, modelExplorer)); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper() { Checked = null, Value = "bar" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Collection( radiosContext.Items, item => { var radiosItem = Assert.IsType <RadiosItem>(item); Assert.Equal(expectedChecked, radiosItem.Checked); }); }
public async Task ProcessAsync_CheckedNullAndModelValueDoesEqualsValueDoesNotSetCheckedAttribute() { // Arrange var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), "Foo"); var viewContext = new ViewContext(); var radiosContext = new RadiosContext( idPrefix: "prefix", resolvedName: "myradios", viewContext: viewContext, aspFor: new ModelExpression("Foo", modelExplorer)); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.AppendHtml(new HtmlString("Label")); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var htmlGenerator = new Mock <DefaultGovUkHtmlGenerator>() { CallBase = true }; htmlGenerator .Setup(mock => mock.GetModelValue(viewContext, modelExplorer, "Foo")) .Returns("bar"); var tagHelper = new RadiosItemTagHelper(htmlGenerator.Object) { Value = "baz" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.False(radiosContext.Items.OfType <RadiosItem>().Single().IsChecked); }
public async Task ProcessAsync_WithNullCollectionModelExpression_ExecutesSuccessfully() { // Arrange var model = new ModelWithCollectionProperty() { CollectionProperty = null }; var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(ModelWithCollectionProperty), model) .GetExplorerForProperty(nameof(ModelWithCollectionProperty.CollectionProperty)); var viewContext = new ViewContext(); var modelExpression = nameof(ModelWithCollectionProperty.CollectionProperty); var radiosContext = new RadiosContext(name: "test", aspFor: new ModelExpression(modelExpression, modelExplorer)); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper() { Value = "2" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Collection( radiosContext.Items, item => { var radiosItem = Assert.IsType <RadiosItem>(item); Assert.False(radiosItem.Checked); }); }
public void OpenFieldset_AlreadyOpen_ThrowsInvalidOperationException() { // Arrange var context = new RadiosContext(name: null, aspFor: null); context.OpenFieldset(); // Act var ex = Record.Exception(() => context.OpenFieldset()); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal("<govuk-radios-fieldset> cannot be nested inside another <govuk-radios-fieldset>.", ex.Message); }
public void OpenFieldset_AlreadyGotErrorMessage_ThrowsInvalidOperationException() { // Arrange var context = new RadiosContext(name: null, aspFor: null); context.SetErrorMessage(visuallyHiddenText: null, attributes: null, content: new HtmlString("Error")); // Act var ex = Record.Exception(() => context.OpenFieldset()); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal("<govuk-radios-fieldset> must be the only direct child of the <govuk-radios>.", ex.Message); }
public void OpenFieldset_AlreadyGotFieldset_ThrowsInvalidOperationException() { // Arrange var context = new RadiosContext(name: null, aspFor: null); context.OpenFieldset(); context.CloseFieldset(new RadiosFieldsetContext(attributes: null)); // Act var ex = Record.Exception(() => context.OpenFieldset()); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal <object>("Only one <govuk-radios-fieldset> element is permitted within each <govuk-radios>.", ex.Message); }
public async Task ProcessAsync_ComputesCorrectIdForSubsequentItemsWhenNotSpecified() { // Arrange var radiosContext = new RadiosContext( idPrefix: "prefix", resolvedName: "myradios", viewContext: null, aspFor: null); radiosContext.AddItem(new RadiosItemDivider() { Content = new HtmlString("Divider") }); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.AppendHtml(new HtmlString("Label")); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper(new DefaultGovUkHtmlGenerator()) { IsChecked = true, Value = "V" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Equal("prefix-1", radiosContext.Items.OfType <RadiosItem>().Single().Id); Assert.Equal("prefix-1-item-hint", radiosContext.Items.OfType <RadiosItem>().Single().HintId); Assert.Equal("conditional-prefix-1", radiosContext.Items.OfType <RadiosItem>().Single().ConditionalId); }
public async Task ProcessAsync_AddsItemToContext() { // Arrange var radiosContext = new RadiosContext(name: "test", aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper() { Checked = true, Disabled = true, Id = "id", Value = "value" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Collection( radiosContext.Items, item => { var radiosItem = Assert.IsType <RadiosItem>(item); Assert.True(radiosItem.Checked); Assert.True(radiosItem.Disabled); Assert.Equal("id", radiosItem.Id); Assert.Equal("value", radiosItem.Value); }); }
public async Task ProcessAsync_ConditionalContentSpecifiedSetsIsConditional() { // Arrange var radiosContext = new RadiosContext( idPrefix: "prefix", resolvedName: "myradios", viewContext: null, aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var itemContext = (RadiosItemContext)context.Items[typeof(RadiosItemContext)]; itemContext.SetConditional(attributes: null, new HtmlString("Conditional")); var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.AppendHtml(new HtmlString("Label")); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper(new DefaultGovUkHtmlGenerator()) { IsChecked = true, Value = "V" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.True(radiosContext.IsConditional); }
public void AddItem_AddsItemToItems() { // Arrange var context = new RadiosContext(name: null, aspFor: null); var item = new RadiosItem() { LabelContent = new HtmlString("Item 1"), Value = "item1" }; // Act context.AddItem(item); // Assert var contextItem = Assert.Single(context.Items); Assert.Same(item, contextItem); }
public async Task ProcessAsync_WithConditional_SetsConditionalOnContext() { // Arrange var radiosContext = new RadiosContext(name: "test", aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var itemContext = context.GetContextItem <RadiosItemContext>(); itemContext.SetConditional(attributes: null, content: new HtmlString("Conditional")); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper() { Value = "value" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Collection( radiosContext.Items, item => { var radiosItem = Assert.IsType <RadiosItem>(item); Assert.Equal("Conditional", radiosItem.Conditional.Content.RenderToString()); }); }
public async Task ProcessAsync_SetsFieldsetOnContext() { // Arrange var radiosContext = new RadiosContext( idPrefix: "prefix", resolvedName: "r", viewContext: null, aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-fieldset", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-fieldset", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var fieldsetContext = (RadiosFieldsetContext)context.Items[typeof(RadiosFieldsetContext)]; fieldsetContext.TrySetLegend(attributes: null, content: new HtmlString("Legend")); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosFieldsetTagHelper() { IsPageHeading = true }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.True(radiosContext.Fieldset.IsPageHeading); Assert.Equal("Legend", radiosContext.Fieldset.LegendContent.AsString()); }
public void OpenFieldset_AlreadyGotItem_ThrowsInvalidOperationException() { // Arrange var context = new RadiosContext(name: null, aspFor: null); var item = new RadiosItem() { LabelContent = new HtmlString("Item 1"), Value = "item1" }; context.AddItem(item); // Act var ex = Record.Exception(() => context.OpenFieldset()); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal("<govuk-radios-fieldset> must be the only direct child of the <govuk-radios>.", ex.Message); }
public void SetHint_AlreadyGotItem_ThrowsInvalidOperationException() { // Arrange var context = new RadiosContext(name: null, aspFor: null); var item = new RadiosItem() { LabelContent = new HtmlString("Item 1"), Value = "item1" }; context.AddItem(item); // Act var ex = Record.Exception(() => context.SetHint(attributes: null, new HtmlString("Hint"))); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal("<govuk-radios-hint> must be specified before <govuk-radios-item>.", ex.Message); }
public async Task ProcessAsync_ParentAlreadyHasFieldset_ThrowsInvalidOperationException() { // Arrange var radiosContext = new RadiosContext(name: null, aspFor: null); radiosContext.OpenFieldset(); var radiosFieldsetContext = new RadiosFieldsetContext(attributes: null); radiosFieldsetContext.SetLegend(isPageHeading: false, attributes: null, content: new HtmlString("Existing legend")); radiosContext.CloseFieldset(radiosFieldsetContext); var context = new TagHelperContext( tagName: "govuk-radios-fieldset", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-fieldset", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var fieldsetContext = context.GetContextItem <RadiosFieldsetContext>(); fieldsetContext.SetLegend(isPageHeading: true, attributes: null, content: new HtmlString("Legend")); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosFieldsetTagHelper(); // Act var ex = await Record.ExceptionAsync(() => tagHelper.ProcessAsync(context, output)); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal("Only one <govuk-radios-fieldset> element is permitted within each <govuk-radios>.", ex.Message); }
public async Task ProcessAsync_WithoutHint_DoesNotSetHintOnContext() { // Arrange var radiosContext = new RadiosContext(name: "test", aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemTagHelper() { Value = "value" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Collection( radiosContext.Items, item => { var radiosItem = Assert.IsType <RadiosItem>(item); Assert.Null(radiosItem.Hint); }); }
public async Task ProcessAsync_AddsDividerToContextItems() { // Arrange var radiosContext = new RadiosContext(name: null, aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-divider", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-divider", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.AppendHtml(new HtmlString("Divider")); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosItemDividerTagHelper(); // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Collection( radiosContext.Items, item => { var divider = Assert.IsType <RadiosItemDivider>(item); Assert.Equal("Divider", divider.Content.ToString()); }); }
public void AddItem_OutsideOfFieldset_ThrowsInvalidOperationException() { // Arrange var context = new RadiosContext(name: null, aspFor: null); var item = new RadiosItem() { LabelContent = new HtmlString("Item 1"), Value = "item1" }; context.OpenFieldset(); var fieldsetContext = new RadiosFieldsetContext(attributes: null); context.CloseFieldset(fieldsetContext); // Act var ex = Record.Exception(() => context.AddItem(item)); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal("<govuk-radios-item> must be inside <govuk-radios-fieldset>.", ex.Message); }
public async Task ProcessAsync_AddsItemToContext() { // Arrange var radiosContext = new RadiosContext( idPrefix: "prefix", resolvedName: "r", viewContext: null, aspFor: null); var context = new TagHelperContext( tagName: "govuk-radios-divider", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(RadiosContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-radios-divider", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.AppendHtml(new HtmlString("Divider")); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new RadiosDividerTagHelper(); // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Contains(radiosContext.Items, item => item is RadiosItemDivider d && d.Content.AsString() == "Divider"); }