Skip to content

Commit 3ab739d

Browse files
committed
Added Tests
1 parent f721769 commit 3ab739d

File tree

5 files changed

+105
-12
lines changed

5 files changed

+105
-12
lines changed

LinkDotNet.Blog.IntegrationTests/LinkDotNet.Blog.IntegrationTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<PackageReference Include="FluentAssertions" Version="5.10.3" />
1212
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.7" />
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
14+
<PackageReference Include="Moq" Version="4.16.1" />
1415
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.25.0.33663">
1516
<PrivateAssets>all</PrivateAssets>
1617
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Blazored.Toast.Services;
2+
using Bunit;
3+
using Bunit.TestDoubles;
4+
using FluentAssertions;
5+
using LinkDotNet.Blog.Web.Shared.Admin;
6+
using LinkDotNet.Infrastructure.Persistence;
7+
using Microsoft.AspNetCore.Components;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Moq;
10+
using Xunit;
11+
12+
namespace LinkDotNet.Blog.IntegrationTests.Web.Shared.Admin
13+
{
14+
public class BlogPostAdminActionsTests
15+
{
16+
[Fact]
17+
public void ShouldDeleteBlogPostWhenOkClicked()
18+
{
19+
const string blogPostId = "2";
20+
var repositoryMock = new Mock<IRepository>();
21+
using var ctx = new TestContext();
22+
ctx.AddTestAuthorization().SetAuthorized("s");
23+
ctx.Services.AddSingleton(repositoryMock.Object);
24+
ctx.Services.AddSingleton(new Mock<IToastService>().Object);
25+
var cut = ctx.RenderComponent<BlogPostAdminActions>(s => s.Add(p => p.BlogPostId, blogPostId));
26+
cut.Find("#delete-blogpost").Click();
27+
28+
cut.Find("#ok").Click();
29+
30+
repositoryMock.Verify(r => r.DeleteAsync(blogPostId), Times.Once);
31+
}
32+
33+
[Fact]
34+
public void ShouldNotDeleteBlogPostWhenCancelClicked()
35+
{
36+
const string blogPostId = "2";
37+
var repositoryMock = new Mock<IRepository>();
38+
using var ctx = new TestContext();
39+
ctx.AddTestAuthorization().SetAuthorized("s");
40+
ctx.Services.AddSingleton(repositoryMock.Object);
41+
ctx.Services.AddSingleton(new Mock<IToastService>().Object);
42+
var cut = ctx.RenderComponent<BlogPostAdminActions>(s => s.Add(p => p.BlogPostId, blogPostId));
43+
cut.Find("#delete-blogpost").Click();
44+
45+
cut.Find("#cancel").Click();
46+
47+
repositoryMock.Verify(r => r.DeleteAsync(blogPostId), Times.Never);
48+
}
49+
50+
[Fact]
51+
public void ShouldGoToEditPageOnEditClick()
52+
{
53+
const string blogPostId = "2";
54+
var repositoryMock = new Mock<IRepository>();
55+
using var ctx = new TestContext();
56+
ctx.AddTestAuthorization().SetAuthorized("s");
57+
ctx.Services.AddSingleton(repositoryMock.Object);
58+
ctx.Services.AddSingleton(new Mock<IToastService>().Object);
59+
var navigationManager = ctx.Services.GetRequiredService<NavigationManager>();
60+
var cut = ctx.RenderComponent<BlogPostAdminActions>(s => s.Add(p => p.BlogPostId, blogPostId));
61+
62+
cut.Find("#edit-blogpost").Click();
63+
64+
navigationManager.Uri.Should().EndWith($"update/{blogPostId}");
65+
}
66+
}
67+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Bunit;
2+
using FluentAssertions;
3+
using LinkDotNet.Blog.Web.Shared;
4+
using Xunit;
5+
6+
namespace LinkDotNet.Blog.IntegrationTests.Web.Shared
7+
{
8+
public class ConfirmDialogTests
9+
{
10+
[Fact]
11+
public void ShouldInvokeEventOnOkClick()
12+
{
13+
var okWasClicked = false;
14+
using var ctx = new TestContext();
15+
var cut = ctx.RenderComponent<ConfirmDialog>(
16+
b => b
17+
.Add(p => p.OnYesPressed, _ => okWasClicked = true));
18+
cut.Instance.Open();
19+
20+
cut.Find("#ok").Click();
21+
22+
okWasClicked.Should().BeTrue();
23+
}
24+
}
25+
}

LinkDotNet.Blog.Web/Shared/Admin/BlogPostAdminActions.razor

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
<AuthorizeView>
77
<div class="blogpost-admin">
8-
<button type="button" class="btn btn-primary" @onclick="EditBlogPost" aria-label="edit"><i class="fas fa-pen-square"></i> Edit
8+
<button id="edit-blogpost" type="button" class="btn btn-primary" @onclick="EditBlogPost" aria-label="edit"><i
9+
class="fas
10+
fa-pen-square"></i> Edit
911
Blogpost</button>
10-
<button type="button" class="btn btn-danger" @onclick="ShowConfirmDialog" aria-label="delete"><i class="fas fa-trash"></i> Delete
12+
<button id="delete-blogpost" type="button" class="btn btn-danger" @onclick="ShowConfirmDialog" aria-label="delete"><i class="fas fa-trash"></i> Delete
1113
Blogpost</button>
1214
</div>
1315
<ConfirmDialog @ref="ConfirmDialog" Title="Delete Blog Post" Content="Do you want to delete the Blog Post?" OnYesPressed="@DeleteBlogPostAsync">
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<ModalDialog @ref="ModalDialog" Title="@Title">
22
<h3>@Content</h3>
33
<div class="actions">
4-
<button @onclick="OnYesButtonPressed" type="button" class="btn btn-primary">Ok</button>
5-
<button @onclick="OnNoButtonPressed" type="button" class="btn btn-secondary">Cancel</button>
4+
<button id="ok" @onclick="OnYesButtonPressed" type="button" class="btn btn-primary">Ok</button>
5+
<button id="cancel" @onclick="OnNoButtonPressed" type="button" class="btn btn-secondary">Cancel</button>
66
</div>
77
</ModalDialog>
88

@@ -15,10 +15,7 @@
1515

1616
[Parameter]
1717
public EventCallback OnYesPressed { get; set; }
18-
19-
[Parameter]
20-
public EventCallback OnNoPressed { get; set; }
21-
18+
2219
private ModalDialog ModalDialog { get; set; }
2320

2421
private async Task OnYesButtonPressed()
@@ -27,13 +24,14 @@
2724
await OnYesPressed.InvokeAsync();
2825
}
2926

30-
private void OnNoButtonPressed()
27+
public void Open()
3128
{
32-
ModalDialog.Close();
29+
ModalDialog.Open();
3330
}
3431

35-
public void Open()
32+
private void OnNoButtonPressed()
3633
{
37-
ModalDialog.Open();
34+
ModalDialog.Close();
3835
}
36+
3937
}

0 commit comments

Comments
 (0)