Beispiel #1
0
        public async Task ProcessAsync_AddsFieldsetToContext()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(name: null, aspFor: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-fieldset",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-checkboxes-fieldset",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var fieldsetContext = context.GetContextItem <CheckboxesFieldsetContext>();
                fieldsetContext.SetLegend(isPageHeading: true, attributes: null, content: new HtmlString("Legend"));

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

            var tagHelper = new CheckboxesFieldsetTagHelper();

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

            // Assert
            Assert.True(checkboxesContext.Fieldset?.Legend?.IsPageHeading);
            Assert.Equal("Legend", checkboxesContext.Fieldset?.Legend?.Content?.RenderToString());
        }
Beispiel #2
0
        public async Task ProcessAsync_ValueNotSpecifiedThrowsNotSupportedException()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(
                idPrefix: "prefix",
                resolvedName: "r",
                aspFor: null,
                viewContext: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-checkboxes-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.AppendHtml(new HtmlString("Legend"));
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new CheckboxesItemTagHelper(new DefaultModelHelper());

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

            Assert.Equal("The 'value' attribute must be specified.", ex.Message);
        }
Beispiel #3
0
        public async Task ProcessAsync_NoNameButParentHasName_DoesNotThrowInvalidOperationException()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(name: "parent", aspFor: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

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

            var tagHelper = new CheckboxesItemTagHelper()
            {
                Value = "value"
            };

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

            // Assert
            Assert.Null(ex);
        }
Beispiel #4
0
        public async Task ProcessAsync_NoValue_ThrowsInvalidOperationException()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(name: "test", aspFor: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

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

            var tagHelper = new CheckboxesItemTagHelper();

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("The 'value' attribute must be specified.", ex.Message);
        }
Beispiel #5
0
        public async Task ProcessAsync_AddsItemToContext()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(
                idPrefix: "prefix",
                resolvedName: "mycheckboxes",
                aspFor: null,
                viewContext: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-checkboxes-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var itemContext = (CheckboxesItemContext)context.Items[typeof(CheckboxesItemContext)];
                itemContext.SetHint(attributes: null, content: new HtmlString("Hint"));
                itemContext.SetConditional(attributes: null, new HtmlString("Conditional"));

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

            var tagHelper = new CheckboxesItemTagHelper(new DefaultModelHelper())
            {
                IsChecked = true,
                Id        = "id",
                Value     = "V"
            };

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

            // Assert
            Assert.Contains(
                checkboxesContext.Items,
                item => item is CheckboxesItem i &&
                i.IsChecked &&
                !i.IsDisabled &&
                i.Content.AsString() == "Label" &&
                !i.IsDisabled &&
                i.Id == "id" &&
                i.Value == "V" &&
                i.ConditionalContent.AsString() == "Conditional" &&
                i.HintContent.AsString() == "Hint");
        }
Beispiel #6
0
        public async Task ProcessAsync_WithCollectionModelExpression_DeducesCheckedFromModelExpression(
            int[] modelValues,
            string itemValue,
            bool expectedChecked)
        {
            // Arrange
            var model = new ModelWithCollectionProperty()
            {
                CollectionProperty = modelValues
            };

            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(ModelWithCollectionProperty), model)
                                .GetExplorerForProperty(nameof(ModelWithCollectionProperty.CollectionProperty));
            var viewContext     = new ViewContext();
            var modelExpression = nameof(ModelWithCollectionProperty.CollectionProperty);

            var checkboxesContext = new CheckboxesContext(name: "test", aspFor: new ModelExpression(modelExpression, modelExplorer));

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

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

            var tagHelper = new CheckboxesItemTagHelper()
            {
                ViewContext = viewContext,
                Value       = itemValue
            };

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

            // Assert
            Assert.Collection(
                checkboxesContext.Items,
                item =>
            {
                var checkboxesItem = Assert.IsType <CheckboxesItem>(item);
                Assert.Equal(expectedChecked, checkboxesItem.Checked);
            });
        }
Beispiel #7
0
        public void SetLabel_ThrowsNotSupportedException()
        {
            // Arrange
            var context = new CheckboxesContext(name: null, aspFor: null);

            // Act
            var ex = Record.Exception(() => context.SetLabel(isPageHeading: false, attributes: null, content: null));

            // Assert
            Assert.IsType <NotSupportedException>(ex);
        }
Beispiel #8
0
        public async Task ProcessAsync_CheckedNullAndModelValueDoesEqualsValueDoesNotSetCheckedAttribute()
        {
            // Arrange
            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), "Foo");
            var viewContext   = new ViewContext();

            var radiosContext = new CheckboxesContext(
                idPrefix: "prefix",
                resolvedName: "mycheckboxes",
                viewContext: viewContext,
                aspFor: new ModelExpression("Foo", modelExplorer));

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), radiosContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-checkboxes-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.AppendHtml(new HtmlString("Label"));
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var htmlGenerator = new Mock <DefaultGovUkHtmlGenerator>()
            {
                CallBase = true
            };

            var modelHelperMock = new Mock <IModelHelper>();

            modelHelperMock
            .Setup(mock => mock.GetModelValue(viewContext, modelExplorer, "Foo"))
            .Returns("bar");

            var tagHelper = new CheckboxesItemTagHelper(modelHelperMock.Object)
            {
                Value = "baz"
            };

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

            // Assert
            Assert.False(radiosContext.Items.Single().IsChecked);
        }
