Ejemplo n.º 1
0
        public async Task ProcessAsync_NoValue_ThrowsInvalidOperationException()
        {
            // Arrange
            var radiosContext = new RadiosContext(name: "test", aspFor: null);

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

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

            var tagHelper = new RadiosItemTagHelper();

            // 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);
        }
Ejemplo n.º 2
0
        public async Task ProcessAsync_NoNameButParentHasName_DoesNotThrowInvalidOperationException()
        {
            // Arrange
            var radiosContext = new RadiosContext(name: "parent", aspFor: null);

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

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

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

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

            // Assert
            Assert.Null(ex);
        }
Ejemplo n.º 3
0
        public async Task ProcessAsync_ValueNotSpecifiedThrowsNotSupportedException()
        {
            // Arrange
            var radiosContext = new RadiosContext(
                idPrefix: "prefix",
                resolvedName: "r",
                viewContext: null,
                aspFor: null);

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

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

            var tagHelper = new RadiosItemTagHelper(new DefaultGovUkHtmlGenerator());

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

            Assert.Equal("The 'value' attribute must be specified.", ex.Message);
        }
Ejemplo n.º 4
0
        public async Task ProcessAsync_AddsItemToContext()
        {
            // Arrange
            var radiosContext = new RadiosContext(
                idPrefix: "prefix",
                resolvedName: "r",
                viewContext: null,
                aspFor: null);

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

            var output = new TagHelperOutput(
                "govuk-radios-item",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var itemContext = (RadiosItemContext)context.Items[typeof(RadiosItemContext)];
                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 RadiosItemTagHelper(new DefaultGovUkHtmlGenerator())
            {
                IsChecked = true,
                Id        = "id",
                Value     = "V"
            };

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

            // Assert
            Assert.Contains(
                radiosContext.Items,
                item => item is RadiosItem 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");
        }
Ejemplo n.º 5
0
        public async Task ProcessAsync_WithModelExpression_DeducesCheckedFromModelExpression(string modelValue, bool expectedChecked)
        {
            // Arrange
            var model = new Model()
            {
                Foo = modelValue
            };

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

            var radiosContext = new RadiosContext(name: "test", aspFor: new ModelExpression(modelExpression, modelExplorer));

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

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

            var tagHelper = new RadiosItemTagHelper()
            {
                Checked = null,
                Value   = "bar"
            };

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

            // Assert
            Assert.Collection(
                radiosContext.Items,
                item =>
            {
                var radiosItem = Assert.IsType <RadiosItem>(item);
                Assert.Equal(expectedChecked, radiosItem.Checked);
            });
        }
Ejemplo n.º 6
0
        public async Task ProcessAsync_CheckedNullAndModelValueDoesEqualsValueDoesNotSetCheckedAttribute()
        {
            // Arrange
            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), "Foo");
            var viewContext   = new ViewContext();

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

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

            var output = new TagHelperOutput(
                "govuk-radios-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
            };

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

            var tagHelper = new RadiosItemTagHelper(htmlGenerator.Object)
            {
                Value = "baz"
            };

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

            // Assert
            Assert.False(radiosContext.Items.OfType <RadiosItem>().Single().IsChecked);
        }
Ejemplo n.º 7
0
        public async Task ProcessAsync_WithNullCollectionModelExpression_ExecutesSuccessfully()
        {
            // Arrange
            var model = new ModelWithCollectionProperty()
            {
                CollectionProperty = null
            };

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

            var radiosContext = new RadiosContext(name: "test", aspFor: new ModelExpression(modelExpression, modelExplorer));

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

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

            var tagHelper = new RadiosItemTagHelper()
            {
                Value = "2"
            };

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

            // Assert
            Assert.Collection(
                radiosContext.Items,
                item =>
            {
                var radiosItem = Assert.IsType <RadiosItem>(item);
                Assert.False(radiosItem.Checked);
            });
        }
Ejemplo n.º 8
0
        public async Task ProcessAsync_ComputesCorrectIdForSubsequentItemsWhenNotSpecified()
        {
            // Arrange
            var radiosContext = new RadiosContext(
                idPrefix: "prefix",
                resolvedName: "myradios",
                viewContext: null,
                aspFor: null);

            radiosContext.AddItem(new RadiosItemDivider()
            {
                Content = new HtmlString("Divider")
            });

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

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

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

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

            // Assert
            Assert.Equal("prefix-1", radiosContext.Items.OfType <RadiosItem>().Single().Id);
            Assert.Equal("prefix-1-item-hint", radiosContext.Items.OfType <RadiosItem>().Single().HintId);
            Assert.Equal("conditional-prefix-1", radiosContext.Items.OfType <RadiosItem>().Single().ConditionalId);
        }
Ejemplo n.º 9
0
        public async Task ProcessAsync_AddsItemToContext()
        {
            // Arrange
            var radiosContext = new RadiosContext(name: "test", aspFor: null);

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

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

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

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

            // Assert
            Assert.Collection(
                radiosContext.Items,
                item =>
            {
                var radiosItem = Assert.IsType <RadiosItem>(item);
                Assert.True(radiosItem.Checked);
                Assert.True(radiosItem.Disabled);
                Assert.Equal("id", radiosItem.Id);
                Assert.Equal("value", radiosItem.Value);
            });
        }
Ejemplo n.º 10
0
        public async Task ProcessAsync_WithConditional_SetsConditionalOnContext()
        {
            // Arrange
            var radiosContext = new RadiosContext(name: "test", aspFor: null);

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

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

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

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

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

            // Assert
            Assert.Collection(
                radiosContext.Items,
                item =>
            {
                var radiosItem = Assert.IsType <RadiosItem>(item);
                Assert.Equal("Conditional", radiosItem.Conditional.Content.RenderToString());
            });
        }
Ejemplo n.º 11
0
        public async Task ProcessAsync_ConditionalContentSpecifiedSetsIsConditional()
        {
            // Arrange
            var radiosContext = new RadiosContext(
                idPrefix: "prefix",
                resolvedName: "myradios",
                viewContext: null,
                aspFor: null);

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

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

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

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

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

            // Assert
            Assert.True(radiosContext.IsConditional);
        }
Ejemplo n.º 12
0
        public async Task ProcessAsync_WithoutHint_DoesNotSetHintOnContext()
        {
            // Arrange
            var radiosContext = new RadiosContext(name: "test", aspFor: null);

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

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

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

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

            // Assert
            Assert.Collection(
                radiosContext.Items,
                item =>
            {
                var radiosItem = Assert.IsType <RadiosItem>(item);
                Assert.Null(radiosItem.Hint);
            });
        }