Skip to content

Commit

Permalink
228 - Adds toast parameters to InMemoryToast to enable better testing (
Browse files Browse the repository at this point in the history
…#248)

* Add new property to InMemoryToast and populate where appropriate
* Add example test for documentation
  • Loading branch information
TheoWakelin authored Feb 19, 2024
1 parent dc1cfed commit bc95f20
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions samples/BlazorServer/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
_toastParameters = new ToastParameters();
_toastParameters.Add(nameof(MyToastComponent.Title), "I'm a custom toast component with parameters");
_toastParameters.Add(nameof(MyToastComponent.ToastParam), "I'm a parameter");
_toastParameters.Add(nameof(MyToastComponent.Status), ToastLevel.Info);
}

private void OnShowHtml()
Expand Down
1 change: 1 addition & 0 deletions samples/BlazorServer/Pages/MyToastComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ else
@code{
[Parameter] public string Title { get; set; }
[Parameter] public string ToastParam { get; set; }
[Parameter] public ToastLevel Status { get; set; }
}
3 changes: 2 additions & 1 deletion samples/BlazorWebAssembly/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
{
_toastParameters = new ToastParameters()
.Add(nameof(MyToastComponent.Title), "I'm a custom toast component with parameters")
.Add(nameof(MyToastComponent.ToastParam), "I'm a parameter");
.Add(nameof(MyToastComponent.ToastParam), "I'm a parameter")
.Add(nameof(MyToastComponent.Status), ToastLevel.Info);
}

private void OnShowHtml()
Expand Down
1 change: 1 addition & 0 deletions samples/BlazorWebAssembly/Pages/MyToastComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ else

[Parameter] public string? Title { get; set; }
[Parameter] public string? ToastParam { get; set; }
[Parameter] public ToastLevel? Status { get; set; }
}
15 changes: 14 additions & 1 deletion samples/bUnitExample/BlazoredToastTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Blazored.Toast;
using Blazored.Toast.Services;
using BlazorWebAssembly.Pages;
using Bunit;
Expand Down Expand Up @@ -109,5 +108,19 @@ public void DisplaysToastComponent()
// Assert
Assert.Equal(1, toastService.Toasts.Count(_ => _.ToastType == typeof(MyToastComponent)));
}

[Fact]
public void DisplaysToastComponentWithLevel()
{
// Arrange
var toastService = this.AddBlazoredToast();
var cut = RenderComponent<Index>();

// Act
cut.Find("#CustomButton").Click();

// Assert
Assert.Equal(ToastLevel.Info, toastService.Toasts.Single().ToastParameters!.TryGet<ToastLevel>(nameof(MyToastComponent.Status)));
}
}
}
9 changes: 8 additions & 1 deletion src/Blazored.Toast.TestExtensions/InMemoryToast.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Blazored.Toast.Services;
using Microsoft.AspNetCore.Components;
using System;

namespace Blazored.Toast.TestExtensions
{
Expand All @@ -9,6 +8,8 @@ public class InMemoryToast
public Type ToastType { get; set; }
public ToastLevel ToastLevel { get; }
public RenderFragment? Message { get; }

public ToastParameters? ToastParameters { get; }

public InMemoryToast(Type toastType, ToastLevel toastLevel, RenderFragment message)
{
Expand All @@ -21,5 +22,11 @@ public InMemoryToast(Type toastType)
{
ToastType = toastType;
}

public InMemoryToast(Type toastType, ToastParameters toastParameters)
{
ToastType = toastType;
ToastParameters = toastParameters;
}
}
}
4 changes: 2 additions & 2 deletions src/Blazored.Toast.TestExtensions/InMemoryToastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public void ShowToast<TComponent>() where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));

public void ShowToast<TComponent>(ToastParameters parameters) where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));
=> _toasts.Add(new InMemoryToast(typeof(TComponent), parameters));

public void ShowToast<TComponent>(Action<ToastSettings>? settings) where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));

public void ShowToast<TComponent>(ToastParameters parameters, Action<ToastSettings>? settings) where TComponent : IComponent
=> _toasts.Add(new InMemoryToast(typeof(TComponent)));
=> _toasts.Add(new InMemoryToast(typeof(TComponent), parameters));

public void ShowError(string message, Action<ToastSettings>? settings = null)
=> ShowToast(ToastLevel.Error, message, settings);
Expand Down

0 comments on commit bc95f20

Please sign in to comment.