Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

228 - Adds toast parameters to InMemoryToast to enable better testing #248

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -9,25 +9,25 @@
private readonly List<InMemoryToast> _toasts = new();
public IReadOnlyList<InMemoryToast> Toasts => _toasts;

public event Action<Type, ToastParameters?, Action<ToastSettings>?>? OnShowComponent;

Check warning on line 12 in src/Blazored.Toast.TestExtensions/InMemoryToastService.cs

View workflow job for this annotation

GitHub Actions / Build and test

The event 'InMemoryToastService.OnShowComponent' is never used
public event Action<ToastLevel, RenderFragment, Action<ToastSettings>?>? OnShow;

Check warning on line 13 in src/Blazored.Toast.TestExtensions/InMemoryToastService.cs

View workflow job for this annotation

GitHub Actions / Build and test

The event 'InMemoryToastService.OnShow' is never used

Check warning on line 13 in src/Blazored.Toast.TestExtensions/InMemoryToastService.cs

View workflow job for this annotation

GitHub Actions / Build and test

The event 'InMemoryToastService.OnShow' is never used
public event Action? OnClearAll;

Check warning on line 14 in src/Blazored.Toast.TestExtensions/InMemoryToastService.cs

View workflow job for this annotation

GitHub Actions / Build and test

The event 'InMemoryToastService.OnClearAll' is never used
public event Action<ToastLevel>? OnClearToasts;

Check warning on line 15 in src/Blazored.Toast.TestExtensions/InMemoryToastService.cs

View workflow job for this annotation

GitHub Actions / Build and test

The event 'InMemoryToastService.OnClearToasts' is never used
public event Action? OnClearCustomToasts;

Check warning on line 16 in src/Blazored.Toast.TestExtensions/InMemoryToastService.cs

View workflow job for this annotation

GitHub Actions / Build and test

The event 'InMemoryToastService.OnClearCustomToasts' is never used

Check warning on line 16 in src/Blazored.Toast.TestExtensions/InMemoryToastService.cs

View workflow job for this annotation

GitHub Actions / Build and test

The event 'InMemoryToastService.OnClearCustomToasts' is never used
public event Action? OnClearQueue;

Check warning on line 17 in src/Blazored.Toast.TestExtensions/InMemoryToastService.cs

View workflow job for this annotation

GitHub Actions / Build and test

The event 'InMemoryToastService.OnClearQueue' is never used
public event Action<ToastLevel>? OnClearQueueToasts;

Check warning on line 18 in src/Blazored.Toast.TestExtensions/InMemoryToastService.cs

View workflow job for this annotation

GitHub Actions / Build and test

The event 'InMemoryToastService.OnClearQueueToasts' is never used

Check warning on line 18 in src/Blazored.Toast.TestExtensions/InMemoryToastService.cs

View workflow job for this annotation

GitHub Actions / Build and test

The event 'InMemoryToastService.OnClearQueueToasts' is never used

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
Loading