public async Task ProcessAsync_NoNameButParentHasName_DoesNotThrowInvalidOperationException() { // Arrange var checkboxesContext = new CheckboxesContext(name: "parent", aspFor: null); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), checkboxesContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new CheckboxesItemTagHelper() { Value = "value" }; // Act var ex = await Record.ExceptionAsync(() => tagHelper.ProcessAsync(context, output)); // Assert Assert.Null(ex); }
public async Task ProcessAsync_NoValue_ThrowsInvalidOperationException() { // Arrange var checkboxesContext = new CheckboxesContext(name: "test", aspFor: null); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), checkboxesContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new CheckboxesItemTagHelper(); // 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_ValueNotSpecifiedThrowsNotSupportedException() { // Arrange var checkboxesContext = new CheckboxesContext( idPrefix: "prefix", resolvedName: "r", aspFor: null, viewContext: null); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), checkboxesContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.AppendHtml(new HtmlString("Legend")); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new CheckboxesItemTagHelper(new DefaultModelHelper()); // 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_AddsItemToContext() { // Arrange var checkboxesContext = new CheckboxesContext( idPrefix: "prefix", resolvedName: "mycheckboxes", aspFor: null, viewContext: null); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), checkboxesContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var itemContext = (CheckboxesItemContext)context.Items[typeof(CheckboxesItemContext)]; 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 CheckboxesItemTagHelper(new DefaultModelHelper()) { IsChecked = true, Id = "id", Value = "V" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Contains( checkboxesContext.Items, item => item is CheckboxesItem 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 async Task ProcessAsync_WithCollectionModelExpression_DeducesCheckedFromModelExpression( int[] modelValues, string itemValue, bool expectedChecked) { // Arrange var model = new ModelWithCollectionProperty() { CollectionProperty = modelValues }; var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(ModelWithCollectionProperty), model) .GetExplorerForProperty(nameof(ModelWithCollectionProperty.CollectionProperty)); var viewContext = new ViewContext(); var modelExpression = nameof(ModelWithCollectionProperty.CollectionProperty); var checkboxesContext = new CheckboxesContext(name: "test", aspFor: new ModelExpression(modelExpression, modelExplorer)); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), checkboxesContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new CheckboxesItemTagHelper() { ViewContext = viewContext, Value = itemValue }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Collection( checkboxesContext.Items, item => { var checkboxesItem = Assert.IsType <CheckboxesItem>(item); Assert.Equal(expectedChecked, checkboxesItem.Checked); }); }
public async Task ProcessAsync_CheckedNullAndModelValueDoesEqualsValueDoesNotSetCheckedAttribute() { // Arrange var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), "Foo"); var viewContext = new ViewContext(); var radiosContext = new CheckboxesContext( idPrefix: "prefix", resolvedName: "mycheckboxes", viewContext: viewContext, aspFor: new ModelExpression("Foo", modelExplorer)); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), radiosContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-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 }; var modelHelperMock = new Mock <IModelHelper>(); modelHelperMock .Setup(mock => mock.GetModelValue(viewContext, modelExplorer, "Foo")) .Returns("bar"); var tagHelper = new CheckboxesItemTagHelper(modelHelperMock.Object) { Value = "baz" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.False(radiosContext.Items.Single().IsChecked); }
public async Task ProcessAsync_AddsItemToContext() { // Arrange var checkboxesContext = new CheckboxesContext(name: "test", aspFor: null); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), checkboxesContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new CheckboxesItemTagHelper() { Checked = true, Disabled = true, Id = "id", Name = "name", Value = "value" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Collection( checkboxesContext.Items, item => { var checkboxesItem = Assert.IsType <CheckboxesItem>(item); Assert.True(checkboxesItem.Checked); Assert.True(checkboxesItem.Disabled); Assert.Equal("id", checkboxesItem.Id); Assert.Equal("name", checkboxesItem.Name); Assert.Equal("value", checkboxesItem.Value); }); }
public async Task ProcessAsync_ComputesCorrectIdForSubsequentItemsWhenNotSpecified() { // Arrange var checkboxesContext = new CheckboxesContext( idPrefix: "prefix", resolvedName: "mycheckboxes", aspFor: null, viewContext: null); checkboxesContext.AddItem(new CheckboxesItem() { Content = new HtmlString("First") }); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), checkboxesContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.AppendHtml(new HtmlString("Label")); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new CheckboxesItemTagHelper(new DefaultModelHelper()) { IsChecked = true, Value = "V" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Equal("prefix-1", checkboxesContext.Items.Last().Id); Assert.Equal("prefix-1-item-hint", checkboxesContext.Items.Last().HintId); Assert.Equal("conditional-prefix-1", checkboxesContext.Items.Last().ConditionalId); }
public async Task ProcessAsync_WithConditional_SetsConditionalOnContext() { // Arrange var checkboxesContext = new CheckboxesContext(name: "test", aspFor: null); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), checkboxesContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var itemContext = context.GetContextItem <CheckboxesItemContext>(); itemContext.SetConditional(attributes: null, content: new HtmlString("Conditional")); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new CheckboxesItemTagHelper() { Name = "name", Value = "value" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Collection( checkboxesContext.Items, item => { var checkboxesItem = Assert.IsType <CheckboxesItem>(item); Assert.Equal("Conditional", checkboxesItem.Conditional.Content.RenderToString()); }); }
public async Task ProcessAsync_ConditionalContentSpecifiedSetsIsConditional() { // Arrange var checkboxesContext = new CheckboxesContext( idPrefix: "prefix", resolvedName: "mycheckboxes", aspFor: null, viewContext: null); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), checkboxesContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var itemContext = (CheckboxesItemContext)context.Items[typeof(CheckboxesItemContext)]; itemContext.SetConditional(attributes: null, new HtmlString("Conditional")); var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.AppendHtml(new HtmlString("Label")); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new CheckboxesItemTagHelper(new DefaultGovUkHtmlGenerator()) { IsChecked = true, Value = "V" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.True(checkboxesContext.IsConditional); }
public async Task ProcessAsync_WithoutHint_DoesNotSetHintOnContext() { // Arrange var checkboxesContext = new CheckboxesContext(name: "test", aspFor: null); var context = new TagHelperContext( tagName: "govuk-checkboxes-item", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>() { { typeof(CheckboxesContext), checkboxesContext } }, uniqueId: "test"); var output = new TagHelperOutput( "govuk-checkboxes-item", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new CheckboxesItemTagHelper() { Name = "name", Value = "value" }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Collection( checkboxesContext.Items, item => { var checkboxesItem = Assert.IsType <CheckboxesItem>(item); Assert.Null(checkboxesItem.Hint); }); }