コード例 #1
0
        public void SetItem_OutsideOfFieldset_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new DateInputContext(haveExplicitValue: false);

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

            context.CloseFieldset(fieldsetContext);

            // Act
            var ex = Record.Exception(() => context.SetItem(DateInputItemType.Month, new DateInputContextItem()));

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("<govuk-date-input-month> must be inside <govuk-date-input-fieldset>.", ex.Message);
        }
コード例 #2
0
        public void SetItem_WithValueWithTopLevelValue_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new DateInputContext(haveExplicitValue: true);

            context.SetItem(DateInputItemType.Month, new DateInputContextItem());

            // Act
            var ex = Record.Exception(() => context.SetItem(DateInputItemType.Month, new DateInputContextItem()
            {
                ValueSpecified = true
            }));

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Value cannot be specified for both <govuk-date-input-month> and the parent <govuk-date-input>.", ex.Message);
        }
コード例 #3
0
        public void OpenFieldset_AlreadyGotItem_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new DateInputContext(haveExplicitValue: false);

            var item = new DateInputContextItem()
            {
                LabelContent = new HtmlString("Day")
            };

            context.SetItem(DateInputItemType.Day, item);

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("<govuk-date-input-fieldset> must be the only direct child of the <govuk-date-input>.", ex.Message);
        }
コード例 #4
0
        public void SetHint_AlreadyGotItem_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new DateInputContext(haveExplicitValue: false);

            var item = new DateInputContextItem()
            {
                LabelContent = new HtmlString("Day")
            };

            context.SetItem(DateInputItemType.Day, item);

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("<govuk-date-input-hint> must be specified before <govuk-date-input-day>.", ex.Message);
        }
コード例 #5
0
        public async Task ProcessAsync_ParentAlreadyHasFieldset_ThrowsInvalidOperationException()
        {
            // Arrange
            var dateInputContext = new DateInputContext(haveExplicitValue: false);

            dateInputContext.OpenFieldset();
            var checkboxesFieldsetContext = new DateInputFieldsetContext(attributes: null);

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

            var context = new TagHelperContext(
                tagName: "govuk-date-input-fieldset",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(DateInputContext), dateInputContext }
            },
                uniqueId: "test");

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

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

            var tagHelper = new DateInputFieldsetTagHelper();

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("Only one <govuk-date-input-fieldset> element is permitted within each <govuk-date-input>.", ex.Message);
        }
コード例 #6
0
        public void SetHint_OutsideOfFieldset_ThrowsInvalidOperationException()
        {
            // Arrange
            var context = new DateInputContext(haveExplicitValue: false);

            var item = new DateInputContextItem()
            {
                LabelContent = new HtmlString("Day")
            };

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

            context.CloseFieldset(fieldsetContext);

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

            // Assert
            Assert.IsType <InvalidOperationException>(ex);
            Assert.Equal("<govuk-date-input-hint> must be inside <govuk-date-input-fieldset>.", ex.Message);
        }
コード例 #7
0
        public async Task ProcessAsync_SetsErrorMessageAndErrorComponentsOnContext()
        {
            // Arrange
            var dateInputContext = new DateInputContext(haveExplicitValue: false);

            var context = new TagHelperContext(
                tagName: "govuk-date-input-error-message",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>()
            {
                { typeof(DateInputContext), dateInputContext }
            },
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-date-input-error-message",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Error message");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new DateInputErrorMessageTagHelper()
            {
                ErrorItems = DateInputErrorComponents.Day | DateInputErrorComponents.Month
            };

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

            // Assert
            Assert.Equal("Error message", dateInputContext.ErrorMessage?.Content?.ToString());
            Assert.Equal(DateInputErrorComponents.Day | DateInputErrorComponents.Month, dateInputContext.ErrorComponents);
        }