Skip to content

Commit

Permalink
Added Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Jul 7, 2021
1 parent f721769 commit 3ab739d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.25.0.33663">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Blazored.Toast.Services;
using Bunit;
using Bunit.TestDoubles;
using FluentAssertions;
using LinkDotNet.Blog.Web.Shared.Admin;
using LinkDotNet.Infrastructure.Persistence;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;

namespace LinkDotNet.Blog.IntegrationTests.Web.Shared.Admin
{
public class BlogPostAdminActionsTests
{
[Fact]
public void ShouldDeleteBlogPostWhenOkClicked()
{
const string blogPostId = "2";
var repositoryMock = new Mock<IRepository>();
using var ctx = new TestContext();
ctx.AddTestAuthorization().SetAuthorized("s");
ctx.Services.AddSingleton(repositoryMock.Object);
ctx.Services.AddSingleton(new Mock<IToastService>().Object);
var cut = ctx.RenderComponent<BlogPostAdminActions>(s => s.Add(p => p.BlogPostId, blogPostId));
cut.Find("#delete-blogpost").Click();

cut.Find("#ok").Click();

repositoryMock.Verify(r => r.DeleteAsync(blogPostId), Times.Once);
}

[Fact]
public void ShouldNotDeleteBlogPostWhenCancelClicked()
{
const string blogPostId = "2";
var repositoryMock = new Mock<IRepository>();
using var ctx = new TestContext();
ctx.AddTestAuthorization().SetAuthorized("s");
ctx.Services.AddSingleton(repositoryMock.Object);
ctx.Services.AddSingleton(new Mock<IToastService>().Object);
var cut = ctx.RenderComponent<BlogPostAdminActions>(s => s.Add(p => p.BlogPostId, blogPostId));
cut.Find("#delete-blogpost").Click();

cut.Find("#cancel").Click();

repositoryMock.Verify(r => r.DeleteAsync(blogPostId), Times.Never);
}

[Fact]
public void ShouldGoToEditPageOnEditClick()
{
const string blogPostId = "2";
var repositoryMock = new Mock<IRepository>();
using var ctx = new TestContext();
ctx.AddTestAuthorization().SetAuthorized("s");
ctx.Services.AddSingleton(repositoryMock.Object);
ctx.Services.AddSingleton(new Mock<IToastService>().Object);
var navigationManager = ctx.Services.GetRequiredService<NavigationManager>();
var cut = ctx.RenderComponent<BlogPostAdminActions>(s => s.Add(p => p.BlogPostId, blogPostId));

cut.Find("#edit-blogpost").Click();

navigationManager.Uri.Should().EndWith($"update/{blogPostId}");
}
}
}
25 changes: 25 additions & 0 deletions LinkDotNet.Blog.IntegrationTests/Web/Shared/ConfirmDialogTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Bunit;
using FluentAssertions;
using LinkDotNet.Blog.Web.Shared;
using Xunit;

namespace LinkDotNet.Blog.IntegrationTests.Web.Shared
{
public class ConfirmDialogTests
{
[Fact]
public void ShouldInvokeEventOnOkClick()
{
var okWasClicked = false;
using var ctx = new TestContext();
var cut = ctx.RenderComponent<ConfirmDialog>(
b => b
.Add(p => p.OnYesPressed, _ => okWasClicked = true));
cut.Instance.Open();

cut.Find("#ok").Click();

okWasClicked.Should().BeTrue();
}
}
}
6 changes: 4 additions & 2 deletions LinkDotNet.Blog.Web/Shared/Admin/BlogPostAdminActions.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

<AuthorizeView>
<div class="blogpost-admin">
<button type="button" class="btn btn-primary" @onclick="EditBlogPost" aria-label="edit"><i class="fas fa-pen-square"></i> Edit
<button id="edit-blogpost" type="button" class="btn btn-primary" @onclick="EditBlogPost" aria-label="edit"><i
class="fas
fa-pen-square"></i> Edit
Blogpost</button>
<button type="button" class="btn btn-danger" @onclick="ShowConfirmDialog" aria-label="delete"><i class="fas fa-trash"></i> Delete
<button id="delete-blogpost" type="button" class="btn btn-danger" @onclick="ShowConfirmDialog" aria-label="delete"><i class="fas fa-trash"></i> Delete
Blogpost</button>
</div>
<ConfirmDialog @ref="ConfirmDialog" Title="Delete Blog Post" Content="Do you want to delete the Blog Post?" OnYesPressed="@DeleteBlogPostAsync">
Expand Down
18 changes: 8 additions & 10 deletions LinkDotNet.Blog.Web/Shared/ConfirmDialog.razor
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<ModalDialog @ref="ModalDialog" Title="@Title">
<h3>@Content</h3>
<div class="actions">
<button @onclick="OnYesButtonPressed" type="button" class="btn btn-primary">Ok</button>
<button @onclick="OnNoButtonPressed" type="button" class="btn btn-secondary">Cancel</button>
<button id="ok" @onclick="OnYesButtonPressed" type="button" class="btn btn-primary">Ok</button>
<button id="cancel" @onclick="OnNoButtonPressed" type="button" class="btn btn-secondary">Cancel</button>
</div>
</ModalDialog>

Expand All @@ -15,10 +15,7 @@

[Parameter]
public EventCallback OnYesPressed { get; set; }

[Parameter]
public EventCallback OnNoPressed { get; set; }


private ModalDialog ModalDialog { get; set; }

private async Task OnYesButtonPressed()
Expand All @@ -27,13 +24,14 @@
await OnYesPressed.InvokeAsync();
}

private void OnNoButtonPressed()
public void Open()
{
ModalDialog.Close();
ModalDialog.Open();
}

public void Open()
private void OnNoButtonPressed()
{
ModalDialog.Open();
ModalDialog.Close();
}

}

0 comments on commit 3ab739d

Please sign in to comment.