Skip to content

Commit

Permalink
merge: master to test/gokce-unitTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Gokce Ozkan committed May 29, 2024
2 parents 8772ac4 + 954c1b3 commit 0699566
Show file tree
Hide file tree
Showing 58 changed files with 1,990 additions and 93 deletions.
103 changes: 103 additions & 0 deletions SiemensIXBlazor.Tests/AGGrid/AGGridTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Diagnostics;
using Bunit;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;
using Moq;
using SiemensIXBlazor.Components.AGGrid;

namespace SiemensIXBlazor.Tests
{
public class AGGridTests : TestContextBase
{
[Fact]
public void ComponentRendersWithoutCrashing()
{
// Arrange
var cut = RenderComponent<AGGrid>();

// Assert
cut.MarkupMatches("<div id=''></div>");
}

[Fact]
public void OnCellClickedPropertyIsSetCorrectly()
{
// Arrange
var eventTriggered = false;
var cut = RenderComponent<AGGrid>(parameters => parameters.Add(p => p.OnCellClicked, EventCallback.Factory.Create(this, () => eventTriggered = true)));

// Act
cut.Instance.OnCellClicked.InvokeAsync(true);

//Assert
Assert.True(eventTriggered);
}

[Fact]
public void IdPropertyIsSetCorrectly()
{
// Arrange
var cut = RenderComponent<AGGrid>(parameters => parameters.Add(p => p.Id, "testId"));

// Assert
Assert.Equal("testId", cut.Instance.Id);
}

[Fact]
public async Task CreateGrid_ReturnsNull_WhenIdIsEmpty()
{
// Arrange
var grid = new AGGrid();

// Act
var result = await grid.CreateGrid(new GridOptions());

// Assert
Assert.Null(result);
}

[Fact]
public async Task CreateGrid_ReturnsJSObjectReference_WhenIdIsNotEmpty()
{
// Arrange
var gridOptions = new GridOptions();
Mock<IJSRuntime> jsRuntimeMock = new();
Mock<IJSObjectReference> jsObjectReferenceMock = new();

// Mock of module import for JSRuntime
jsRuntimeMock.Setup(x => x.InvokeAsync<IJSObjectReference>("agGridInterop.createGrid", It.IsAny<object[]>()))
.Returns(new ValueTask<IJSObjectReference>(jsObjectReferenceMock.Object));
Services.AddSingleton(jsRuntimeMock.Object);

var cut = RenderComponent<AGGrid>(parameters => parameters.Add(p => p.Id, "testId"));

// Act
var result = await cut.Instance.CreateGrid(gridOptions);

// Assert
Assert.NotNull(result);
jsRuntimeMock.Verify(x => x.InvokeAsync<object>("agGridInterop.createGrid", It.IsAny<object[]>()), Times.Once);
}

[Fact]
public async Task GetSelectedRows_ReturnsObject()
{
// Arrange
var jsRuntimeMock = new Mock<IJSRuntime>();
Services.AddSingleton(jsRuntimeMock.Object);
var cut = RenderComponent<AGGrid>(parameters => parameters.Add(p => p.Id, "testId"));
var jsObjectReferenceMock = new Mock<IJSObjectReference>();
jsRuntimeMock.Setup(x => x.InvokeAsync<object>("agGridInterop.getSelectedRows", It.IsAny<object[]>()))
.ReturnsAsync(new object());


// Act
var result = await cut.Instance.GetSelectedRows(jsObjectReferenceMock.Object);

// Assert
Assert.NotNull(result);
jsRuntimeMock.Verify(x => x.InvokeAsync<object>("agGridInterop.getSelectedRows", It.IsAny<object[]>()), Times.Once);
}
}
}
20 changes: 1 addition & 19 deletions SiemensIXBlazor.Tests/About/AboutMenuItemTest.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
using Bunit;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;
using Moq;
using SiemensIXBlazor.Components.About;

