Пример #1
0
        public void RenderComponent_ComponentReferenceNoops()
        {
            // Arrange
            var dispatcher   = Renderer.CreateDefaultDispatcher();
            var expectedHtml = new[] {
                "<", "p", ">", "<", "span", ">", "Hello world!", "</", "span", ">", "</", "p", ">",
                "<", "span", ">", "Child content!", "</", "span", ">"
            };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "p");
                rtb.OpenElement(1, "span");
                rtb.AddContent(2, "Hello world!");
                rtb.CloseElement();
                rtb.CloseElement();
                rtb.OpenComponent(3, typeof(ChildComponent));
                rtb.AddAttribute(4, "Value", "Child content!");
                rtb.AddComponentReferenceCapture(5, cr => { });
                rtb.CloseComponent();
            })).BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder, dispatcher);

            // Act
            var result = GetResult(dispatcher.Invoke(() => htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty)));

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #2
0
        public void RenderComponent_ElementRefsNoops()
        {
            // Arrange
            var dispatcher   = Renderer.CreateDefaultDispatcher();
            var expectedHtml = new[] {
                "<", "p", ">", "<", "span", ">", "Hello world!", "</", "span", ">", "</", "p", ">"
            };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "p");
                rtb.AddElementReferenceCapture(1, er => { });
                rtb.OpenElement(2, "span");
                rtb.AddContent(3,
                               // This internally creates a region frame.
                               rf => rf.AddContent(0, "Hello world!"));
                rtb.CloseElement();
                rtb.CloseElement();
            })).BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder, dispatcher);

            // Act
            var result = GetResult(dispatcher.Invoke(() => htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty)));

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #3
0
        public void RenderComponent_CanRenderComponentWithRenderFragmentContent()
        {
            // Arrange
            var expectedHtml = new[] {
                "<", "p", ">", "<", "span", ">", "Hello world!", "</", "span", ">", "</", "p", ">"
            };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "p");
                rtb.OpenElement(1, "span");
                rtb.AddContent(2,
                               // This internally creates a region frame.
                               rf => rf.AddContent(0, "Hello world!"));
                rtb.CloseElement();
                rtb.CloseElement();
            })).BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder);

            // Act
            var result = htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty);

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #4
0
        public void RenderComponent_CanRenderWithMultipleChildren()
        {
            // Arrange
            var dispatcher   = Renderer.CreateDefaultDispatcher();
            var expectedHtml = new[] { "<", "p", ">",
                                       "<", "span", ">", "Hello world!", "</", "span", ">",
                                       "<", "span", ">", "Bye Bye world!", "</", "span", ">",
                                       "</", "p", ">" };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "p");
                rtb.OpenElement(1, "span");
                rtb.AddContent(2, "Hello world!");
                rtb.CloseElement();
                rtb.OpenElement(3, "span");
                rtb.AddContent(4, "Bye Bye world!");
                rtb.CloseElement();
                rtb.CloseElement();
            })).BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder, dispatcher);

            // Act
            var result = GetResult(dispatcher.Invoke(() => htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty)));

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #5
0
        public void RenderComponent_CanPassParameters()
        {
            // Arrange
            var expectedHtml = new[] {
                "<", "p", ">", "<", "input", " ", "value", "=", "\"", "5", "\"", " />", "</", "p", ">"
            };

            RenderFragment Content(ParameterCollection pc) => new RenderFragment((RenderTreeBuilder rtb) =>
            {
                rtb.OpenElement(0, "p");
                rtb.OpenElement(1, "input");
                rtb.AddAttribute(2, "change", pc.GetValueOrDefault <Action <UIChangeEventArgs> >("update"));
                rtb.AddAttribute(3, "value", pc.GetValueOrDefault <int>("value"));
                rtb.CloseElement();
                rtb.CloseElement();
            });

            var serviceProvider = new ServiceCollection()
                                  .AddSingleton(new Func <ParameterCollection, RenderFragment>(Content))
                                  .BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder);
            Action <UIChangeEventArgs> change = (UIChangeEventArgs changeArgs) => throw new InvalidOperationException();

            // Act
            var result = htmlRenderer.RenderComponent <ComponentWithParameters>(
                new ParameterCollection(new[] {
                RenderTreeFrame.Element(0, string.Empty),
                RenderTreeFrame.Attribute(1, "update", change),
                RenderTreeFrame.Attribute(2, "value", 5)
            }, 0));

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #6
0
        public void RenderComponent_CanRenderComponentWithChildrenComponents()
        {
            // Arrange
            var expectedHtml = new[] {
                "<", "p", ">", "<", "span", ">", "Hello world!", "</", "span", ">", "</", "p", ">",
                "<", "span", ">", "Child content!", "</", "span", ">"
            };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "p");
                rtb.OpenElement(1, "span");
                rtb.AddContent(2, "Hello world!");
                rtb.CloseElement();
                rtb.CloseElement();
                rtb.OpenComponent(3, typeof(ChildComponent));
                rtb.AddAttribute(4, "Value", "Child content!");
                rtb.CloseComponent();
            })).BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder);

            // Act
            var result = htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty);

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #7
0
        public void RenderComponent_CanRenderEmptyElement()
        {
            // Arrange
            var expectedHtml    = new[] { "<", "p", ">", "</", "p", ">" };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "p");
                rtb.CloseElement();
            })).BuildServiceProvider();
            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder);

            // Act
            var result = htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty);

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #8
0
        public void RenderComponent_DoesNotEncodeMarkup()
        {
            // Arrange
            var dispatcher      = Renderer.CreateDefaultDispatcher();
            var expectedHtml    = new[] { "<", "p", ">", "<span>Hello world!</span>", "</", "p", ">" };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "p");
                rtb.AddMarkupContent(1, "<span>Hello world!</span>");
                rtb.CloseElement();
            })).BuildServiceProvider();
            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder, dispatcher);

            // Act
            var result = GetResult(dispatcher.Invoke(() => htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty)));

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #9
0
        public void RenderComponent_DoesNotRenderBooleanAttributesWhenValueIsFalse()
        {
            // Arrange
            var expectedHtml    = new[] { "<", "input", " />" };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "input");
                rtb.AddAttribute(1, "disabled", false);
                rtb.CloseElement();
            })).BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder);

            // Act
            var result = htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty);

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #10
0
        public void RenderComponent_CanRenderBooleanAttributes()
        {
            // Arrange
            var dispatcher      = Renderer.CreateDefaultDispatcher();
            var expectedHtml    = new[] { "<", "input", " ", "disabled", " />" };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "input");
                rtb.AddAttribute(1, "disabled", true);
                rtb.CloseElement();
            })).BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder, dispatcher);

            // Act
            var result = GetResult(dispatcher.Invoke(() => htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty)));

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #11
0
        public void RenderComponent_CanRenderWithAttributes()
        {
            // Arrange
            var expectedHtml    = new[] { "<", "p", " ", "class", "=", "\"", "lead", "\"", ">", "Hello world!", "</", "p", ">" };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "p");
                rtb.AddAttribute(1, "class", "lead");
                rtb.AddContent(2, "Hello world!");
                rtb.CloseElement();
            })).BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder);

            // Act
            var result = htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty);

            // Assert
            Assert.Equal(expectedHtml, result);
        }
Пример #12
0
        public void RenderComponent_HtmlEncodesAttributeValues()
        {
            // Arrange
            var dispatcher      = Renderer.CreateDefaultDispatcher();
            var expectedHtml    = new[] { "<", "p", " ", "class", "=", "\"", "&lt;lead", "\"", ">", "Hello world!", "</", "p", ">" };
            var serviceProvider = new ServiceCollection().AddSingleton(new RenderFragment(rtb =>
            {
                rtb.OpenElement(0, "p");
                rtb.AddAttribute(1, "class", "<lead");
                rtb.AddContent(2, "Hello world!");
                rtb.CloseElement();
            })).BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder, dispatcher);

            // Act
            var result = GetResult(dispatcher.Invoke(() => htmlRenderer.RenderComponent <TestComponent>(ParameterCollection.Empty)));

            // Assert
            Assert.Equal(expectedHtml, result);
        }