Skip to content

Commit

Permalink
Add Error event in Blazor ViewModel (#3279) (#3915)
Browse files Browse the repository at this point in the history
* Add Error event in Blazor ViewModel (#3279)

* fix tests

* Error event provides Exception
  • Loading branch information
rrrooommmaaa committed May 6, 2024
1 parent 89bc195 commit db84ef0
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Source/Csla.Blazor.Test/Fakes/FakePerson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class FakePerson : BusinessBase<FakePerson>
public static Csla.PropertyInfo<string> MobileTelephoneProperty = RegisterProperty<string>(nameof(MobileTelephone));
public static Csla.PropertyInfo<FakePersonEmailAddresses> EmailAddressesProperty = RegisterProperty<FakePersonEmailAddresses>(nameof(EmailAddresses));

public static string FirstNameFailOnInsertValue = "FailOnInsert";

#region Properties

[MaxLength(25)]
Expand Down Expand Up @@ -92,6 +94,13 @@ private void Create([Inject] IChildDataPortal<FakePersonEmailAddresses> dataPort
BusinessRules.CheckRules();
}

[Insert]
private void Insert()
{
if (FirstName == FirstNameFailOnInsertValue) {
throw new Exception("Insert failed");
}
}
#endregion

}
Expand Down
97 changes: 97 additions & 0 deletions Source/Csla.Blazor.Test/ViewModelSaveAsyncErrorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Csla.Blazor.Test.Fakes;
using Csla.TestHelpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Csla.Blazor.Test
{
[TestClass]
public class ViewModelSaveAsyncErrorTests
{
private static TestDIContext _testDIContext;

[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
_testDIContext = TestDIContextFactory.CreateDefaultContext();
}

[TestMethod]
public async Task SavingFailure_ErrorEventIsInvoked()
{
// Arrange
var person = GetValidFakePerson();
var appCntxt = _testDIContext.CreateTestApplicationContext();
var vm = new ViewModel<FakePerson>(appCntxt)
{
Model = person,
};
var error = 0;
vm.Error += (o, e) =>
{
error++;
Assert.IsNotNull(e.Error);
// these fields should be populated when Error event is triggered
Assert.IsNotNull(vm.Exception);
Assert.IsNotNull(vm.ViewModelErrorText);
Assert.AreSame(e.Error, vm.Exception);
Assert.AreSame(o, vm);
};
person.FirstName = FakePerson.FirstNameFailOnInsertValue;

// Act
await vm.SaveAsync();

// Assert
Assert.AreEqual(1, error); // Error event should have been triggered only once
Assert.IsNotNull(vm.Exception);
Assert.IsNotNull(vm.ViewModelErrorText);
}

[TestMethod]
public async Task SavingSuccess_ErrorEventIsNotInvoked()
{
// Arrange
var person = GetValidFakePerson();
var appCntxt = _testDIContext.CreateTestApplicationContext();
var vm = new ViewModel<FakePerson>(appCntxt)
{
Model = person,
};
var error = false;
vm.Error += (o, e) =>
{
error = true;
};

// Act
await vm.SaveAsync();

// Assert
Assert.IsFalse(error); // Error event shouldn't have been triggered
Assert.IsNull(vm.Exception);
Assert.IsNull(vm.ViewModelErrorText);
}

#region Helper Methods

FakePerson GetValidFakePerson()
{
IDataPortal<FakePerson> dataPortal;
FakePerson person;

// Create an instance of a DataPortal that can be used for instantiating objects
dataPortal = _testDIContext.CreateDataPortal<FakePerson>();
person = dataPortal.Create();

person.FirstName = "John";
person.LastName = "Smith";
person.HomeTelephone = "01234 567890";

return person;
}

#endregion

}

}
7 changes: 7 additions & 0 deletions Source/Csla.Blazor/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public ViewModel(ApplicationContext applicationContext)
/// </summary>
public event Action Saved;
/// <summary>
/// Event raised when failed to save Model.
/// </summary>
public event EventHandler<Core.ErrorEventArgs> Error;
/// <summary>
/// Event raised when Model is changing.
/// </summary>
public event Action<T, T> ModelChanging;
Expand Down Expand Up @@ -284,6 +288,9 @@ public async Task SaveAsync()
{
HookChangedEvents(Model);
IsBusy = false;
if (Exception != null) {
Error?.Invoke(this, new Core.ErrorEventArgs(this, Exception));
}
}
}

Expand Down

0 comments on commit db84ef0

Please sign in to comment.