namespace SiemensIXBlazor.Tests.About
{
public class AboutMenuItemTest: TestContext
public class AboutMenuItemTest: TestContextBase
{
private readonly Mock<IJSRuntime> _jsRuntimeMock;
private readonly Mock<IJSObjectReference> _jsObjectReferenceMock;

public AboutMenuItemTest()
{
_jsRuntimeMock = new Mock<IJSRuntime>();
_jsObjectReferenceMock = new Mock<IJSObjectReference>();

// 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);
}


[Fact]
public void ComponentRendersWithoutCrashing()
{
Expand Down
20 changes: 1 addition & 19 deletions SiemensIXBlazor.Tests/About/AboutMenuNewsTest.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
using Bunit;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;
using Moq;
using SiemensIXBlazor.Components.About;

namespace SiemensIXBlazor.Tests.About
{
public class AboutMenuNewsTest: TestContext
public class AboutMenuNewsTest: TestContextBase
{
private readonly Mock<IJSRuntime> _jsRuntimeMock;
private readonly Mock<IJSObjectReference> _jsObjectReferenceMock;

public AboutMenuNewsTest()
{
_jsRuntimeMock = new Mock<IJSRuntime>();
_jsObjectReferenceMock = new Mock<IJSObjectReference>();

// 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);
}


[Fact]
public void ComponentRendersWithoutCrashing()
{
Expand Down
20 changes: 1 addition & 19 deletions SiemensIXBlazor.Tests/About/AboutMenuTest.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
using Bunit;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.JSInterop;
using Moq;
using SiemensIXBlazor.Components.About;

namespace SiemensIXBlazor.Tests.About
{
public class AboutMenuTest: TestContext
public class AboutMenuTest: TestContextBase
{
private readonly Mock<IJSRuntime> _jsRuntimeMock;
private readonly Mock<IJSObjectReference> _jsObjectReferenceMock;

public AboutMenuTest()
{
_jsRuntimeMock = new Mock<IJSRuntime>();
_jsObjectReferenceMock = new Mock<IJSObjectReference>();

// 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);
}


[Fact]
public void ComponentRendersWithoutCrashing()
{
Expand Down
69 changes: 69 additions & 0 deletions SiemensIXBlazor.Tests/ActionCardTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Bunit;
using SiemensIXBlazor.Components;
using SiemensIXBlazor.Enums.PushCard;

namespace SiemensIXBlazor.Tests
{
public class ActionCardTests: TestContextBase
{
[Fact]
public void ComponentRendersWithoutCrashing()
{
// Arrange
var cut = RenderComponent<ActionCard>();

// Assert
cut.MarkupMatches("<ix-action-card variant='insight'></ix-action-card>");
}

[Fact]
public void IconPropertyIsSetCorrectly()
{
// Arrange
var cut = RenderComponent<ActionCard>(parameters => parameters.Add(p => p.Icon, "testIcon"));

// Assert
Assert.Equal("testIcon", cut.Instance.Icon);
}

[Fact]
public void HeadingPropertyIsSetCorrectly()
{
// Arrange
var cut = RenderComponent<ActionCard>(parameters => parameters.Add(p => p.Heading, "testHeading"));

// Assert
Assert.Equal("testHeading", cut.Instance.Heading);
}

[Fact]
public void SubHeadingPropertyIsSetCorrectly()
{
// Arrange
var cut = RenderComponent<ActionCard>(parameters => parameters.Add(p => p.SubHeading, "testSubHeading"));

// Assert
Assert.Equal("testSubHeading", cut.Instance.SubHeading);
}

[Fact]
public void SelectedPropertyIsSetCorrectly()
{
// Arrange
var cut = RenderComponent<ActionCard>(parameters => parameters.Add(p => p.Selected, true));

// Assert
Assert.True(cut.Instance.Selected);
}

[Fact]
public void VariantPropertyIsSetCorrectly()
{
// Arrange
var cut = RenderComponent<ActionCard>(parameters => parameters.Add(p => p.Variant, PushCardVariant.insight));

// Assert
Assert.Equal(PushCardVariant.insight, cut.Instance.Variant);
}
}
}
37 changes: 37 additions & 0 deletions SiemensIXBlazor.Tests/ApplicationHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Bunit;
using SiemensIXBlazor.Components;

namespace SiemensIXBlazor.Tests
{
public class ApplicationHeaderTests : TestContextBase
{
[Fact]
public void ApplicationHeaderRendersWithoutCrashing()
{
// Arrange
var cut = RenderComponent<ApplicationHeader>(parameters => {
parameters.Add(p => p.Name, "testName");
});

// Assert
cut.MarkupMatches("<ix-application-header name='testName'></ix-application-header>");
}

[Fact]
public void ApplicationHeaderRendersChildContent()
{
// Arrange
var expectedContent = "Expected content";

// Act
var cut = RenderComponent<ApplicationHeader>(parameters => parameters
.Add(p => p.ChildContent, builder =>
{
builder.AddContent(0, expectedContent);
}));

// Assert
Assert.Contains(expectedContent, cut.Markup);
}
}
}
40 changes: 40 additions & 0 deletions SiemensIXBlazor.Tests/ApplicationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Bunit;
using SiemensIXBlazor.Components;
using SiemensIXBlazor.Objects.Application;

namespace SiemensIXBlazor.Tests
{
public class ApplicationTests : TestContextBase
{
[Fact]
public void ApplicationRendersWithoutCrashing()
{
// Arrange
var cut = RenderComponent<Application>(parameters => {
parameters.Add(p => p.Id, "testId");
parameters.Add(p => p.Breakpoints, ["sm", "md", "lg"]);
parameters.Add(p => p.ForceBreakpoint, Enums.ForceBreakpoint.lg);
parameters.Add(p => p.Theme, "testTheme");
parameters.Add(p => p.ThemeSystemAppearance, true);
});

// Assert
cut.MarkupMatches("<ix-application id='testId' breakpoints=\"['sm','md','lg']\" force-breakpoint='lg' theme='testTheme' theme-system-appearance=''></ix-application>");
}

[Fact]
public void AppSwitchConfig_SetsValueAndCallsInitialParameter()
{
// Arrange
var cut = RenderComponent<Application>();
var config = new AppSwitchConfig();

// Act
cut.Instance.AppSwitchConfig = config;

// Assert
Assert.Equal(config, cut.Instance.AppSwitchConfig);
}

}
}
21 changes: 21 additions & 0 deletions SiemensIXBlazor.Tests/AvatarTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Bunit;
using SiemensIXBlazor.Components.Avatar;

namespace SiemensIXBlazor.Tests
{
public class AvatarTests : TestContextBase
{
[Fact]
public void AvatarRendersWithoutCrashing()
{
// Arrange
var cut = RenderComponent<Avatar>(parameters => {
parameters.Add(p => p.Image, "testImage");
parameters.Add(p => p.Initials, "testInitials");
});

// Assert
cut.MarkupMatches("<ix-avatar image='testImage' initials='testInitials'></ix-avatar>");
}
}
}
Loading

0 comments on commit 0699566

Please sign in to comment.