Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Invoke base lifecycle methods in component (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
klemmchr authored Mar 5, 2022
1 parent 69050e0 commit 851b783
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
27 changes: 16 additions & 11 deletions src/MvvmBlazor.CodeGenerators/Components/MvvmComponentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,56 +312,61 @@ protected internal TValue Bind<TValue>(Expression<Func<T, TValue>> property)
/// <inheritdoc />
protected override void OnInitialized()
{{
base.OnInitialized();
SetBindingContext();
base.OnInitialized();
BindingContext?.OnInitialized();
}}
/// <inheritdoc />
protected override Task OnInitializedAsync()
protected override async Task OnInitializedAsync()
{{
return BindingContext?.OnInitializedAsync() ?? Task.CompletedTask;
await base.OnInitializedAsync();
await BindingContext!.OnInitializedAsync();
}}
/// <inheritdoc />
protected override void OnParametersSet()
{{
SetParameters();
base.OnParametersSet();
BindingContext?.OnParametersSet();
}}
/// <inheritdoc />
protected override Task OnParametersSetAsync()
protected override async Task OnParametersSetAsync()
{{
return BindingContext?.OnParametersSetAsync() ?? Task.CompletedTask;
await base.OnParametersSetAsync();
await BindingContext.OnParametersSetAsync();
}}
/// <inheritdoc />
protected override bool ShouldRender()
{{
return BindingContext?.ShouldRender() ?? true;
return BindingContext!.ShouldRender();
}}
/// <inheritdoc />
protected override void OnAfterRender(bool firstRender)
{{
BindingContext?.OnAfterRender(firstRender);
base.OnAfterRender(firstRender);
BindingContext!.OnAfterRender(firstRender);
}}
/// <inheritdoc />
protected override Task OnAfterRenderAsync(bool firstRender)
protected override async Task OnAfterRenderAsync(bool firstRender)
{{
return BindingContext?.OnAfterRenderAsync(firstRender) ?? Task.CompletedTask;
await base.OnAfterRenderAsync(firstRender);
await BindingContext!.OnAfterRenderAsync(firstRender);
}}
/// <inheritdoc />
public override async Task SetParametersAsync(ParameterView parameters)
{{
await base.SetParametersAsync(parameters).ConfigureAwait(false);
await base.SetParametersAsync(parameters);
if (BindingContext != null)
{{
await BindingContext.SetParametersAsync(parameters).ConfigureAwait(false);
await BindingContext.SetParametersAsync(parameters);
}}
}}
}}
Expand Down
21 changes: 9 additions & 12 deletions src/MvvmBlazor.Tests/Components/MvvmComponentBaseTTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ public void AfterRender_called_On_binding_context()
}

[Fact]
public void AfterRenderAsync_called_on_binding_context()
public async Task AfterRenderAsync_called_on_binding_context()
{
var task = new Task(() => { });
var task = Task.CompletedTask;
var viewModel = Services.GetMock<ViewModelBase>();
var component = Services.GetRequiredService<MockMvvmComponentBase>();

viewModel.Setup(x => x.OnAfterRenderAsync(It.IsAny<bool>())).Returns(task).Verifiable();

var res = component.AfterRenderAsync(true);
res.ShouldBe(task);
await component.AfterRenderAsync(true);

viewModel.Verify();
}
Expand Down Expand Up @@ -75,17 +74,16 @@ public void OnInitialized_called_on_binding_context()
}

[Fact]
public void OnInitializedAsync_called_on_binding_context()
public async Task OnInitializedAsync_called_on_binding_context()
{
var task = new Task(() => { });
var task = Task.CompletedTask;

var viewModel = Services.GetMock<ViewModelBase>();
var component = Services.GetRequiredService<MockMvvmComponentBase>();

viewModel.Setup(x => x.OnInitializedAsync()).Returns(task).Verifiable();

var res = component.InitializedAsync();
res.ShouldBe(task);
await component.InitializedAsync();

viewModel.Verify();
}
Expand All @@ -106,17 +104,16 @@ public void OnParametersSet_sets_viewmodel_parameters()
}

[Fact]
public void OnParametersSetAsync_called_on_binding_context()
public async Task OnParametersSetAsync_called_on_binding_context()
{
var task = new Task(() => { });
var task = Task.CompletedTask;

var viewModel = Services.GetMock<ViewModelBase>();
var component = Services.GetRequiredService<MockMvvmComponentBase>();

viewModel.Setup(x => x.OnParametersSetAsync()).Returns(task).Verifiable();

var res = component.ParametersSetAsync();
res.ShouldBe(task);
await component.ParametersSetAsync();

viewModel.Verify();
}
Expand Down

0 comments on commit 851b783

Please sign in to comment.