Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
idotta committed Jun 3, 2024
2 parents 5ab318e + a23a1ba commit abd8f81
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/SimpleRouter.Avalonia/ViewLocatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public virtual bool Match(object? data)
/// </summary>
/// <param name="route">The route used to resolve the control.</param>
/// <returns>The resolved control.</returns>
protected abstract Control? ResolveControl(IRoute? route);
public abstract Control? ResolveControl(IRoute? route);

/// <summary>
/// Tries to deduce the control based on the provided data by using reflection.
Expand Down
83 changes: 83 additions & 0 deletions src/SimpleRouter.Tests/AvaloniaRouteViewHostTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Avalonia.Controls;
using Moq;
using SimpleRouter.Avalonia;

namespace SimpleRouter.Tests;

public class AvaloniaRouteViewHostTests
{
[Fact]
public void NavigateToRoute_RouteIsNull_SetsDefaultContent()
{
// Arrange
var routeViewHost = new RouteViewHost();
var defaultContent = new object();
routeViewHost.DefaultContent = defaultContent;
var router = new Mock<IRouter>();
router.Setup(r => r.Current).Returns<IRoute?>(null);

Check warning on line 17 in src/SimpleRouter.Tests/AvaloniaRouteViewHostTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 17 in src/SimpleRouter.Tests/AvaloniaRouteViewHostTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

// Act
routeViewHost.Router = router.Object;

// Assert
Assert.Equal(defaultContent, routeViewHost.Content);
}

[Fact]
public void NavigateToRoute_ViewLocatorIsNull_SetsRouteAsContent()
{
// Arrange
var routeViewHost = new RouteViewHost();
var route = new Mock<IRoute>().Object;
var router = new Mock<IRouter>();
router.Setup(r => r.Current).Returns(route);

// Act
routeViewHost.Router = router.Object;

// Assert
Assert.Equal(route, routeViewHost.Content);
}

[Fact]
public void NavigateToRoute_ViewLocatorBuildReturnsNull_SetsDefaultContent()
{
// Arrange
var routeViewHost = new RouteViewHost();
var route = new Mock<IRoute>().Object;
var viewLocator = new Mock<ViewLocatorBase>();
var defaultContent = new object();
var router = new Mock<IRouter>();
router.Setup(r => r.Current).Returns(route);
routeViewHost.ViewLocator = viewLocator.Object;
viewLocator.Setup(v => v.Build(route)).Returns<Control?>(null);

Check warning on line 53 in src/SimpleRouter.Tests/AvaloniaRouteViewHostTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 53 in src/SimpleRouter.Tests/AvaloniaRouteViewHostTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
routeViewHost.DefaultContent = defaultContent;

// Act
routeViewHost.Router = router.Object;

// Assert
Assert.Equal(defaultContent, routeViewHost.Content);
}

[Fact]
public void NavigateToRoute_ViewLocatorBuildReturnsView_SetsViewAsContent()
{
// Arrange
var routeViewHost = new RouteViewHost();
var route = new Mock<IRoute>().Object;
var view = new Mock<Control>().Object;
var viewLocator = new Mock<ViewLocatorBase>();
routeViewHost.ViewLocator = viewLocator.Object;
viewLocator.Setup(v => v.Build(route)).Returns(view);
var router = new Mock<IRouter>();
router.Setup(r => r.Current).Returns(route);

// Act
routeViewHost.Router = router.Object;

// Assert
Assert.Equal(view, routeViewHost.Content);
Assert.Equal(route, view.DataContext);
}
}
70 changes: 70 additions & 0 deletions src/SimpleRouter.Tests/AvaloniaViewLocatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Avalonia.Controls;
using Moq;
using SimpleRouter.Avalonia;

namespace SimpleRouter.Tests;

public class AvaloniaViewLocatorTests
{
[Fact]
public void Build_WithNullParameter_ReturnsDefaultContent()
{
// Arrange
var viewLocator = new Mock<ViewLocatorBase>() { CallBase = true };
var defaultContent = new Control();
viewLocator.Object.DefaultContent = defaultContent;

// Act
var result = viewLocator.Object.Build(null);

// Assert
Assert.Equal(defaultContent, result);
}

[Fact]
public void Build_WithNonNullParameter_ResolvesControl()
{
// Arrange
var viewLocator = new Mock<ViewLocatorBase>() { CallBase = true };
var route = new Mock<IRoute>();
var resolvedControl = new Control();
viewLocator.Object.DefaultContent = new Control();
viewLocator.Setup(x => x.ResolveControl(route.Object)).Returns(resolvedControl);

// Act
var result = viewLocator.Object.Build(route.Object);

// Assert
Assert.Equal(resolvedControl, result);
}

[Fact]
public void Match_WithDataOfTypeIRoute_ReturnsTrue()
{
// Arrange
var viewLocator = new Mock<ViewLocatorBase>() { CallBase = true }.Object;
var data = new Mock<IRoute>().Object;

// Act
var result = viewLocator.Match(data);

// Assert
Assert.True(result);
}

[Fact]
public void Match_WithDataNotOfTypeIRoute_ReturnsFalse()
{
// Arrange
var viewLocator = new Mock<ViewLocatorBase>() { CallBase = true }.Object;
var data = new object();

// Act
var result = viewLocator.Match(data);

// Assert
Assert.False(result);
}


}
1 change: 1 addition & 0 deletions src/SimpleRouter.Tests/SimpleRouter.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SimpleRouter.Avalonia\SimpleRouter.Avalonia.csproj" />
<ProjectReference Include="..\SimpleRouter\SimpleRouter.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public override bool Match(object? data)
return data is ViewModelBase;
}

protected override Control? ResolveControl(IRoute? route)
public override Control? ResolveControl(IRoute? route)
{
ArgumentNullException.ThrowIfNull(route);
return route switch
Expand All @@ -39,7 +39,7 @@ public override bool Match(object? data)
return data is ViewModelBase;
}

protected override Control? ResolveControl(IRoute? route)
public override Control? ResolveControl(IRoute? route)
{
ArgumentNullException.ThrowIfNull(route);
return route switch
Expand Down

0 comments on commit abd8f81

Please sign in to comment.