Skip to content

Commit

Permalink
Add some missing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Leclerc committed Dec 22, 2023
1 parent 7e11448 commit 776a40f
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/AzureMapsControl.Components/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AzureMapsControl.Components
{
using System;
using System.Diagnostics.CodeAnalysis;

using AzureMapsControl.Components.Animations;
using AzureMapsControl.Components.Configuration;
Expand All @@ -21,6 +22,7 @@ public static class Extensions
/// <param name="services">Current list of services</param>
/// <param name="configure">Configuration</param>
/// <returns>Services</returns>
[ExcludeFromCodeCoverage]
public static IServiceCollection AddAzureMapsControl(this IServiceCollection services, Action<AzureMapsConfiguration> configure)
{
services
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AzureMapsControl.Components.Tests.Configuration
{
using AzureMapsControl.Components.Configuration;
using AzureMapsControl.Components.Tests.Json;

using Xunit;

Expand Down Expand Up @@ -51,4 +52,59 @@ public void Should_NotHaveAnAuthType()
Assert.False(configuration.Validate());
}
}

public class AzureMapsConfigurationJsonConverterTests : JsonConverterTests<AzureMapsConfiguration>
{
public AzureMapsConfigurationJsonConverterTests() : base(new AzureMapsConfigurationJsonConverter())
{
}

[Fact]
public void Should_Write_WithSubscriptionKey()
{
var configuration = new AzureMapsConfiguration {
SubscriptionKey = "subKey"
};
var expectedJson = "{"
+ "\"authType\":\"" + configuration.AuthType + "\","
+ "\"subscriptionKey\":\"" + configuration.SubscriptionKey + "\""
+ "}";

TestAndAssertWrite(configuration, expectedJson);
}

[Fact]
public void Should_Write_WithAad()
{
var configuration = new AzureMapsConfiguration {
AadAppId = "appId",
AadTenant = "tenant",
ClientId = "client"
};

var expectedJson = "{"
+ "\"authType\":\"" + configuration.AuthType + "\","
+ "\"aadAppId\":\"" + configuration.AadAppId + "\","
+ "\"aadTenant\":\"" + configuration.AadTenant + "\","
+ "\"clientId\":\"" + configuration.ClientId + "\""
+ "}";

TestAndAssertWrite(configuration, expectedJson);
}

[Fact]
public void Should_Write_WithAnonymous()
{
var configuration = new AzureMapsConfiguration {
ClientId = "client"
};

var expectedJson = "{"
+ "\"authType\":\"" + configuration.AuthType + "\","
+ "\"clientId\":\"" + configuration.ClientId + "\""
+ "}";

TestAndAssertWrite(configuration, expectedJson);
}
}
}
49 changes: 49 additions & 0 deletions tests/AzureMapsControl.Components.Tests/Runtime/MapJsRuntime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace AzureMapsControl.Components.Tests.Runtime
{
using System.Threading.Tasks;
using AzureMapsControl.Components.Runtime;

using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;

using Moq;

using Xunit;

public class MapJsRuntimeTests
{
private readonly Mock<IJSRuntime> _jsRuntimeMock = new Mock<IJSRuntime>();
private readonly Mock<ILogger<MapJsRuntime>> _loggerMock = new Mock<ILogger<MapJsRuntime>>();

private readonly MapJsRuntime _sut;

public MapJsRuntimeTests() => _sut = new(_jsRuntimeMock.Object, _loggerMock.Object);

[Fact]
public async Task Should_InvokeVoidAsync()
{
var identifier = "identifier";
var args = new object[] { 1, 2 };

await _sut.InvokeVoidAsync(identifier, args);

_jsRuntimeMock.Verify(runtime => runtime.InvokeAsync<Microsoft.JSInterop.Infrastructure.IJSVoidResult>(identifier, args), Times.Once);
_jsRuntimeMock.VerifyNoOtherCalls();
}

[Fact]
public async Task Should_InvokeAsync()
{
var identifier = "identifier";
var args = new object[] { 1, 2 };
var expected = "expected";
_jsRuntimeMock.Setup(runtime => runtime.InvokeAsync<string>(identifier, args)).ReturnsAsync(expected).Verifiable();

var result = await _sut.InvokeAsync<string>(identifier, args);

Assert.Equal(expected, result);
_jsRuntimeMock.Verify();
_jsRuntimeMock.VerifyNoOtherCalls();
}
}
}
75 changes: 75 additions & 0 deletions tests/AzureMapsControl.Components.Tests/Traffic/TrafficOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace AzureMapsControl.Components.Tests.Traffic
{
using AzureMapsControl.Components.Tests.Json;
using AzureMapsControl.Components.Traffic;

using Xunit;

public class TrafficOptionsJsonConverterTests : JsonConverterTests<TrafficOptions>
{
public TrafficOptionsJsonConverterTests() : base(new TrafficOptionsJsonConverter()) { }

[Fact]
public void Should_ReadTrafficFlow()
{
var json = "{"
+ "\"flow\":\"" + TrafficFlow.Relative.ToString() + "\""
+ "}";
var result = Read(json);
Assert.Equal(TrafficFlow.Relative, result.Flow);
Assert.Null(result.Incidents);
}

[Fact]
public void Should_ReadIncidents()
{
var json = "{"
+ "\"incidents\":true"
+ "}";
var result = Read(json);
Assert.True(result.Incidents);
}

[Fact]
public void Should_ReadTrafficOptions()
{
var json = "{"
+ "\"flow\":\"" + TrafficFlow.Relative.ToString() + "\","
+ "\"incidents\":true"
+ "}";
var result = Read(json);
Assert.Equal(TrafficFlow.Relative, result.Flow);
Assert.True(result.Incidents);
}

[Fact]
public async void Should_WriteTrafficOptions()

Check warning on line 46 in tests/AzureMapsControl.Components.Tests/Traffic/TrafficOptions.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 46 in tests/AzureMapsControl.Components.Tests/Traffic/TrafficOptions.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 46 in tests/AzureMapsControl.Components.Tests/Traffic/TrafficOptions.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 46 in tests/AzureMapsControl.Components.Tests/Traffic/TrafficOptions.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var trafficOptions = new TrafficOptions {
Flow = TrafficFlow.Relative
};

var expectedJson = "{"
+ "\"flow\":\"" + trafficOptions.Flow.ToString() + "\""
+ "}";

TestAndAssertWrite(trafficOptions, expectedJson);
}

[Fact]
public async void Should_WriteTrafficOptions_WithIncidents()

Check warning on line 60 in tests/AzureMapsControl.Components.Tests/Traffic/TrafficOptions.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 60 in tests/AzureMapsControl.Components.Tests/Traffic/TrafficOptions.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 60 in tests/AzureMapsControl.Components.Tests/Traffic/TrafficOptions.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 60 in tests/AzureMapsControl.Components.Tests/Traffic/TrafficOptions.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var trafficOptions = new TrafficOptions {
Flow = TrafficFlow.Relative,
Incidents = true
};

var expectedJson = "{"
+ "\"flow\":\"" + trafficOptions.Flow.ToString() + "\","
+ "\"incidents\":true"
+ "}";

TestAndAssertWrite(trafficOptions, expectedJson);
}
}
}

0 comments on commit 776a40f

Please sign in to comment.