Skip to content
New issue

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

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

Already on GitHub? # to your account

Added bUnit extention and test examples to Toast #117

Merged
merged 12 commits into from
Aug 6, 2021
Merged
2 changes: 1 addition & 1 deletion .github/workflows/ci-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
types: [ published ]

env:
NETCORE_VERSION: '3.1.301'
NETCORE_VERSION: '5.0.x'
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_CLI_TELEMETRY_OPTOUT: true
PROJECT_NAME: Toast
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/ci-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ on:

env:
PROJECT_NAME: Blazored.Toast
NETCORE_VERSION: '3.1.301'

NETCORE_VERSION: '5.0.x'
jobs:
build:
name: Build and test
Expand All @@ -24,7 +23,9 @@ jobs:
run: dotnet restore

- name: Building project...
run: dotnet build --configuration Release --no-restore src/$PROJECT_NAME/$PROJECT_NAME.csproj
run: |
dotnet build --configuration Release --no-restore src/$PROJECT_NAME/$PROJECT_NAME.csproj
dotnet build --configuration Release --no-restore src/$PROJECT_NAME.TestExtensions/$PROJECT_NAME.TestExtensions.csproj

- name: Testing...
run: dotnet test --no-restore --verbosity normal
46 changes: 46 additions & 0 deletions Blazored.Toast.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazored.Toast.Tests", "tests\Blazored.Toast.Tests\Blazored.Toast.Tests.csproj", "{5E396684-2ED7-4D93-8AAA-B57291C6ABED}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{AAC2415B-57A1-4091-89E9-842C0483CC34}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "bUnitExample", "samples\bUnitExample\bUnitExample.csproj", "{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazored.Toast.TestExtensions", "src\Blazored.Toast.TestExtensions\Blazored.Toast.TestExtensions.csproj", "{942FBF78-648B-47C4-922F-503CF11B1EAE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -65,13 +73,51 @@ Global
{011F9D7D-38DC-436C-9597-CDF701683608}.Release|x64.Build.0 = Release|Any CPU
{011F9D7D-38DC-436C-9597-CDF701683608}.Release|x86.ActiveCfg = Release|Any CPU
{011F9D7D-38DC-436C-9597-CDF701683608}.Release|x86.Build.0 = Release|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Debug|x64.ActiveCfg = Debug|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Debug|x64.Build.0 = Debug|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Debug|x86.ActiveCfg = Debug|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Debug|x86.Build.0 = Debug|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Release|Any CPU.Build.0 = Release|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Release|x64.ActiveCfg = Release|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Release|x64.Build.0 = Release|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Release|x86.ActiveCfg = Release|Any CPU
{5E396684-2ED7-4D93-8AAA-B57291C6ABED}.Release|x86.Build.0 = Release|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Debug|x64.ActiveCfg = Debug|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Debug|x64.Build.0 = Debug|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Debug|x86.ActiveCfg = Debug|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Debug|x86.Build.0 = Debug|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Release|Any CPU.Build.0 = Release|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Release|x64.ActiveCfg = Release|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Release|x64.Build.0 = Release|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Release|x86.ActiveCfg = Release|Any CPU
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC}.Release|x86.Build.0 = Release|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Debug|x64.ActiveCfg = Debug|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Debug|x64.Build.0 = Debug|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Debug|x86.ActiveCfg = Debug|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Debug|x86.Build.0 = Debug|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Release|Any CPU.Build.0 = Release|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Release|x64.ActiveCfg = Release|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Release|x64.Build.0 = Release|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Release|x86.ActiveCfg = Release|Any CPU
{942FBF78-648B-47C4-922F-503CF11B1EAE}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6622CD34-5B0D-43EC-B86E-3A671BE72104} = {DAD8AEBC-BDBA-4EFB-A8EA-971624D2E333}
{011F9D7D-38DC-436C-9597-CDF701683608} = {DAD8AEBC-BDBA-4EFB-A8EA-971624D2E333}
{5E396684-2ED7-4D93-8AAA-B57291C6ABED} = {AAC2415B-57A1-4091-89E9-842C0483CC34}
{FA280CA2-87E1-4EC3-BF6D-9E134B62F0CC} = {DAD8AEBC-BDBA-4EFB-A8EA-971624D2E333}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A209D472-2B33-4B1C-8FE6-8F53A0DC6901}
Expand Down
4 changes: 2 additions & 2 deletions samples/BlazorWebAssembly/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

<h1>Hello, Blazor Toast!</h1>

<button class="btn btn-info" @onclick="@(() => toastService.ShowInfo("I'm an INFO message"))">Info Toast</button>
<button class="btn btn-success" @onclick="@(() => toastService.ShowSuccess("I'm a SUCCESS message with a custom heading", "Congratulations!"))">Success Toast</button>
<button class="btn btn-info" id="InfoButton" @onclick="@(() => toastService.ShowInfo("I'm an INFO message"))">Info Toast</button>
<button class="btn btn-success" id="SuccessButton" @onclick="@(() => toastService.ShowSuccess("I'm a SUCCESS message with a custom heading", "Congratulations!"))">Success Toast</button>
<button class="btn btn-warning" @onclick="@(() => toastService.ShowWarning("I'm a WARNING message"))">Warning Toast</button>
<button class="btn btn-danger" @onclick="@(() => toastService.ShowError("I'm an ERROR message"))">Error Toast</button>
<button class="btn btn-info" @onclick="@OnShowHtml">Info Toast with HTML</button>
Expand Down
159 changes: 159 additions & 0 deletions samples/bUnitExample/BlazoredToastTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using Blazored.Toast;
using Blazored.Toast.Services;
using Bunit;
using Xunit;

