Esempio n. 1
0
        public void DoesNotRenderAfterOnParametersSetAsyncTaskIsCancelled()
        {
            // Arrange
            var renderer  = new TestRenderer();
            var component = new TestComponent()
            {
                Counter = 1
            };
            var onParametersSetTask = new TaskCompletionSource <object>();

            component.OnParametersSetAsyncLogic = _ => onParametersSetTask.Task;

            // Act
            var componentId = renderer.AssignRootComponentId(component);

            renderer.RenderRootComponent(componentId);

            // Assert
            Assert.Single(renderer.Batches);

            // Cancel task started by OnParametersSet
            component.Counter = 2;
            onParametersSetTask.SetCanceled();

            // Component should not be rendered again
            Assert.Single(renderer.Batches);
        }
Esempio n. 2
0
        public void DoesNotRenderAfterOnInitAsyncTaskIsCancelled()
        {
            // Arrange
            var renderer  = new TestRenderer();
            var component = new TestComponent()
            {
                Counter = 1
            };
            var initTask = new TaskCompletionSource <object>();

            component.OnInitAsyncLogic = _ => initTask.Task;

            // Act
            var componentId = renderer.AssignRootComponentId(component);

            renderer.RenderRootComponent(componentId);

            // Assert
            Assert.Single(renderer.Batches);

            // Cancel task started by OnInitAsync
            component.Counter = 2;
            initTask.SetCanceled();

            // Component should only be rendered again due to
            // the call to StateHasChanged after SetParametersAsync
            Assert.Equal(2, renderer.Batches.Count);
        }
Esempio n. 3
0
        public void RendersAfterParametersSetAsyncTaskIsCompleted()
        {
            // Arrange
            var renderer  = new TestRenderer();
            var component = new TestComponent();

            component.Counter = 1;
            var parametersSetTask = new TaskCompletionSource <bool>();

            component.RunsBaseOnParametersSetAsync = false;
            component.OnParametersSetAsyncLogic    = c => parametersSetTask.Task;

            // Act
            var componentId = renderer.AssignRootComponentId(component);

            renderer.RenderRootComponent(componentId);

            // Assert
            Assert.Single(renderer.Batches);

            // Completes task started by OnParametersSetAsync
            component.Counter = 2;
            parametersSetTask.SetResult(true);

            // Component should be rendered again
            Assert.Equal(2, renderer.Batches.Count);
        }
        public void RequiresTypeParameter()
        {
            var instance    = new DynamicComponent();
            var renderer    = new TestRenderer();
            var componentId = renderer.AssignRootComponentId(instance);

            var ex = Assert.Throws <InvalidOperationException>(
                () => renderer.RenderRootComponent(componentId, ParameterView.Empty));

            Assert.StartsWith(
                $"{ nameof(DynamicComponent)} requires a non-null value for the parameter {nameof(DynamicComponent.Type)}.",
                ex.Message);
        }
Esempio n. 5
0
        public void RunsOnInitWhenRendered()
        {
            // Arrange
            var renderer  = new TestRenderer();
            var component = new TestComponent();

            var onInitRuns = 0;

            component.OnInitLogic = c => onInitRuns++;

            // Act
            var componentId = renderer.AssignRootComponentId(component);

            renderer.RenderRootComponent(componentId);

            // Assert
            Assert.Equal(1, onInitRuns);
        }
Esempio n. 6
0
        public void RunsOnParametersSetWhenRendered()
        {
            // Arrange
            var renderer  = new TestRenderer();
            var component = new TestComponent();

            var onParametersSetRuns = 0;

            component.OnParametersSetLogic = c => onParametersSetRuns++;

            // Act
            var componentId = renderer.AssignRootComponentId(component);

            renderer.RenderRootComponent(componentId);

            // Assert
            Assert.Equal(1, onParametersSetRuns);
            Assert.Single(renderer.Batches);
        }
        public void CanRenderComponentByType()
        {
            // Arrange
            var instance    = new DynamicComponent();
            var renderer    = new TestRenderer();
            var componentId = renderer.AssignRootComponentId(instance);
            var parameters  = ParameterView.FromDictionary(new Dictionary <string, object>
            {
                { nameof(DynamicComponent.Type), typeof(TestComponent) },
            });

            // Act
            renderer.RenderRootComponent(componentId, parameters);

            // Assert
            var batch = renderer.Batches.Single();

            AssertFrame.Component <TestComponent>(batch.ReferenceFrames[0], 2, 0);
            AssertFrame.Text(batch.ReferenceFrames[2], "Hello from TestComponent with IntProp=0", 0);
        }
