public async Task ProcessAsync_InvalidLevelThrowsInvalidOperationException(int level) { // Arrange var context = new TagHelperContext( tagName: "govuk-accordion", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-accordion", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var accordionContext = (AccordionContext)context.Items[typeof(AccordionContext)]; accordionContext.AddItem(new AccordionItem() { Content = new HtmlString("First content"), Expanded = false, HeadingContent = new HtmlString("First heading"), SummaryContent = new HtmlString("First summary") }); accordionContext.AddItem(new AccordionItem() { Content = new HtmlString("First content"), Expanded = true, HeadingContent = new HtmlString("Second heading") }); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new AccordionTagHelper(new DefaultGovUkHtmlGenerator()) { Id = "testaccordion", HeadingLevel = level }; // Act & Assert var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => tagHelper.ProcessAsync(context, output)); Assert.Equal("The 'heading-level' attribute must be between 1 and 6.", ex.Message); }
public async Task ProcessAsync_NoId_ThrowsInvalidOperationException() { // Arrange var context = new TagHelperContext( tagName: "govuk-accordion", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-accordion", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var accordionContext = (AccordionContext)context.Items[typeof(AccordionContext)]; accordionContext.AddItem(new AccordionItem() { Content = new HtmlString("First content"), Expanded = false, HeadingContent = new HtmlString("First heading"), SummaryContent = new HtmlString("First summary") }); accordionContext.AddItem(new AccordionItem() { Content = new HtmlString("First content"), Expanded = true, HeadingContent = new HtmlString("Second heading") }); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new AccordionTagHelper(new ComponentGenerator()); // Act var ex = await Record.ExceptionAsync(() => tagHelper.ProcessAsync(context, output)); // Assert Assert.IsType <InvalidOperationException>(ex); Assert.Equal("The 'id' attribute must be specified.", ex.Message); }
public async Task ProcessAsync_GeneratesExpectedOutput() { // Arrange var context = new TagHelperContext( tagName: "govuk-accordion", allAttributes: new TagHelperAttributeList(), items: new Dictionary <object, object>(), uniqueId: "test"); var output = new TagHelperOutput( "govuk-accordion", attributes: new TagHelperAttributeList(), getChildContentAsync: (useCachedResult, encoder) => { var accordionContext = (AccordionContext)context.Items[typeof(AccordionContext)]; accordionContext.AddItem(new AccordionItem() { Content = new HtmlString("First content"), Expanded = false, HeadingContent = new HtmlString("First heading"), SummaryContent = new HtmlString("First summary") }); accordionContext.AddItem(new AccordionItem() { Content = new HtmlString("First content"), Expanded = true, HeadingContent = new HtmlString("Second heading") }); var tagHelperContent = new DefaultTagHelperContent(); return(Task.FromResult <TagHelperContent>(tagHelperContent)); }); var tagHelper = new AccordionTagHelper(new DefaultGovUkHtmlGenerator()) { Id = "testaccordion", HeadingLevel = 1, }; // Act await tagHelper.ProcessAsync(context, output); // Assert Assert.Equal( "<div class=\"govuk-accordion\" data-module=\"govuk-accordion\" id=\"testaccordion\">" + "<div class=\"govuk-accordion__section\">" + "<div class=\"govuk-accordion__section-header\">" + "<h1 class=\"govuk-accordion__section-heading\">" + "<span class=\"govuk-accordion__section-button\" id=\"testaccordion-heading-0\">First heading</span>" + "</h1>" + "<div class=\"govuk-body govuk-accordion__section-summary\" id=\"testaccordion-summary-0\">" + "</div>" + "</div>" + "<div aria-labelledby=\"testaccordion-heading-0\" class=\"govuk-accordion__section-content\" id=\"testaccordion-content-0\">" + "First content" + "</div>" + "</div>" + "<div class=\"govuk-accordion__section--expanded govuk-accordion__section\">" + "<div class=\"govuk-accordion__section-header\">" + "<h1 class=\"govuk-accordion__section-heading\">" + "<span class=\"govuk-accordion__section-button\" id=\"testaccordion-heading-1\">Second heading</span>" + "</h1>" + "</div>" + "<div aria-labelledby=\"testaccordion-heading-1\" class=\"govuk-accordion__section-content\" id=\"testaccordion-content-1\">" + "First content" + "</div>" + "</div>" + "</div>", output.AsString()); }
/// <summary> /// 测试初始化 /// </summary> public AccordionTagHelperTest(ITestOutputHelper output) { _output = output; _component = new AccordionTagHelper(); }