public async Task ProcessAsync_DoesNotUseViewContext_IfSelectedNotNull( string originalContent, string selected, string value, ICollection <string> _, TagHelperOutput __) { // Arrange var originalAttributes = new TagHelperAttributeList { { "label", "my-label" }, { "selected", selected }, }; var originalTagName = "not-option"; var contextAttributes = new TagHelperAttributeList { { "label", "my-label" }, { "selected", selected }, { "value", value }, }; var originalPreContent = "original pre-content"; var originalPostContent = "original post-content"; var tagHelperContext = new TagHelperContext( tagName: "option", allAttributes: contextAttributes, items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( originalTagName, originalAttributes, getChildContentAsync: (useCachedResult, encoder) => { var tagHelperContent = new DefaultTagHelperContent(); tagHelperContent.SetContent(originalContent); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }) { TagMode = TagMode.StartTagAndEndTag, }; output.PreContent.SetContent(originalPreContent); output.Content.SetContent(originalContent); output.PostContent.SetContent(originalPostContent); var metadataProvider = new EmptyModelMetadataProvider(); var htmlGenerator = new TestableHtmlGenerator(metadataProvider); var tagHelper = new OptionTagHelper(htmlGenerator) { Value = value, }; // Act & Assert (does not throw) // Tag helper would throw an NRE if it used ViewContext or Generator values. await tagHelper.ProcessAsync(tagHelperContext, output); }
public async Task ProcessAsync_GeneratesExpectedOutput( string originalContent, string selected, string value, ICollection <string> currentValues, TagHelperOutput expectedTagHelperOutput) { // Arrange var originalAttributes = new TagHelperAttributeList { { "label", "my-label" }, }; if (selected != null) { originalAttributes.Add("selected", selected); } var contextAttributes = new TagHelperAttributeList(originalAttributes); if (value != null) { contextAttributes.Add("value", value); } var tagHelperContext = new TagHelperContext( tagName: "option", allAttributes: contextAttributes, items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( expectedTagHelperOutput.TagName, originalAttributes, getChildContentAsync: (useCachedResult, encoder) => { // GetChildContentAsync should not be invoked since we are setting the content below. Assert.True(false); return(Task.FromResult <TagHelperContent>(null)); }) { TagMode = TagMode.StartTagAndEndTag }; output.Content.SetContent(originalContent); var metadataProvider = new EmptyModelMetadataProvider(); var htmlGenerator = new TestableHtmlGenerator(metadataProvider); var viewContext = TestableHtmlGenerator.GetViewContext( model: null, htmlGenerator: htmlGenerator, metadataProvider: metadataProvider); tagHelperContext.Items[typeof(SelectTagHelper)] = currentValues == null ? null : new CurrentValues(currentValues); var tagHelper = new OptionTagHelper(htmlGenerator) { Value = value, ViewContext = viewContext, }; // Act await tagHelper.ProcessAsync(tagHelperContext, output); // Assert Assert.Equal(expectedTagHelperOutput.TagName, output.TagName); Assert.Equal(expectedTagHelperOutput.Content.GetContent(), output.Content.GetContent()); Assert.Equal(expectedTagHelperOutput.Attributes.Count, output.Attributes.Count); foreach (var attribute in output.Attributes) { Assert.Contains(attribute, expectedTagHelperOutput.Attributes); } }