Esempio n. 8
0
        public void RendersAfterParametersSetAndInitAsyncTasksAreCompleted()
        {
            // Arrange
            var renderer  = new TestRenderer();
            var component = new TestComponent();

            component.Counter = 1;
            var initTask          = new TaskCompletionSource <bool>();
            var parametersSetTask = new TaskCompletionSource <bool>();

            component.RunsBaseOnInitAsync          = true;
            component.RunsBaseOnParametersSetAsync = true;
            component.OnInitAsyncLogic             = c => initTask.Task;
            component.OnParametersSetAsyncLogic    = c => parametersSetTask.Task;

            // Act
            var componentId = renderer.AssignRootComponentId(component);

            renderer.RenderRootComponent(componentId);

            // Assert
            Assert.Single(renderer.Batches);

            // Completes task started by OnParametersSetAsync
            component.Counter = 2;
            parametersSetTask.SetResult(false);

            // Component should be rendered again
            Assert.Equal(2, renderer.Batches.Count);

            // Completes task started by OnInitAsync
            // NOTE: We will probably change this behavior. It would make more sense for the base class
            // to wait until InitAsync is completed before proceeding with SetParametersAsync, rather
            // that running the two lifecycle methods in parallel. This will come up as a requirement
            // when implementing async server-side prerendering.
            component.Counter = 3;
            initTask.SetResult(true);

            // Component should be rendered again
            Assert.Equal(3, renderer.Batches.Count);
        }
