From b33d2297e4a0fedc7806c3d1cc797b6276c634bf Mon Sep 17 00:00:00 2001 From: emregokrem Date: Thu, 16 May 2024 11:32:46 +0300 Subject: [PATCH] Drawer test added with TestContextBase class --- SiemensIXBlazor.Tests/DrawerTests.cs | 70 +++++++++++++++++++ SiemensIXBlazor.Tests/TestContextBase.cs | 29 ++++++++ .../Components/Drawer/Drawer.razor.cs | 4 +- 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 SiemensIXBlazor.Tests/DrawerTests.cs create mode 100644 SiemensIXBlazor.Tests/TestContextBase.cs diff --git a/SiemensIXBlazor.Tests/DrawerTests.cs b/SiemensIXBlazor.Tests/DrawerTests.cs new file mode 100644 index 0000000..2071b99 --- /dev/null +++ b/SiemensIXBlazor.Tests/DrawerTests.cs @@ -0,0 +1,70 @@ +// ----------------------------------------------------------------------- +// SPDX-FileCopyrightText: 2024 Siemens AG +// +// SPDX-License-Identifier: MIT +// +// This source code is licensed under the MIT license found in the +// LICENSE file in the root directory of this source tree. +// ----------------------------------------------------------------------- + +using Bunit; +using Microsoft.AspNetCore.Components; +using SiemensIXBlazor.Components; + +namespace SiemensIXBlazor.Tests +{ + public class DrawerTests : TestContextBase + { + [Fact] + public void DrawerRendersCorrectly() + { + // Arrange + var cut = RenderComponent( + ("Id", "testId"), + ("CloseOnClickOutside", true), + ("FullHeight", false), + ("MaxWidth", 28), + ("MinWidth", 16), + ("Show", true), + ("Width", 16) + ); + + // Assert + cut.MarkupMatches(""); + } + + [Fact] + public async Task ClosedEventWorks() + { + // Arrange + var closed = false; + var cut = RenderComponent( + ("Id", "drawer"), + ("ClosedEvent", EventCallback.Factory.Create(this, () => closed = true)) + ); + + // Act + await cut.Instance.Closed(); + + // Assert + Assert.True(closed); + } + + [Fact] + public async Task OpenedEventWorks() + { + // Arrange + var opened = false; + var cut = RenderComponent( + ("Id", "drawer"), + ("OpenedEvent", EventCallback.Factory.Create(this, () => opened = true)) + ); + + // Act + await cut.Instance.Opened(); + + // Assert + Assert.True(opened); + } + } +} \ No newline at end of file diff --git a/SiemensIXBlazor.Tests/TestContextBase.cs b/SiemensIXBlazor.Tests/TestContextBase.cs new file mode 100644 index 0000000..dea2a98 --- /dev/null +++ b/SiemensIXBlazor.Tests/TestContextBase.cs @@ -0,0 +1,29 @@ +// ----------------------------------------------------------------------- +// SPDX-FileCopyrightText: 2024 Siemens AG +// +// SPDX-License-Identifier: MIT +// +// This source code is licensed under the MIT license found in the +// LICENSE file in the root directory of this source tree. +// ----------------------------------------------------------------------- + +using Bunit; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.JSInterop; +using Moq; + +namespace SiemensIXBlazor.Tests; + +public class TestContextBase : TestContext +{ + public TestContextBase() + { + Mock jsRuntimeMock = new(); + Mock jsObjectReferenceMock = new(); + + // Mock of module import for JSRuntime + jsRuntimeMock.Setup(x => x.InvokeAsync("import", It.IsAny())) + .Returns(new ValueTask(jsObjectReferenceMock.Object)); + Services.AddSingleton(jsRuntimeMock.Object); + } +} \ No newline at end of file diff --git a/SiemensIXBlazor/Components/Drawer/Drawer.razor.cs b/SiemensIXBlazor/Components/Drawer/Drawer.razor.cs index 88ca6b3..fffe223 100644 --- a/SiemensIXBlazor/Components/Drawer/Drawer.razor.cs +++ b/SiemensIXBlazor/Components/Drawer/Drawer.razor.cs @@ -50,13 +50,13 @@ protected async override Task OnAfterRenderAsync(bool firstRender) } [JSInvokable] - public async void Closed() + public async Task Closed() { await ClosedEvent.InvokeAsync(); } [JSInvokable] - public async void Opened() + public async Task Opened() { await OpenedEvent.InvokeAsync(); }