Skip to content

Commit

Permalink
refactor: Migrate bUnit from v1 to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Mar 9, 2024
1 parent 4107baa commit 2e3fd14
Show file tree
Hide file tree
Showing 51 changed files with 285 additions and 285 deletions.
4 changes: 2 additions & 2 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="bunit.generators" Version="1.27.11-preview" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="bunit" Version="1.26.64" />
<PackageReference Include="bunit.generators" Version="2.0.22-preview" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="bunit" Version="2.0.22-preview" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace LinkDotNet.Blog.IntegrationTests.Web.Features.AboutMe.Components;

public sealed class TalksTests : SqlDatabaseTestBase<Talk>, IDisposable
{
private readonly TestContext ctx = new();
private readonly BunitContext ctx = new();

public TalksTests()
{
Expand All @@ -20,7 +20,7 @@ public TalksTests()
[Fact]
public void WhenAddingTalkThenDisplayedToUser()
{
var cut = ctx.RenderComponent<Talks>(
var cut = ctx.Render<Talks>(
p => p.Add(s => s.ShowAdminActions, true));
cut.Find("#add-talk").Click();
cut.Find("#talk-title").Change("title");
Expand All @@ -42,7 +42,7 @@ public async Task LoadEarlierSavedTalks()
await Repository.StoreAsync(new TalkBuilder().Build());
await Repository.StoreAsync(new TalkBuilder().Build());

var cut = ctx.RenderComponent<Talks>();
var cut = ctx.Render<Talks>();

cut.WaitForComponents<TalkEntry>().Count.Should().Be(2);
}
Expand All @@ -51,7 +51,7 @@ public async Task LoadEarlierSavedTalks()
public async Task WhenUserClickDeleteButtonThenDeleted()
{
await Repository.StoreAsync(new TalkBuilder().Build());
var cut = ctx.RenderComponent<Talks>(
var cut = ctx.Render<Talks>(
p => p.Add(s => s.ShowAdminActions, true));

cut.WaitForComponent<TalkEntry>().Find("#talk-delete").Click();
Expand All @@ -67,7 +67,7 @@ public async Task TalksAreOrderFromNewestToLatest()
await Repository.StoreAsync(new TalkBuilder().WithPublishedDate(new DateTime(2021, 1, 1)).Build());
await Repository.StoreAsync(new TalkBuilder().WithPublishedDate(new DateTime(2022, 1, 1)).Build());

var cut = ctx.RenderComponent<Talks>(
var cut = ctx.Render<Talks>(
p => p.Add(s => s.ShowAdminActions, true));

var talks = cut.WaitForComponents<TalkEntry>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public class CreateNewBlogPostPageTests : SqlDatabaseTestBase<BlogPost>
[Fact]
public async Task ShouldSaveBlogPostOnSave()
{
using var ctx = new TestContext();
using var ctx = new BunitContext();
var toastService = Substitute.For<IToastService>();
ctx.JSInterop.SetupVoid("hljs.highlightAll");
ctx.AddTestAuthorization().SetAuthorized("some username");
ctx.AddAuthorization().SetAuthorized("some username");
ctx.Services.AddScoped(_ => Repository);
ctx.Services.AddScoped(_ => toastService);
ctx.ComponentFactories.AddStub<UploadFile>();
ctx.Services.AddScoped(_ => Substitute.For<IFileProcessor>());
using var cut = ctx.RenderComponent<CreateBlogPost>();
using var cut = ctx.Render<CreateBlogPost>();
var newBlogPost = cut.FindComponent<CreateNewBlogPost>();

TriggerNewBlogPost(newBlogPost);
Expand All @@ -37,22 +37,22 @@ public async Task ShouldSaveBlogPostOnSave()
[Fact]
public async Task ShouldSetContentFromFile()
{
using var ctx = new TestContext();
using var ctx = new BunitContext();
const string contentFromFile = "content";
ctx.JSInterop.SetupVoid("hljs.highlightAll");
ctx.AddTestAuthorization().SetAuthorized("some username");
ctx.AddAuthorization().SetAuthorized("some username");
ctx.Services.AddScoped(_ => Repository);
ctx.Services.AddScoped(_ => Substitute.For<IToastService>());
var args = SetupUploadFile(contentFromFile, ctx);
var cut = ctx.RenderComponent<CreateNewBlogPost>();
var cut = ctx.Render<CreateNewBlogPost>();
var uploadFile = cut.FindComponent<UploadFile>();

await uploadFile.InvokeAsync(() => cut.FindComponent<InputFile>().Instance.OnChange.InvokeAsync(args));

cut.Find("#content").TextContent.Should().Be(contentFromFile);
}

private static void TriggerNewBlogPost(IRenderedComponent<CreateNewBlogPost> cut)
private static void TriggerNewBlogPost(RenderedComponent<CreateNewBlogPost> cut)
{
cut.Find("#title").Input("My Title");
cut.Find("#short").Input("My short Description");
Expand All @@ -64,7 +64,7 @@ private static void TriggerNewBlogPost(IRenderedComponent<CreateNewBlogPost> cut
cut.Find("form").Submit();
}

private static InputFileChangeEventArgs SetupUploadFile(string contentFromFile, TestContext ctx)
private static InputFileChangeEventArgs SetupUploadFile(string contentFromFile, BunitContext ctx)
{
var file = Substitute.For<IBrowserFile>();
var fileProcessor = Substitute.For<IFileProcessor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ public class UpdateBlogPostPageTests : SqlDatabaseTestBase<BlogPost>
[Fact]
public async Task ShouldSaveBlogPostOnSave()
{
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.JSInterop.SetupVoid("hljs.highlightAll");
var toastService = Substitute.For<IToastService>();
var blogPost = new BlogPostBuilder().WithTitle("Title").WithShortDescription("Sub").Build();
await Repository.StoreAsync(blogPost);
ctx.AddTestAuthorization().SetAuthorized("some username");
ctx.AddAuthorization().SetAuthorized("some username");
ctx.Services.AddScoped(_ => Repository);
ctx.Services.AddScoped(_ => toastService);
ctx.ComponentFactories.AddStub<UploadFile>();
using var cut = ctx.RenderComponent<UpdateBlogPostPage>(
using var cut = ctx.Render<UpdateBlogPostPage>(
p => p.Add(s => s.BlogPostId, blogPost.Id));
var newBlogPost = cut.FindComponent<CreateNewBlogPost>();

Expand All @@ -39,18 +39,18 @@ public async Task ShouldSaveBlogPostOnSave()
[Fact]
public void ShouldThrowWhenNoIdProvided()
{
using var ctx = new TestContext();
ctx.AddTestAuthorization().SetAuthorized("some username");
using var ctx = new BunitContext();
ctx.AddAuthorization().SetAuthorized("some username");
ctx.Services.AddScoped(_ => Repository);
ctx.Services.AddScoped(_ => Substitute.For<IToastService>());

Action act = () => ctx.RenderComponent<UpdateBlogPostPage>(
Action act = () => ctx.Render<UpdateBlogPostPage>(
p => p.Add(s => s.BlogPostId, null));

act.Should().ThrowExactly<ArgumentNullException>();
}

private static void TriggerUpdate(IRenderedFragment cut)
private static void TriggerUpdate(RenderedFragment cut)
{
cut.Find("#short").Input("My new Description");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public async Task ShouldShowCounts()
{
var blogPost = new BlogPostBuilder().WithTitle("I was clicked").WithLikes(2).Build();
await Repository.StoreAsync(blogPost);
using var ctx = new TestContext();
using var ctx = new BunitContext();
RegisterRepositories(ctx);
await SaveBlogPostArticleClicked(blogPost.Id, 10);

var cut = ctx.RenderComponent<VisitCountPerPage>();
var cut = ctx.Render<VisitCountPerPage>();

var elements = cut.WaitForElements("td");
elements.Count.Should().Be(3);
Expand Down Expand Up @@ -52,10 +52,10 @@ public async Task ShouldFilterByDate()
{ BlogPostId = blogPost1.Id, DateClicked = new DateOnly(2021, 1, 1), Clicks = 1 };
await DbContext.BlogPostRecords.AddRangeAsync(clicked1, clicked2, clicked3, clicked4);
await DbContext.SaveChangesAsync();
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.ComponentFactories.AddStub<DateRangeSelector>();
RegisterRepositories(ctx);
var cut = ctx.RenderComponent<VisitCountPerPage>();
var cut = ctx.Render<VisitCountPerPage>();
var filter = new Filter { StartDate = new DateOnly(2019, 1, 1), EndDate = new DateOnly(2020, 12, 31) };

await cut.InvokeAsync(() => cut.FindComponent<DateRangeSelectorStub>().Instance.FilterChanged.InvokeAsync(filter));
Expand Down Expand Up @@ -84,16 +84,16 @@ public async Task ShouldShowTotalClickCount()
{ BlogPostId = blogPost2.Id, DateClicked = DateOnly.MinValue, Clicks = 1 };
await DbContext.BlogPostRecords.AddRangeAsync(clicked1, clicked2, clicked3);
await DbContext.SaveChangesAsync();
using var ctx = new TestContext();
using var ctx = new BunitContext();
RegisterRepositories(ctx);

var cut = ctx.RenderComponent<VisitCountPerPage>();
var cut = ctx.Render<VisitCountPerPage>();

cut.WaitForElement("td");
cut.Find("#total-clicks").TextContent.Should().Be("4 clicks in total");
}

private void RegisterRepositories(TestContextBase ctx)
private void RegisterRepositories(BunitContext ctx)
{
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => new Repository<BlogPost>(DbContextFactory, Substitute.For<ILogger<Repository<BlogPost>>>()));
ctx.Services.AddScoped<IRepository<BlogPostRecord>>(_ => new Repository<BlogPostRecord>(DbContextFactory, Substitute.For<ILogger<Repository<BlogPostRecord>>>()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public async Task ShouldOnlyShowPublishedPosts()
var unpublishedPost = new BlogPostBuilder().WithTitle("Not published").IsPublished(false).Build();
await Repository.StoreAsync(publishedPost);
await Repository.StoreAsync(unpublishedPost);
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
ctx.Services.AddScoped(_ => Repository);
var cut = ctx.RenderComponent<DraftBlogPostPage>();
var cut = ctx.Render<DraftBlogPostPage>();
cut.WaitForElement(".blog-card");

var blogPosts = cut.FindComponents<ShortBlogPost>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public class ArchivePageTests : SqlDatabaseTestBase<BlogPost>
[Fact]
public async Task ShouldDisplayAllBlogPosts()
{
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.Services.AddScoped(_ => Repository);
await Repository.StoreAsync(CreateBlogPost(new DateTime(2021, 1, 1), "Blog Post 1"));
await Repository.StoreAsync(CreateBlogPost(new DateTime(2021, 2, 1), "Blog Post 2"));
await Repository.StoreAsync(CreateBlogPost(new DateTime(2022, 1, 1), "Blog Post 3"));

var cut = ctx.RenderComponent<ArchivePage>();
var cut = ctx.Render<ArchivePage>();

cut.WaitForElements("h2");
var yearHeader = cut.FindAll("h2");
Expand All @@ -41,14 +41,14 @@ public async Task ShouldDisplayAllBlogPosts()
[Fact]
public async Task ShouldOnlyShowPublishedBlogPosts()
{
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.Services.AddScoped(_ => Repository);
var publishedBlogPost = new BlogPostBuilder().WithUpdatedDate(new DateTime(2022, 1, 1)).IsPublished().Build();
var unPublishedBlogPost = new BlogPostBuilder().WithUpdatedDate(new DateTime(2022, 1, 1)).IsPublished(false).Build();
await Repository.StoreAsync(publishedBlogPost);
await Repository.StoreAsync(unPublishedBlogPost);

var cut = ctx.RenderComponent<ArchivePage>();
var cut = ctx.Render<ArchivePage>();

cut.WaitForElements("h2");
cut.FindAll("h2").Should().HaveCount(1);
Expand All @@ -57,12 +57,12 @@ public async Task ShouldOnlyShowPublishedBlogPosts()
[Fact]
public async Task ShouldShowTotalAmountOfBlogPosts()
{
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.Services.AddScoped(_ => Repository);
await Repository.StoreAsync(CreateBlogPost(new DateTime(2021, 1, 1), "Blog Post 1"));
await Repository.StoreAsync(CreateBlogPost(new DateTime(2021, 2, 1), "Blog Post 2"));

var cut = ctx.RenderComponent<ArchivePage>();
var cut = ctx.Render<ArchivePage>();

cut.WaitForElements("h2");
cut.Find("h3").TextContent.Should().Be("Archive (2 posts)");
Expand All @@ -71,21 +71,21 @@ public async Task ShouldShowTotalAmountOfBlogPosts()
[Fact]
public void ShouldShowLoading()
{
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => new SlowRepository());

var cut = ctx.RenderComponent<ArchivePage>();
var cut = ctx.Render<ArchivePage>();

cut.FindComponents<Loading>().Count.Should().Be(1);
}

[Fact]
public void ShouldSetOgData()
{
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.Services.AddScoped(_ => Repository);

var cut = ctx.RenderComponent<ArchivePage>();
var cut = ctx.Render<ArchivePage>();

var ogData = cut.FindComponent<OgData>().Instance;
ogData.Title.Should().Contain("Archive");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public async Task ShouldShowAllBlogPostsWithLatestOneFirst()
var newestBlogPost = new BlogPostBuilder().WithTitle("New").Build();
await Repository.StoreAsync(oldestBlogPost);
await Repository.StoreAsync(newestBlogPost);
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
RegisterComponents(ctx);
var cut = ctx.RenderComponent<Index>();
var cut = ctx.Render<Index>();
cut.WaitForElement(".blog-card");

var blogPosts = cut.FindComponents<ShortBlogPost>();
Expand All @@ -40,10 +40,10 @@ public async Task ShouldOnlyShowPublishedPosts()
var unpublishedPost = new BlogPostBuilder().WithTitle("Not published").IsPublished(false).Build();
await Repository.StoreAsync(publishedPost);
await Repository.StoreAsync(unpublishedPost);
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
RegisterComponents(ctx);
var cut = ctx.RenderComponent<Index>();
var cut = ctx.Render<Index>();
cut.WaitForElement(".blog-card");

var blogPosts = cut.FindComponents<ShortBlogPost>();
Expand All @@ -56,10 +56,10 @@ public async Task ShouldOnlyShowPublishedPosts()
public async Task ShouldOnlyLoadTenEntities()
{
await CreatePublishedBlogPosts(11);
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
RegisterComponents(ctx);
var cut = ctx.RenderComponent<Index>();
var cut = ctx.Render<Index>();
cut.WaitForElement(".blog-card");

var blogPosts = cut.FindComponents<ShortBlogPost>();
Expand All @@ -71,11 +71,11 @@ public async Task ShouldOnlyLoadTenEntities()
public async Task ShouldLoadOnlyItemsInPage()
{
await CreatePublishedBlogPosts(11);
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
RegisterComponents(ctx);

var cut = ctx.RenderComponent<Index>(
var cut = ctx.Render<Index>(
p => p.Add(s => s.Page, 2));

cut.WaitForElement(".blog-card");
Expand All @@ -92,10 +92,10 @@ public async Task ShouldLoadTags()
.WithTags("C Sharp", "Tag2")
.Build();
await Repository.StoreAsync(publishedPost);
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
RegisterComponents(ctx);
var cut = ctx.RenderComponent<Index>();
var cut = ctx.Render<Index>();
cut.WaitForElement(".blog-card");

var tags = cut.FindComponent<ShortBlogPost>().FindAll(".goto-tag");
Expand All @@ -112,10 +112,10 @@ public async Task ShouldLoadTags()
[InlineData("http://localhost/relative/url", "http://localhost/relative/url")]
public void ShouldSetAbsoluteUriForOgData(string givenUri, string expectedUri)
{
using var ctx = new TestContext();
using var ctx = new BunitContext();
RegisterComponents(ctx, givenUri);

var cut = ctx.RenderComponent<Index>();
var cut = ctx.Render<Index>();

cut.WaitForComponent<OgData>().Instance.AbsolutePreviewImageUrl.Should().Be(expectedUri);
}
Expand All @@ -127,11 +127,11 @@ public void ShouldSetAbsoluteUriForOgData(string givenUri, string expectedUri)
public async Task ShouldSetPageToFirstIfOutOfRange(int? page)
{
await CreatePublishedBlogPosts(11);
using var ctx = new TestContext();
using var ctx = new BunitContext();
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
RegisterComponents(ctx);

var cut = ctx.RenderComponent<Index>(p => p.Add(
var cut = ctx.Render<Index>(p => p.Add(
i => i.Page, page));

cut.WaitForElement(".blog-card");
Expand Down Expand Up @@ -162,7 +162,7 @@ private async Task CreatePublishedBlogPosts(int amount)
}
}

private void RegisterComponents(TestContextBase ctx, string profilePictureUri = null)
private void RegisterComponents(BunitContext ctx, string profilePictureUri = null)
{
ctx.Services.AddScoped(_ => Repository);
ctx.Services.AddScoped(_ => Options.Create(CreateSampleAppConfiguration(profilePictureUri).ApplicationConfiguration));
Expand Down
Loading

0 comments on commit 2e3fd14

Please sign in to comment.