public async Task ProcessAsync_RowIsMissingKey_ThrowsInvalidOperationException()
        {
            // Arrange
            var summaryListContext = new SummaryListContext();

            var context = new TagHelperContext(
                tagName: "govuk-summary-list-row",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(SummaryListContext), summaryListContext }
            },
                uniqueId: "test");

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

            var tagHelper = new SummaryListRowTagHelper();

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("A <govuk-summary-list-row-key> element must be provided.", ex.Message);
        }
Пример #2
0
        public async Task ProcessAsync_AddsValueToContext()
        {
            // Arrange
            var summaryListContext = new SummaryListContext();

            var rowContext = new SummaryListRowContext();

            var context = new TagHelperContext(
                tagName: "govuk-summary-list-row-key",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(SummaryListContext), summaryListContext },
                { typeof(SummaryListRowContext), rowContext }
            },
                uniqueId: "test");

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

            var tagHelper = new SummaryListRowKeyTagHelper();

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

            // Assert
            Assert.Equal("Key content", rowContext.Key.Value.Content.RenderToString());
        }
Пример #3
0
        public async Task ProcessAsync_RowAlreadyHasValueThrowsInvalidOperationException()
        {
            // Arrange
            var summaryListContext = new SummaryListContext();
            var rowContext         = new SummaryListRowContext();

            rowContext.TrySetValue(new HtmlString("Existing value"));

            var context = new TagHelperContext(
                tagName: "govuk-summary-list-row-value",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(SummaryListContext), summaryListContext },
                { typeof(SummaryListRowContext), rowContext }
            },
                uniqueId: "test");

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

            var tagHelper = new SummaryListRowValueTagHelper();

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

            Assert.Equal("Cannot render <govuk-summary-list-row-value> here.", ex.Message);
        }
