Пример #1
0
        public void CanAddFragments()
        {
            // Arrange
            var            builder  = new RenderTreeBuilder(new TestRenderer());
            RenderFragment fragment = fragmentBuilder =>
            {
                fragmentBuilder.AddContent(0, "Hello from the fragment");
                fragmentBuilder.OpenElement(1, "Fragment element");
                fragmentBuilder.AddContent(2, "Some text");
                fragmentBuilder.CloseElement();
            };

            // Act
            builder.OpenElement(10, "parent");
            builder.AddContent(11, fragment);
            builder.CloseElement();

            // Assert
            Assert.Collection(builder.GetFrames(),
                              frame => AssertFrame.Element(frame, "parent", 5, 10),
                              frame => AssertFrame.Region(frame, 4, 11),
                              frame => AssertFrame.Text(frame, "Hello from the fragment", 0),
                              frame => AssertFrame.Element(frame, "Fragment element", 2, 1),
                              frame => AssertFrame.Text(frame, "Some text", 2));
        }
        public void PassesThroughRegionsInsidePrependedElements()
        {
            // Arrange
            oldTree.AddContent(0, "Will not change");
            newTree.AddContent(0, "Will not change");
            newTree.OpenElement(1, "root");
            newTree.OpenRegion(2);
            newTree.AddContent(0, "text1");
            newTree.CloseRegion();
            newTree.CloseElement();

            // Act
            var(result, referenceFrames) = GetSingleUpdatedComponent();

            // Assert
            Assert.Collection(result.Edits,
                              entry =>
            {
                AssertEdit(entry, RenderTreeEditType.PrependFrame, 1);
                Assert.Equal(0, entry.ReferenceFrameIndex);
            });
            Assert.Collection(referenceFrames,
                              frame => AssertFrame.Element(frame, "root", 3, 1),
                              frame => AssertFrame.Region(frame, 2, 2),
                              frame => AssertFrame.Text(frame, "text1"));
        }
Пример #3
0
        public void CanAddRegions()
        {
            // Arrange
            var builder = new RenderTreeBuilder(new TestRenderer());

            // Act
            builder.OpenElement(10, "parent");                      //  0: <parent>
            builder.OpenRegion(11);                                 //  1:     [region
            builder.AddContent(3, "Hello");                         //  2:         Hello
            builder.OpenRegion(4);                                  //  3:         [region
            builder.OpenElement(3, "another");                      //  4:             <another>
            builder.CloseElement();                                 //                 </another>
            builder.CloseRegion();                                  //             ]
            builder.AddContent(6, "Goodbye");                       //  5:         Goodbye
            builder.CloseRegion();                                  //         ]
            builder.CloseElement();                                 //     </parent>

            // Assert
            Assert.Collection(builder.GetFrames(),
                              frame => AssertFrame.Element(frame, "parent", 6, 10),
                              frame => AssertFrame.Region(frame, 5, 11),
                              frame => AssertFrame.Text(frame, "Hello", 3),
                              frame => AssertFrame.Region(frame, 2, 4),
                              frame => AssertFrame.Element(frame, "another", 1, 3),
                              frame => AssertFrame.Text(frame, "Goodbye", 6));
        }
Пример #4
0
    public void RendersContentInsideLayout()
    {
        // Arrange/Act
        var setParametersTask = _renderer.Dispatcher.InvokeAsync(() => _layoutViewComponent.SetParametersAsync(ParameterView.FromDictionary(new Dictionary <string, object>
        {
            { nameof(LayoutView.Layout), typeof(RootLayout) },
            { nameof(LayoutView.ChildContent), (RenderFragment)(builder => {
                    builder.AddContent(123, "Hello");
                    builder.AddContent(456, "Goodbye");
                }) }
        })));

        // Assert
        Assert.True(setParametersTask.IsCompletedSuccessfully);
        var batch = _renderer.Batches.Single();

        var layoutViewFrames = _renderer.GetCurrentRenderTreeFrames(_layoutViewComponentId).AsEnumerable();

        Assert.Collection(layoutViewFrames,
                          frame => AssertFrame.Component <RootLayout>(frame, subtreeLength: 2, sequence: 0),
                          frame => AssertFrame.Attribute(frame, nameof(LayoutComponentBase.Body), sequence: 1));

        var rootLayoutComponentId = batch.GetComponentFrames <RootLayout>().Single().ComponentId;
        var rootLayoutFrames      = _renderer.GetCurrentRenderTreeFrames(rootLayoutComponentId).AsEnumerable();

        Assert.Collection(rootLayoutFrames,
                          frame => AssertFrame.Text(frame, "RootLayout starts here", sequence: 0),
                          frame => AssertFrame.Region(frame, subtreeLength: 3),
                          frame => AssertFrame.Text(frame, "Hello", sequence: 123),
                          frame => AssertFrame.Text(frame, "Goodbye", sequence: 456),
                          frame => AssertFrame.Text(frame, "RootLayout ends here", sequence: 2));
    }
