Skip to content

Commit 776a40f

Browse files
author
Arnaud Leclerc
committed
Add some missing unit tests
1 parent 7e11448 commit 776a40f

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

src/AzureMapsControl.Components/Extensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AzureMapsControl.Components
22
{
33
using System;
4+
using System.Diagnostics.CodeAnalysis;
45

56
using AzureMapsControl.Components.Animations;
67
using AzureMapsControl.Components.Configuration;
@@ -21,6 +22,7 @@ public static class Extensions
2122
/// <param name="services">Current list of services</param>
2223
/// <param name="configure">Configuration</param>
2324
/// <returns>Services</returns>
25+
[ExcludeFromCodeCoverage]
2426
public static IServiceCollection AddAzureMapsControl(this IServiceCollection services, Action<AzureMapsConfiguration> configure)
2527
{
2628
services

tests/AzureMapsControl.Components.Tests/Configuration/AzureMapsConfiguration.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace AzureMapsControl.Components.Tests.Configuration
22
{
33
using AzureMapsControl.Components.Configuration;
4+
using AzureMapsControl.Components.Tests.Json;
45

56
using Xunit;
67

@@ -51,4 +52,59 @@ public void Should_NotHaveAnAuthType()
5152
Assert.False(configuration.Validate());
5253
}
5354
}
55+
56+
public class AzureMapsConfigurationJsonConverterTests : JsonConverterTests<AzureMapsConfiguration>
57+
{
58+
public AzureMapsConfigurationJsonConverterTests() : base(new AzureMapsConfigurationJsonConverter())
59+
{
60+
}
61+
62+
[Fact]
63+
public void Should_Write_WithSubscriptionKey()
64+
{
65+
var configuration = new AzureMapsConfiguration {
66+
SubscriptionKey = "subKey"
67+
};
68+
var expectedJson = "{"
69+
+ "\"authType\":\"" + configuration.AuthType + "\","
70+
+ "\"subscriptionKey\":\"" + configuration.SubscriptionKey + "\""
71+
+ "}";
72+
73+
TestAndAssertWrite(configuration, expectedJson);
74+
}
75+
76+
[Fact]
77+
public void Should_Write_WithAad()
78+
{
79+
var configuration = new AzureMapsConfiguration {
80+
AadAppId = "appId",
81+
AadTenant = "tenant",
82+
ClientId = "client"
83+
};
84+
85+
var expectedJson = "{"
86+
+ "\"authType\":\"" + configuration.AuthType + "\","
87+
+ "\"aadAppId\":\"" + configuration.AadAppId + "\","
88+
+ "\"aadTenant\":\"" + configuration.AadTenant + "\","
89+
+ "\"clientId\":\"" + configuration.ClientId + "\""
90+
+ "}";
91+
92+
TestAndAssertWrite(configuration, expectedJson);
93+
}
94+
95+
[Fact]
96+
public void Should_Write_WithAnonymous()
97+
{
98+
var configuration = new AzureMapsConfiguration {
99+
ClientId = "client"
100+
};
101+
102+
var expectedJson = "{"
103+
+ "\"authType\":\"" + configuration.AuthType + "\","
104+
+ "\"clientId\":\"" + configuration.ClientId + "\""
105+
+ "}";
106+
107+
TestAndAssertWrite(configuration, expectedJson);
108+
}
109+
}
54110
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace AzureMapsControl.Components.Tests.Runtime
2+
{
3+
using System.Threading.Tasks;
4+
using AzureMapsControl.Components.Runtime;
5+
6+
using Microsoft.Extensions.Logging;
7+
using Microsoft.JSInterop;
8+
9+
using Moq;
10+
11+
using Xunit;
12+
13+
public class MapJsRuntimeTests
14+
{
15+
private readonly Mock<IJSRuntime> _jsRuntimeMock = new Mock<IJSRuntime>();
16+
private readonly Mock<ILogger<MapJsRuntime>> _loggerMock = new Mock<ILogger<MapJsRuntime>>();
17+
18+
private readonly MapJsRuntime _sut;
19+
20+
public MapJsRuntimeTests() => _sut = new(_jsRuntimeMock.Object, _loggerMock.Object);
21+
22+
[Fact]
23+
public async Task Should_InvokeVoidAsync()
24+
{
25+
var identifier = "identifier";
26+
var args = new object[] { 1, 2 };
27+
28+
await _sut.InvokeVoidAsync(identifier, args);
29+
30+
_jsRuntimeMock.Verify(runtime => runtime.InvokeAsync<Microsoft.JSInterop.Infrastructure.IJSVoidResult>(identifier, args), Times.Once);
31+
_jsRuntimeMock.VerifyNoOtherCalls();
32+
}
33+
34+
[Fact]
35+
public async Task Should_InvokeAsync()
36+
{
37+
var identifier = "identifier";
38+
var args = new object[] { 1, 2 };
39+
var expected = "expected";
40+
_jsRuntimeMock.Setup(runtime => runtime.InvokeAsync<string>(identifier, args)).ReturnsAsync(expected).Verifiable();
41+
42+
var result = await _sut.InvokeAsync<string>(identifier, args);
43+
44+
Assert.Equal(expected, result);
45+
_jsRuntimeMock.Verify();
46+
_jsRuntimeMock.VerifyNoOtherCalls();
47+
}
48+
}
49+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace AzureMapsControl.Components.Tests.Traffic
2+
{
3+
using AzureMapsControl.Components.Tests.Json;
4+
using AzureMapsControl.Components.Traffic;
5+
6+
using Xunit;
7+
8+
public class TrafficOptionsJsonConverterTests : JsonConverterTests<TrafficOptions>
9+
{
10+
public TrafficOptionsJsonConverterTests() : base(new TrafficOptionsJsonConverter()) { }
11+
12+
[Fact]
13+
public void Should_ReadTrafficFlow()
14+
{
15+
var json = "{"
16+
+ "\"flow\":\"" + TrafficFlow.Relative.ToString() + "\""
17+
+ "}";
18+
var result = Read(json);
19+
Assert.Equal(TrafficFlow.Relative, result.Flow);
20+
Assert.Null(result.Incidents);
21+
}
22+
23+
[Fact]
24+
public void Should_ReadIncidents()
25+
{
26+
var json = "{"
27+
+ "\"incidents\":true"
28+
+ "}";
29+
var result = Read(json);
30+
Assert.True(result.Incidents);
31+
}
32+
33+
[Fact]
34+
public void Should_ReadTrafficOptions()
35+
{
36+
var json = "{"
37+
+ "\"flow\":\"" + TrafficFlow.Relative.ToString() + "\","
38+
+ "\"incidents\":true"
39+
+ "}";
40+
var result = Read(json);
41+
Assert.Equal(TrafficFlow.Relative, result.Flow);
42+
Assert.True(result.Incidents);
43+
}
44+
45+
[Fact]
46+
public async void Should_WriteTrafficOptions()
47+
{
48+
var trafficOptions = new TrafficOptions {
49+
Flow = TrafficFlow.Relative
50+
};
51+
52+
var expectedJson = "{"
53+
+ "\"flow\":\"" + trafficOptions.Flow.ToString() + "\""
54+
+ "}";
55+
56+
TestAndAssertWrite(trafficOptions, expectedJson);
57+
}
58+
59+
[Fact]
60+
public async void Should_WriteTrafficOptions_WithIncidents()
61+
{
62+
var trafficOptions = new TrafficOptions {
63+
Flow = TrafficFlow.Relative,
64+
Incidents = true
65+
};
66+
67+
var expectedJson = "{"
68+
+ "\"flow\":\"" + trafficOptions.Flow.ToString() + "\","
69+
+ "\"incidents\":true"
70+
+ "}";
71+
72+
TestAndAssertWrite(trafficOptions, expectedJson);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)