namespace bUnitExample
{
public class BlazoredToastTests : TestContext
{
[Fact]
public void DisplaysToast()
{
// Arrange
var toastService = this.AddBlazoredToast();
var cut = RenderComponent<BlazorWebAssembly.Pages.Index>();

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

// Assert
Assert.True(toastService.ToastCountIsOne());
}

[Fact]
public void DisplaysZeroToasts()
{
// Arrange Act
var toastService = this.AddBlazoredToast();
RenderComponent<BlazorWebAssembly.Pages.Index>();

// Assert
Assert.True(toastService.ToastCountIsZero());
}

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

// Act
var button = cut.Find("#InfoButton");
button.Click();
button.Click();

// Assert
Assert.True(toastService.ToastCountIs(2));
}

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

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

// Assert
Assert.True(toastService.ToastCountIsOneWithLevel(ToastLevel.Info));
}

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

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

// Assert
Assert.True(toastService.ToastCountIsOneWithMessage("I'm an INFO message"));
}

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

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

// Assert
Assert.True(toastService.ToastCountIsOneWithHeading("Congratulations!"));
}

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

// Act
cut.Find("#InfoButton").Click();
cut.Find("#SuccessButton").Click();

// Assert
Assert.Collection(toastService.Toasts,
_ => Assert.True(_.HasLevel(ToastLevel.Info)),
_ => Assert.True(_.HasLevel(ToastLevel.Success)));
}

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

// Act
cut.Find("#InfoButton").Click();
cut.Find("#SuccessButton").Click();

// Assert
Assert.Collection(toastService.Toasts,
_ => Assert.True(_.HasMessage("I'm an INFO message")),
_ => Assert.True(_.HasMessage("I'm a SUCCESS message with a custom heading")));
}

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

// Act
cut.Find("#InfoButton").Click();
cut.Find("#SuccessButton").Click();

// Assert
Assert.Collection(toastService.Toasts,
_ => Assert.True(_.HasHeading("Info")),
_ => Assert.True(_.HasHeading("Congratulations!")));
}

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

// Act
cut.Find("#InfoButton").Click();
cut.Find("#SuccessButton").Click();

// Assert
Assert.NotEmpty(toastService.Toasts);
}
}
}
29 changes: 29 additions & 0 deletions samples/bUnitExample/bUnitExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="bunit.core" Version="1.1.5" />
<PackageReference Include="bunit.web" Version="1.1.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Blazored.Toast.TestExtensions\Blazored.Toast.TestExtensions.csproj" />
<ProjectReference Include="..\BlazorWebAssembly\BlazorWebAssembly.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions src/Blazored.Toast.TestExtensions/BUnitToastTestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Blazored.Toast.Services;
using Blazored.Toast.TestExtensions;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Diagnostics.CodeAnalysis;

namespace Bunit
{
[ExcludeFromCodeCoverage]
public static class BUnitToastTestExtensions
{
public static InMemoryToastService AddBlazoredToast(this TestContext context)
{
if (context is null)
throw new ArgumentNullException(nameof(context));

context.Services
.AddSingleton<IToastService, InMemoryToastService>();

return (InMemoryToastService)context.Services.GetService<IToastService>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="bunit" Version="1.1.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Blazored.Toast\Blazored.Toast.csproj" />
</ItemGroup>

</Project>
47 changes: 47 additions & 0 deletions src/Blazored.Toast.TestExtensions/InMemoryToast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Blazored.Toast.Services;
using Bunit;
using Microsoft.AspNetCore.Components;

namespace Blazored.Toast.TestExtensions
{
public class InMemoryToast
{
private readonly ToastLevel _toastLevel;
private readonly RenderFragment _message;
private readonly string _heading;

public InMemoryToast(ToastLevel toastLevel, RenderFragment message, string heading)
{
_toastLevel = toastLevel;
_message = message;
_heading = heading;
}

/// <summary>
/// Checks toast element is rendered with level.
/// </summary>
/// <param name="level">The toast level expected.</param>
public bool HasLevel(ToastLevel level)
=> _toastLevel == level;

/// <summary>
/// Checks toast element is rendered with message.
/// </summary>
/// <param name="message">The toast message expected.</param>
public bool HasMessage(string message)
{
using var ctx = new TestContext();
var cut = ctx.Render(_message);

cut.MarkupMatches(message);
return true;
}

/// <summary>
/// Checks toast element is rendered with heading.
/// </summary>
/// <param name="heading">The toast heading expected.</param>
public bool HasHeading(string heading)
=> _heading == heading;
}
}
Loading