Beispiel #9
0
        public void CloseFieldset_FieldsetNotOpened_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new CheckboxesContext(name: null, aspFor: null);

            // Act
            var ex = Record.Exception(() => context.CloseFieldset(new CheckboxesFieldsetContext(attributes: null)));

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Fieldset has not been opened.", ex.Message);
        }
Beispiel #10
0
        public void OpenFieldset_AlreadyOpen_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new CheckboxesContext(name: null, aspFor: null);

            context.OpenFieldset();

            // Act
            var ex = Record.Exception(() => context.OpenFieldset());

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("<govuk-checkboxes-fieldset> cannot be nested inside another <govuk-checkboxes-fieldset>.", ex.Message);
        }
Beispiel #11
0
        public void OpenFieldset_AlreadyGotErrorMessage_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new CheckboxesContext(name: null, aspFor: null);

            context.SetErrorMessage(visuallyHiddenText: null, attributes: null, content: new HtmlString("Error"));

            // Act
            var ex = Record.Exception(() => context.OpenFieldset());

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("<govuk-checkboxes-fieldset> must be the only direct child of the <govuk-checkboxes>.", ex.Message);
        }
Beispiel #12
0
        public void OpenFieldset_AlreadyGotFieldset_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new CheckboxesContext(name: null, aspFor: null);

            context.OpenFieldset();
            context.CloseFieldset(new CheckboxesFieldsetContext(attributes: null));

            // Act
            var ex = Record.Exception(() => context.OpenFieldset());

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal <object>("Only one <govuk-checkboxes-fieldset> element is permitted within each <govuk-checkboxes>.", ex.Message);
        }
Beispiel #13
0
        public async Task ProcessAsync_AddsItemToContext()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(name: "test", aspFor: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

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

            var tagHelper = new CheckboxesItemTagHelper()
            {
                Checked  = true,
                Disabled = true,
                Id       = "id",
                Name     = "name",
                Value    = "value"
            };

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

            // Assert
            Assert.Collection(
                checkboxesContext.Items,
                item =>
            {
                var checkboxesItem = Assert.IsType <CheckboxesItem>(item);
                Assert.True(checkboxesItem.Checked);
                Assert.True(checkboxesItem.Disabled);
                Assert.Equal("id", checkboxesItem.Id);
                Assert.Equal("name", checkboxesItem.Name);
                Assert.Equal("value", checkboxesItem.Value);
            });
        }
Beispiel #14
0
        public async Task ProcessAsync_ComputesCorrectIdForSubsequentItemsWhenNotSpecified()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(
                idPrefix: "prefix",
                resolvedName: "mycheckboxes",
                aspFor: null,
                viewContext: null);

            checkboxesContext.AddItem(new CheckboxesItem()
            {
                Content = new HtmlString("First")
            });

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-checkboxes-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.AppendHtml(new HtmlString("Label"));
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new CheckboxesItemTagHelper(new DefaultModelHelper())
            {
                IsChecked = true,
                Value     = "V"
            };

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

            // Assert
            Assert.Equal("prefix-1", checkboxesContext.Items.Last().Id);
            Assert.Equal("prefix-1-item-hint", checkboxesContext.Items.Last().HintId);
            Assert.Equal("conditional-prefix-1", checkboxesContext.Items.Last().ConditionalId);
        }
Beispiel #15
0
        public async Task ProcessAsync_WithConditional_SetsConditionalOnContext()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(name: "test", aspFor: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-checkboxes-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var itemContext = context.GetContextItem <CheckboxesItemContext>();
                itemContext.SetConditional(attributes: null, content: new HtmlString("Conditional"));

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

            var tagHelper = new CheckboxesItemTagHelper()
            {
                Name  = "name",
                Value = "value"
            };

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

            // Assert
            Assert.Collection(
                checkboxesContext.Items,
                item =>
            {
                var checkboxesItem = Assert.IsType <CheckboxesItem>(item);
                Assert.Equal("Conditional", checkboxesItem.Conditional.Content.RenderToString());
            });
        }
        public async Task ProcessAsync_ConditionalContentSpecifiedSetsIsConditional()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(
                idPrefix: "prefix",
                resolvedName: "mycheckboxes",
                aspFor: null,
                viewContext: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-checkboxes-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var itemContext = (CheckboxesItemContext)context.Items[typeof(CheckboxesItemContext)];
                itemContext.SetConditional(attributes: null, new HtmlString("Conditional"));

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

            var tagHelper = new CheckboxesItemTagHelper(new DefaultGovUkHtmlGenerator())
            {
                IsChecked = true,
                Value     = "V"
            };

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

            // Assert
            Assert.True(checkboxesContext.IsConditional);
        }
