Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drawer test added with TestContextBase class #55

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions SiemensIXBlazor.Tests/DrawerTests.cs
Original file line number Diff line number Diff line change
@@ -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<Drawer>(
("Id", "testId"),
("CloseOnClickOutside", true),
("FullHeight", false),
("MaxWidth", 28),
("MinWidth", 16),
("Show", true),
("Width", 16)
);

// Assert
cut.MarkupMatches("<ix-drawer minwidth=\"16\" id=\"testId\" show=\"\" close-on-click-outside=\"\" max-width=\"28\" min-width=\"16\" width=\"16\"></ix-drawer>");
}

[Fact]
public async Task ClosedEventWorks()
{
// Arrange
var closed = false;
var cut = RenderComponent<Drawer>(
("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<Drawer>(
("Id", "drawer"),
("OpenedEvent", EventCallback.Factory.Create(this, () => opened = true))
);

// Act
await cut.Instance.Opened();

// Assert
Assert.True(opened);
}
}
}
29 changes: 29 additions & 0 deletions SiemensIXBlazor.Tests/TestContextBase.cs
Original file line number Diff line number Diff line change
@@ -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<IJSRuntime> jsRuntimeMock = new();
Mock<IJSObjectReference> jsObjectReferenceMock = new();

// Mock of module import for JSRuntime
jsRuntimeMock.Setup(x => x.InvokeAsync<IJSObjectReference>("import", It.IsAny<object[]>()))
.Returns(new ValueTask<IJSObjectReference>(jsObjectReferenceMock.Object));
Services.AddSingleton(jsRuntimeMock.Object);
}
}
4 changes: 2 additions & 2 deletions SiemensIXBlazor/Components/Drawer/Drawer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Loading