Пример #4
0
        public async Task ProcessAsync_AddsRowToContext()
        {
            // Arrange
            var summaryListContext = new SummaryListContext();

            var context = new TagHelperContext(
                tagName: "govuk-summary-list-row",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(SummaryListContext), summaryListContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-summary-list-row",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var rowContext = (SummaryListRowContext)context.Items[typeof(SummaryListRowContext)];
                rowContext.TrySetKey(new HtmlString("Key"));
                rowContext.TrySetValue(new HtmlString("Value"));
                rowContext.AddAction(new SummaryListRowAction()
                {
                    Content            = new HtmlString("First action"),
                    Href               = "first",
                    VisuallyHiddenText = "vht1"
                });
                rowContext.AddAction(new SummaryListRowAction()
                {
                    Content            = new HtmlString("Second action"),
                    Href               = "second",
                    VisuallyHiddenText = "vht2"
                });

                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new SummaryListRowTagHelper();

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

            // Assert
            Assert.Equal(1, summaryListContext.Rows.Count);

            var firstRow = summaryListContext.Rows.First();

            Assert.Equal("Key", firstRow.Key.AsString());
            Assert.Equal("Value", firstRow.Value.AsString());
            Assert.Equal(2, firstRow.Actions.Count());
            Assert.Equal("First action", firstRow.Actions.First().Content.AsString());
            Assert.Equal("first", firstRow.Actions.First().Href);
            Assert.Equal("vht1", firstRow.Actions.First().VisuallyHiddenText);
            Assert.Equal("Second action", firstRow.Actions.Skip(1).First().Content.AsString());
            Assert.Equal("second", firstRow.Actions.Skip(1).First().Href);
            Assert.Equal("vht2", firstRow.Actions.Skip(1).First().VisuallyHiddenText);
        }
Пример #5
0
        public async Task ProcessAsync_AddsActionToContext()
        {
            // Arrange
            var summaryListContext = new SummaryListContext();

            var rowContext = new SummaryListRowContext();

            var context = new TagHelperContext(
                tagName: "govuk-summary-list-row-action",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(SummaryListContext), summaryListContext },
                { typeof(SummaryListRowContext), rowContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-summary-list-row-actions",
                attributes: new TagHelperAttributeList()
            {
                { "href", "#" }
            },
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Change");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new SummaryListRowActionTagHelper()
            {
                VisuallyHiddenText = "vht"
            };

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

            // Assert
            Assert.Collection(
                rowContext.Actions,
                action =>
            {
                Assert.Equal("Change", action.Content.RenderToString());
                Assert.Equal("vht", action.VisuallyHiddenText);

                Assert.Collection(
                    action.Attributes,
                    kvp =>
                {
                    Assert.Equal("href", kvp.Key);
                    Assert.Equal("#", kvp.Value);
                });
            });
        }
Пример #6
0
        public async Task ProcessAsync_AddsActionToContext()
        {
            // Arrange
            var summaryListContext = new SummaryListContext();
            var rowContext         = new SummaryListRowContext();

            var context = new TagHelperContext(
                tagName: "govuk-summary-list-row-action",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(SummaryListContext), summaryListContext },
                { typeof(SummaryListRowContext), rowContext }
            },
                uniqueId: "test");

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

            var tagHelper = new SummaryListRowActionTagHelper(
                new DefaultGovUkHtmlGenerator(),
                Mock.Of <IUrlHelperFactory>())
            {
                Href = "href",
                VisuallyHiddenText = "vht"
            };

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

            // Assert
            Assert.Equal(1, rowContext.Actions.Count);

            var firstAction = rowContext.Actions.First();

            Assert.Equal("vht", firstAction.VisuallyHiddenText);
            Assert.Equal("href", firstAction.Href);
            Assert.Equal("Action content", firstAction.Content.AsString());
        }
        public async Task ProcessAsync_AddsAttributesToContext()
        {
            // Arrange
            var summaryListContext = new SummaryListContext();

            var rowContext = new SummaryListRowContext();

            var context = new TagHelperContext(
                tagName: "govuk-summary-list-row-actions",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(SummaryListContext), summaryListContext },
                { typeof(SummaryListRowContext), rowContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-summary-list-row-actions",
                attributes: new TagHelperAttributeList()
            {
                { "class", "additional-class" }
            },
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new SummaryListRowActionsTagHelper();

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

            // Assert
            Assert.Collection(
                rowContext.ActionsAttributes,
                kvp =>
            {
                Assert.Equal("class", kvp.Key);
                Assert.Equal("additional-class", kvp.Value);
            });
        }
        public async Task ProcessAsync_ParentAlreadyHasAction_ThrowsInvalidOperationException()
        {
            // Arrange
            var summaryListContext = new SummaryListContext();

            var rowContext = new SummaryListRowContext();

            rowContext.AddAction(new HtmlGeneration.SummaryListRowAction()
            {
                Content = new HtmlString("Action")
            });

            var context = new TagHelperContext(
                tagName: "govuk-summary-list-row-actions",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(SummaryListContext), summaryListContext },
                { typeof(SummaryListRowContext), rowContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-summary-list-row-actions",
                attributes: new TagHelperAttributeList()
            {
                { "class", "additional-class" }
            },
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new SummaryListRowActionsTagHelper();

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("<govuk-summary-list-row-actions> must be specified before <govuk-summary-list-row-action>.", ex.Message);
        }
Пример #9
0
        public async Task ProcessAsync_ParentAlreadyHasKey_ThrowsInvalidOperationException()
        {
            // Arrange
            var summaryListContext = new SummaryListContext();

            var rowContext = new SummaryListRowContext();

            rowContext.SetKey(new AttributeDictionary(), new HtmlString("Key"));

            var context = new TagHelperContext(
                tagName: "govuk-summary-list-row-key",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(SummaryListContext), summaryListContext },
                { typeof(SummaryListRowContext), rowContext }
            },
                uniqueId: "test");

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

            var tagHelper = new SummaryListRowKeyTagHelper();

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Only one <govuk-summary-list-row-key> element is permitted within each <govuk-summary-list-row>.", ex.Message);
        }
        public async Task ProcessAsync_AddsRowToContext()
        {
            // Arrange
            var summaryListContext = new SummaryListContext();

            var context = new TagHelperContext(
                tagName: "govuk-summary-list-row",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(SummaryListContext), summaryListContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-summary-list-row",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var rowContext = (SummaryListRowContext)context.Items[typeof(SummaryListRowContext)];
                rowContext.SetKey(new AttributeDictionary(), new HtmlString("Key"));
                rowContext.SetValue(new AttributeDictionary(), new HtmlString("Value"));
                rowContext.AddAction(new SummaryListRowAction()
                {
                    Attributes = new AttributeDictionary()
                    {
                        { "href", "first" }
                    },
                    Content            = new HtmlString("First action"),
                    VisuallyHiddenText = "vht1"
                });
                rowContext.AddAction(new SummaryListRowAction()
                {
                    Attributes = new AttributeDictionary()
                    {
                        { "href", "second" }
                    },
                    Content            = new HtmlString("Second action"),
                    VisuallyHiddenText = "vht2"
                });

                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new SummaryListRowTagHelper();

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

            // Assert
            Assert.Equal(1, summaryListContext.Rows.Count);

            Assert.Collection(
                summaryListContext.Rows,
                row =>
            {
                Assert.Equal("Key", row.Key.Content.RenderToString());
                Assert.Equal("Value", row.Value.Content.RenderToString());

                Assert.Collection(
                    row.Actions.Items,
                    action =>
                {
                    Assert.Equal("First action", action.Content.RenderToString());
                    Assert.Contains(action.Attributes, kvp => kvp.Key == "href" && kvp.Value == "first");
                    Assert.Equal("vht1", action.VisuallyHiddenText);
                },
                    action =>
                {
                    Assert.Equal("Second action", action.Content.RenderToString());
                    Assert.Contains(action.Attributes, kvp => kvp.Key == "href" && kvp.Value == "second");
                    Assert.Equal("vht2", action.VisuallyHiddenText);
                });
            });
        }