Beispiel #17
0
        public void AddItem_AddsItemToItems()
        {
            // Arrange
            var context = new CheckboxesContext(name: null, aspFor: null);

            var item = new CheckboxesItem()
            {
                LabelContent = new HtmlString("Item 1"),
                Value        = "item1"
            };

            // Act
            context.AddItem(item);

            // Assert
            var contextItem = Assert.Single(context.Items);

            Assert.Same(item, contextItem);
        }
Beispiel #18
0
        public async Task ProcessAsync_SetsFieldsetOnContext()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(
                idPrefix: "prefix",
                resolvedName: "r",
                aspFor: null,
                viewContext: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-fieldset",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-checkboxes-fieldset",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var fieldsetContext = (CheckboxesFieldsetContext)context.Items[typeof(CheckboxesFieldsetContext)];
                fieldsetContext.TrySetLegend(
                    isPageHeading: true,
                    attributes: null,
                    content: new HtmlString("Legend"));

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

            var tagHelper = new CheckboxesFieldsetTagHelper();

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

            // Assert
            Assert.True(checkboxesContext.Fieldset.LegendIsPageHeading);
            Assert.Equal("Legend", checkboxesContext.Fieldset.LegendContent.AsString());
        }
Beispiel #19
0
        public void SetHint_AlreadyGotItem_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new CheckboxesContext(name: null, aspFor: null);

            var item = new CheckboxesItem()
            {
                LabelContent = new HtmlString("Item 1"),
                Value        = "item1"
            };

            context.AddItem(item);

            // Act
            var ex = Record.Exception(() => context.SetHint(attributes: null, new HtmlString("Hint")));

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("<govuk-checkboxes-hint> must be specified before <govuk-checkboxes-item>.", ex.Message);
        }
Beispiel #20
0
        public void OpenFieldset_AlreadyGotItem_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new CheckboxesContext(name: null, aspFor: null);

            var item = new CheckboxesItem()
            {
                LabelContent = new HtmlString("Item 1"),
                Value        = "item1"
            };

            context.AddItem(item);

            // Act
            var ex = Record.Exception(() => context.OpenFieldset());

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("<govuk-checkboxes-fieldset> must be the only direct child of the <govuk-checkboxes>.", ex.Message);
        }
Beispiel #21
0
        public async Task ProcessAsync_WithoutHint_DoesNotSetHintOnContext()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(name: "test", aspFor: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-item",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

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

            var tagHelper = new CheckboxesItemTagHelper()
            {
                Name  = "name",
                Value = "value"
            };

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

            // Assert
            Assert.Collection(
                checkboxesContext.Items,
                item =>
            {
                var checkboxesItem = Assert.IsType <CheckboxesItem>(item);
                Assert.Null(checkboxesItem.Hint);
            });
        }
Beispiel #22
0
        public async Task ProcessAsync_ParentAlreadyHasFieldset_ThrowsInvalidOperationException()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(name: null, aspFor: null);

            checkboxesContext.OpenFieldset();
            var checkboxesFieldsetContext = new CheckboxesFieldsetContext(attributes: null);

            checkboxesFieldsetContext.SetLegend(isPageHeading: false, attributes: null, content: new HtmlString("Existing legend"));
            checkboxesContext.CloseFieldset(checkboxesFieldsetContext);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-fieldset",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-checkboxes-fieldset",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var fieldsetContext = context.GetContextItem <CheckboxesFieldsetContext>();
                fieldsetContext.SetLegend(isPageHeading: true, attributes: null, content: new HtmlString("Legend"));

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

            var tagHelper = new CheckboxesFieldsetTagHelper();

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Only one <govuk-checkboxes-fieldset> element is permitted within each <govuk-checkboxes>.", ex.Message);
        }
Beispiel #23
0
        public async Task ProcessAsync_AddsDividerToContextItems()
        {
            // Arrange
            var checkboxesContext = new CheckboxesContext(name: null, aspFor: null);

            var context = new TagHelperContext(
                tagName: "govuk-checkboxes-divider",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(CheckboxesContext), checkboxesContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-checkboxes-divider",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.AppendHtml(new HtmlString("Divider"));
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new CheckboxesItemDividerTagHelper();

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

            // Assert
            Assert.Collection(
                checkboxesContext.Items,
                item =>
            {
                var divider = Assert.IsType <CheckboxesItemDivider>(item);
                Assert.Equal("Divider", divider.Content.ToString());
            });
        }
Beispiel #24
0
        public void AddItem_OutsideOfFieldset_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new CheckboxesContext(name: null, aspFor: null);

            var item = new CheckboxesItem()
            {
                LabelContent = new HtmlString("Item 1"),
                Value        = "item1"
            };

            context.OpenFieldset();
            var fieldsetContext = new CheckboxesFieldsetContext(attributes: null);

            context.CloseFieldset(fieldsetContext);

            // Act
            var ex = Record.Exception(() => context.AddItem(item));

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("<govuk-checkboxes-item> must be inside <govuk-checkboxes-fieldset>.", ex.Message);
        }