示例#1
0
        public void RenderComponentAsync_CanRenderEmptyElement()
        {
            // Arrange
            var dispatcher      = Renderer.CreateDefaultDispatcher();
            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, dispatcher);

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

            // Assert
            Assert.Equal(expectedHtml, result);
        }
示例#2
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);
        }
示例#3
0
        public async Task CanRender_AsyncComponent()
        {
            // Arrange
            var expectedHtml = new[] {
                "<", "p", ">", "20", "</", "p", ">"
            };
            var serviceProvider = new ServiceCollection().AddSingleton <AsyncComponent>().BuildServiceProvider();

            var htmlRenderer = new HtmlRenderer(serviceProvider, _encoder);

            // Act
            var result = await htmlRenderer.RenderComponentAsync <AsyncComponent>(ParameterCollection.FromDictionary(new Dictionary <string, object>
            {
                ["Value"] = 10
            }));

            // Assert
            Assert.Equal(expectedHtml, result);
        }
示例#4
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);
        }
示例#5
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);
        }
示例#6
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);
        }
示例#7
0
        public async Task CanRender_NestedAsyncComponents()
        {
            // Arrange
            var dispatcher   = Renderer.CreateDefaultDispatcher();
            var expectedHtml = new[] {
                "<", "p", ">", "20", "</", "p", ">",
                "<", "p", ">", "80", "</", "p", ">"
            };

            var serviceProvider = new ServiceCollection().AddSingleton <AsyncComponent>().BuildServiceProvider();

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

            // Act
            var result = await dispatcher.InvokeAsync(() => htmlRenderer.RenderComponentAsync <NestedAsyncComponent>(ParameterCollection.FromDictionary(new Dictionary <string, object>
            {
                ["Nested"] = false,
                ["Value"]  = 10
            })));

            // Assert
            Assert.Equal(expectedHtml, result);
        }
示例#8
0
        public void RenderComponent_CanPassParameters()
        {
            // Arrange
            var dispatcher   = Renderer.CreateDefaultDispatcher();
            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, dispatcher);
            Action <UIChangeEventArgs> change = (UIChangeEventArgs changeArgs) => throw new InvalidOperationException();

            // Act
            var result = GetResult(dispatcher.Invoke(() => 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);
        }