Skip to content

Commit 61d69bf

Browse files
committed
Added test for prevention
1 parent 83086dd commit 61d69bf

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/LinkDotNet.Blog.Web/Features/Admin/BlogPostEditor/Components/CreateNewBlogPost.razor

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@
127127

128128
private void PreventNavigationWhenDirty(LocationChangingContext context)
129129
{
130-
if (model.IsDirty && !ignoreIsDirty)
131-
{
132-
context.PreventNavigation();
133-
ToastService.ShowWarning(
134-
"You have unsaved changes. Click on this message to ignore this warning the next time",
135-
onClick: () => ignoreIsDirty = true);
136-
}
130+
if (!model.IsDirty || ignoreIsDirty)
131+
return;
132+
133+
context.PreventNavigation();
134+
ToastService.ShowWarning(
135+
"You have unsaved changes. Click on this message to ignore this warning the next time",
136+
onClick: () => ignoreIsDirty = true);
137137
}
138138
}

tests/LinkDotNet.Blog.UnitTests/Web/Features/Admin/BlogPostEditor/Components/CreateNewBlogPostTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,21 @@ public void ShouldStopInternalNavigationWhenDirty()
192192

193193
fakeNavigationManager.History.Count.Should().Be(1);
194194
}
195+
196+
[Fact(Skip = "Need bUnit > 1.9.8")]
197+
public void ShouldNotPreventWhenToastIsClicked()
198+
{
199+
var toastMock = new Mock<IToastService>();
200+
Services.AddScoped(_ => toastMock.Object);
201+
toastMock.Setup(t => t.ShowWarning(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Action>()))
202+
.Callback<string, string, Action>((_, _, onClick) => onClick());
203+
var cut = RenderComponent<CreateNewBlogPost>();
204+
cut.Find("#tags").Change("Hey");
205+
var fakeNavigationManager = Services.GetRequiredService<FakeNavigationManager>();
206+
fakeNavigationManager.NavigateTo("/internal");
207+
208+
fakeNavigationManager.NavigateTo("/internal");
209+
210+
fakeNavigationManager.History.Count.Should().Be(2);
211+
}
195212
}

0 commit comments

Comments
 (0)