Пример #5
0
    public void GivenLayoutButNoContent_RendersLayoutWithEmptyBody()
    {
        // Arrange/Act
        var setParametersTask = _renderer.Dispatcher.InvokeAsync(() => _layoutViewComponent.SetParametersAsync(ParameterView.FromDictionary(new Dictionary <string, object>
        {
            { nameof(LayoutView.Layout), typeof(RootLayout) }
        })));

        // Assert
        Assert.True(setParametersTask.IsCompletedSuccessfully);
        var batch = _renderer.Batches.Single();

        var layoutViewFrames = _renderer.GetCurrentRenderTreeFrames(_layoutViewComponentId).AsEnumerable();

        Assert.Collection(layoutViewFrames,
                          frame => AssertFrame.Component <RootLayout>(frame, subtreeLength: 2, sequence: 0),
                          frame => AssertFrame.Attribute(frame, nameof(LayoutComponentBase.Body), sequence: 1));

        var rootLayoutComponentId = batch.GetComponentFrames <RootLayout>().Single().ComponentId;
        var rootLayoutFrames      = _renderer.GetCurrentRenderTreeFrames(rootLayoutComponentId).AsEnumerable();

        Assert.Collection(rootLayoutFrames,
                          frame => AssertFrame.Text(frame, "RootLayout starts here", sequence: 0),
                          frame => AssertFrame.Region(frame, subtreeLength: 1), // i.e., empty region
                          frame => AssertFrame.Text(frame, "RootLayout ends here", sequence: 2));
    }
        public void ExpandsAllAncestorsWhenAddingAttribute()
        {
            // Arrange
            var valuePropName = "testprop";
            var renderer      = new TestRenderer();
            var builder       = new RenderTreeBuilder();

            builder.OpenElement(0, "grandparent");
            builder.OpenRegion(1);
            builder.OpenElement(2, "sibling before"); // To show that non-ancestors aren't expanded
            builder.CloseElement();
            builder.OpenElement(3, "elem with handler");
            builder.AddAttribute(4, "eventname", (Action)(() => { }));
            builder.SetUpdatesAttributeName(valuePropName);
            builder.CloseElement(); // elem with handler
            builder.CloseRegion();
            builder.CloseElement(); // grandparent
            var frames = builder.GetFrames();

            frames.Array[4] = frames.Array[4].WithAttributeEventHandlerId(123);

            // Act
            RenderTreeUpdater.UpdateToMatchClientState(builder, 123, "new value");
            frames = builder.GetFrames();

            // Assert
            Assert.Collection(frames.AsEnumerable(),
                              frame => AssertFrame.Element(frame, "grandparent", 6, 0),
                              frame => AssertFrame.Region(frame, 5, 1),
                              frame => AssertFrame.Element(frame, "sibling before", 1, 2),
                              frame => AssertFrame.Element(frame, "elem with handler", 3, 3),
                              frame => AssertFrame.Attribute(frame, valuePropName, "new value", RenderTreeDiffBuilder.SystemAddedAttributeSequenceNumber),
                              frame => AssertFrame.Attribute(frame, "eventname", v => Assert.IsType <Action>(v), 4));
        }
Пример #7
0
    public void RendersPageInsideLayoutView()
    {
        // Arrange
        var routeParams = new Dictionary <string, object>
        {
            { nameof(ComponentWithLayout.Message), "Test message" }
        };
        var routeData = new RouteData(typeof(ComponentWithLayout), routeParams);

        // Act
        _renderer.Dispatcher.InvokeAsync(() => _routeViewComponent.SetParametersAsync(ParameterView.FromDictionary(new Dictionary <string, object>
        {
            { nameof(RouteView.RouteData), routeData },
        })));

        // Assert: RouteView renders LayoutView
        var batch           = _renderer.Batches.Single();
        var routeViewFrames = _renderer.GetCurrentRenderTreeFrames(_routeViewComponentId).AsEnumerable();

        Assert.Collection(routeViewFrames,
                          frame => AssertFrame.Component <LayoutView>(frame, subtreeLength: 3, sequence: 0),
                          frame => AssertFrame.Attribute(frame, nameof(LayoutView.Layout), (object)typeof(TestLayout), sequence: 1),
                          frame => AssertFrame.Attribute(frame, nameof(LayoutView.ChildContent), sequence: 2));

        // Assert: LayoutView renders TestLayout
        var layoutViewComponentId = batch.GetComponentFrames <LayoutView>().Single().ComponentId;
        var layoutViewFrames      = _renderer.GetCurrentRenderTreeFrames(layoutViewComponentId).AsEnumerable();

        Assert.Collection(layoutViewFrames,
                          frame => AssertFrame.Component <TestLayout>(frame, subtreeLength: 2, sequence: 0),
                          frame => AssertFrame.Attribute(frame, nameof(LayoutComponentBase.Body), sequence: 1));

        // Assert: TestLayout renders page
        var testLayoutComponentId = batch.GetComponentFrames <TestLayout>().Single().ComponentId;
        var testLayoutFrames      = _renderer.GetCurrentRenderTreeFrames(testLayoutComponentId).AsEnumerable();

        Assert.Collection(testLayoutFrames,
                          frame => AssertFrame.Text(frame, "Layout starts here", sequence: 0),
                          frame => AssertFrame.Region(frame, subtreeLength: 3),
                          frame => AssertFrame.Component <ComponentWithLayout>(frame, sequence: 0, subtreeLength: 2),
                          frame => AssertFrame.Attribute(frame, nameof(ComponentWithLayout.Message), "Test message", sequence: 1),
                          frame => AssertFrame.Text(frame, "Layout ends here", sequence: 2));

        // Assert: page itself is rendered, having received parameters from the original route data
        var pageComponentId = batch.GetComponentFrames <ComponentWithLayout>().Single().ComponentId;
        var pageFrames      = _renderer.GetCurrentRenderTreeFrames(pageComponentId).AsEnumerable();

        Assert.Collection(pageFrames,
                          frame => AssertFrame.Text(frame, "Hello from the page with message 'Test message'", sequence: 0));

        // Assert: nothing else was rendered
        Assert.Equal(4, batch.DiffsInOrder.Count);
    }