Blazored / Toast

A JavaScript free toast library for Blazor and Razor Component applications
https://blazored.github.io/Toast/
MIT License
654 stars 90 forks source link

New override to InMemoryToastService to include toast details #228

Closed jennifer-lebantino closed 6 months ago

jennifer-lebantino commented 1 year ago

Is your feature request related to a problem? Please describe. When testing custom toast components that use BlazoredToast, not enough information is passed to the InMemoryToastService. This limits test capability as there's not enough detail available (i.e. toast level, message).

Describe the solution you'd like It would be great to add an override that takes in whole components in the InMemoryToastService so we can access this data.

Describe alternatives you've considered Currently I can only test that some toast message does appear which defaults to an 'info' level and the message is null.

Additional context N/A

TheoWakelin commented 6 months ago

Instead of adding a new override that takes in whole components to the InMemoryToastService, a nullable ToastParameters field could be added to the InMemoryToast class, which is then populated when ToastParameters is passed as a parameter to the ShowToast<TComponent> overrides.

The advantage of going for this approach would be that consumers of the package would not need to use a new override to get this functionality, meaning they do not need to update the code in the client to test the parameters which are set.

For assertions, assuming the custom component has a status, the value could be verified by doing the below:

Assert.Equal(ToastLevel.Info, toastService.Toasts.Single().ToastParameters!.TryGet<ToastLevel>(nameof(Status));

Eventually, a wrapper around the above could be nice to add, so that the assertions are more readable.

If this approach is okay, I can get a PR created for this.

chrissainty commented 6 months ago

This looks like a great approach, Theo. I especially like the fact developers already using the library won't need to change anything to take advantage of this.

I agree that perhaps an extension method to make accessing a property on a component would be a great addition. But please feel free to submit a PR when you're ready.