Skip to content

Commit

Permalink
Drawer test added with TestContextBase class
Browse files Browse the repository at this point in the history
  • Loading branch information
emregokrem committed May 16, 2024
1 parent 99e3207 commit b33d229
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
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

0 comments on commit b33d229

Please sign in to comment.