public async Task ProcessAsync_TagAlreadySpecifiedThrowsInvalidOperationException()
        {
            // Arrange
            var pbContext = new PhaseBannerContext();

            pbContext.TrySetTag(attributes: null, content: new HtmlString("Existing tag"));

            var context = new TagHelperContext(
                tagName: "govuk-phase-banner-tag",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(PhaseBannerContext), pbContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-phase-banner-tag",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Legend message");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new PhaseBannerTagTagHelper();

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

            Assert.Equal("Cannot render <govuk-phase-banner-tag> here.", ex.Message);
        }
        public async Task ProcessAsync_AddsToContext()
        {
            // Arrange
            var pbContext = new PhaseBannerContext();

            var context = new TagHelperContext(
                tagName: "govuk-phase-banner-tag",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(PhaseBannerContext), pbContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-phase-banner-tag",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Legend message");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new PhaseBannerTagTagHelper();

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

            // Assert
            Assert.Equal("Legend message", pbContext.Tag?.content.AsString());
        }
Пример #3
0
        public async Task ProcessAsync_TagAlreadySpecified_ThrowsInvalidOperationException()
        {
            // Arrange
            var phaseBannerContext = new PhaseBannerContext();

            phaseBannerContext.SetTag(new AttributeDictionary(), content: new HtmlString("Existing tag"));

            var context = new TagHelperContext(
                tagName: "govuk-phase-banner-tag",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(PhaseBannerContext), phaseBannerContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-phase-banner-tag",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Legend message");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new PhaseBannerTagTagHelper();

            // Act
            var ex = await Record.ExceptionAsync(() => tagHelper.ProcessAsync(context, output));

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Only one <govuk-phase-banner-tag> element is permitted within each <govuk-phase-banner>.", ex.Message);
        }