Esempio n. 9
0
        public void RendersAfterParametersSetAndInitAsyncTasksAreCompleted()
        {
            // Arrange
            var renderer  = new TestRenderer();
            var component = new TestComponent();

            component.Counter = 1;
            var initTask          = new TaskCompletionSource <bool>();
            var parametersSetTask = new TaskCompletionSource <bool>();

            component.RunsBaseOnInitAsync          = true;
            component.RunsBaseOnParametersSetAsync = true;
            component.OnInitAsyncLogic             = c => initTask.Task;
            component.OnParametersSetAsyncLogic    = c => parametersSetTask.Task;

            // Act
            var componentId = renderer.AssignRootComponentId(component);

            renderer.RenderRootComponent(componentId);

            // Assert
            Assert.Single(renderer.Batches);

            // Completes task started by OnInitAsync
            component.Counter = 2;
            initTask.SetResult(true);

            // Component should be rendered again 2 times
            // after on init async
            // after set parameters
            Assert.Equal(3, renderer.Batches.Count);

            // Completes task started by OnParametersSetAsync
            component.Counter = 3;
            parametersSetTask.SetResult(false);

            // Component should be rendered again
            // after the async part of onparameterssetasync completes
            Assert.Equal(4, renderer.Batches.Count);
        }
        public void WhenAuthorized_RendersPageInsideLayout()
        {
            // Arrange
            var routeData = new RouteData(typeof(TestPageRequiringAuthorization), new Dictionary <string, object>
            {
                { nameof(TestPageRequiringAuthorization.Message), "Hello, world!" }
            });

            _testAuthorizationService.NextResult = AuthorizationResult.Success();

            // Act
            _renderer.RenderRootComponent(_authorizeRouteViewComponentId, ParameterView.FromDictionary(new Dictionary <string, object>
            {
                { nameof(AuthorizeRouteView.RouteData), routeData },
                { nameof(AuthorizeRouteView.DefaultLayout), typeof(TestLayout) },
            }));

            // Assert: renders layout
            var batch      = _renderer.Batches.Single();
            var layoutDiff = batch.GetComponentDiffs <TestLayout>().Single();

            Assert.Collection(layoutDiff.Edits,
                              edit => AssertPrependText(batch, edit, "Layout starts here"),
                              edit =>
            {
                Assert.Equal(RenderTreeEditType.PrependFrame, edit.Type);
                AssertFrame.Component <TestPageRequiringAuthorization>(batch.ReferenceFrames[edit.ReferenceFrameIndex]);
            },
                              edit => AssertPrependText(batch, edit, "Layout ends here"));

            // Assert: renders page
            var pageDiff = batch.GetComponentDiffs <TestPageRequiringAuthorization>().Single();

            Assert.Collection(pageDiff.Edits,
                              edit => AssertPrependText(batch, edit, "Hello from the page with message: Hello, world!"));
        }
        public void CanRenderComponentByTypeWithParameters()
        {
            // Arrange
            var instance        = new DynamicComponent();
            var renderer        = new TestRenderer();
            var childParameters = new Dictionary <string, object>
            {
                { nameof(TestComponent.IntProp), 123 },
                { nameof(TestComponent.ChildContent), (RenderFragment)(builder =>
                    {
                        builder.AddContent(0, "This is some child content");
                    }) },
            };
            var parameters = ParameterView.FromDictionary(new Dictionary <string, object>
            {
                { nameof(DynamicComponent.Type), typeof(TestComponent) },
                { nameof(DynamicComponent.Parameters), childParameters },
            });

            // Act
            renderer.RenderRootComponent(
                renderer.AssignRootComponentId(instance),
                parameters);

            // Assert
            var batch = renderer.Batches.Single();

            // It renders a reference to the child component with its parameters
            AssertFrame.Component <TestComponent>(batch.ReferenceFrames[0], 4, 0);
            AssertFrame.Attribute(batch.ReferenceFrames[1], nameof(TestComponent.IntProp), 123, 1);
            AssertFrame.Attribute(batch.ReferenceFrames[2], nameof(TestComponent.ChildContent), 1);

            // The child component itself is rendered
            AssertFrame.Text(batch.ReferenceFrames[4], "Hello from TestComponent with IntProp=123", 0);
            AssertFrame.Text(batch.ReferenceFrames[5], "This is some child content", 0);
        }
        public void CanAccessToChildComponentInstance()
        {
            // Arrange
            var instance        = new DynamicComponent();
            var renderer        = new TestRenderer();
            var childParameters = new Dictionary <string, object>
            {
                { nameof(TestComponent.IntProp), 123 },
            };
            var parameters = ParameterView.FromDictionary(new Dictionary <string, object>
            {
                { nameof(DynamicComponent.Type), typeof(TestComponent) },
                { nameof(DynamicComponent.Parameters), childParameters },
            });

            // Act
            renderer.RenderRootComponent(
                renderer.AssignRootComponentId(instance),
                parameters);

            // Assert
            Assert.IsType <TestComponent>(instance.Instance);
            Assert.Equal(123, ((TestComponent)instance.Instance).IntProp);
        }
Esempio n. 13
0
        public void RunsOnInitAsyncAlsoOnBaseClassWhenRendered()
        {
            // Arrange
            var renderer  = new TestRenderer();
            var component = new TestComponent();

            var onInitAsyncRuns = 0;

            component.RunsBaseOnInitAsync = true;
            component.OnInitAsyncLogic    = c =>
            {
                onInitAsyncRuns++;
                return(Task.CompletedTask);
            };

            // Act
            var componentId = renderer.AssignRootComponentId(component);

            renderer.RenderRootComponent(componentId);

            // Assert
            Assert.Equal(1, onInitAsyncRuns);
            Assert.Single(renderer.Batches);
        }
Esempio n. 14
0
        public void RunsOnParametersSetAsyncWhenRendered()
        {
            // Arrange
            var renderer  = new TestRenderer();
            var component = new TestComponent();

            int onParametersSetAsyncRuns = 0;

            component.RunsBaseOnParametersSetAsync = false;
            component.OnParametersSetAsyncLogic    = c =>
            {
                onParametersSetAsyncRuns++;
                return(Task.CompletedTask);
            };

            // Act
            var componentId = renderer.AssignRootComponentId(component);

            renderer.RenderRootComponent(componentId);

            // Assert
            Assert.Equal(1, onParametersSetAsyncRuns);
            Assert.Single(renderer.Batches);
        }