public async Task ProcessAsync_AddsSummaryToContext()
        {
            // Arrange
            var accordionContext = new AccordionContext();
            var itemContext      = new AccordionItemContext();

            var context = new TagHelperContext(
                tagName: "govuk-accordion-item-summary",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(AccordionContext), accordionContext },
                { typeof(AccordionItemContext), itemContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-accordion-item-summary",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Summary content");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new AccordionItemSummaryTagHelper();

            // Act
            await tagHelper.ProcessAsync(context, output);

            // Assert
            Assert.NotNull(itemContext.Summary);
            Assert.Equal("Summary content", itemContext.Summary?.Content.RenderToString());
        }
Esempio n. 2
0
        public async Task ProcessAsync_ItemAlreadyHasSummaryThrowsInvalidOperationException()
        {
            // Arrange
            var accordionContext = new AccordionContext();
            var itemContext      = new AccordionItemContext();

            itemContext.TrySetSummary(attributes: null, new HtmlString("Existing summary"));

            var context = new TagHelperContext(
                tagName: "govuk-accordion-item-summary",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(AccordionContext), accordionContext },
                { typeof(AccordionItemContext), itemContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-accordion-item-summary",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Summary content");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new AccordionItemSummaryTagHelper();

            // Act & Assert
            var ex = await Assert.ThrowsAsync <InvalidOperationException>(() => tagHelper.ProcessAsync(context, output));

            Assert.Equal("Cannot render <govuk-accordion-item-summary> here.", ex.Message);
        }