mirror of
https://github.com/DrMboga/WotBlitzStatisticsBlazor.git
synced 2026-10-24 11:50:05 +02:00
feat: Blazor tests added
This commit is contained in:
1 parent
b4378b643b
commit
c60f622989
7 files changed
+309
-21
No files matched your search
@@ -0,0 +1,64 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bunit;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using WotBlitzStatisticsPro.Blazor.Messages;
|
||||
using WotBlitzStatisticsPro.Blazor.Model;
|
||||
using WotBlitzStatisticsPro.Blazor.Services;
|
||||
using TestContext = Bunit.TestContext;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Blazor.Tests.Services
|
||||
{
|
||||
public class LocalStorageServiceTests
|
||||
{
|
||||
private TestContext _testContext;
|
||||
private BunitJSInterop _jsRuntimeMock;
|
||||
protected MockNavigationManager _navigationManagerMock;
|
||||
|
||||
private LocalStorageService _localStorageService;
|
||||
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_navigationManagerMock = new MockNavigationManager();
|
||||
|
||||
_testContext = new TestContext();
|
||||
_jsRuntimeMock = _testContext.JSInterop;
|
||||
_jsRuntimeMock.Setup<string>("localStorage.getItem", UserSettings.UserSettingsLocalStorageKey).SetResult(string.Empty);
|
||||
var localStorageSetItemHandler = _jsRuntimeMock.SetupVoid("localStorage.setItem", _ => true);
|
||||
localStorageSetItemHandler.SetVoidResult();
|
||||
|
||||
_localStorageService = new LocalStorageService(_jsRuntimeMock.JSRuntime, _navigationManagerMock);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ShouldInvokeJsLocalStorageSetWithAppropriateDataOnChangeCurrentCulture()
|
||||
{
|
||||
string cultureName = "fake-CL";
|
||||
|
||||
await _localStorageService.Handle(new ChangeCurrentCultureMessage(cultureName), CancellationToken.None);
|
||||
|
||||
_jsRuntimeMock.Invocations.Count.Should().Be(2);
|
||||
var jsInvocations = _jsRuntimeMock.Invocations.ToArray();
|
||||
jsInvocations[0].Identifier.Should().Be("localStorage.getItem");
|
||||
jsInvocations[1].Identifier.Should().Be("localStorage.setItem");
|
||||
|
||||
var args = jsInvocations[1].Arguments;
|
||||
args.Should().NotBeNull();
|
||||
args.Count.Should().Be(2);
|
||||
args[0].Should().Be("user-settings");
|
||||
args[1].Should().Be($"{{\"Culture\":\"{cultureName}\"}}");
|
||||
|
||||
_navigationManagerMock.NavigationCount.Should().Be(1);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown() => _testContext?.Dispose();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using WotBlitzStatisticsPro.Blazor.Messages;
|
||||
using WotBlitzStatisticsPro.Blazor.Services;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Blazor.Tests.Services
|
||||
{
|
||||
public class NavigatorInterceptorTests
|
||||
{
|
||||
private NavigationMessagesInterceptor _service;
|
||||
private MockNavigationManager _navigationManager;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_navigationManager = new MockNavigationManager();
|
||||
_service = new NavigationMessagesInterceptor(_navigationManager);
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ShouldNavigateToAppropriateLinkWhenOpenPlayerInfoMessageArrived()
|
||||
{
|
||||
int accountId = 42;
|
||||
var message = new OpenPlayerInfoMessage(accountId, false);
|
||||
|
||||
await _service.Handle(message, CancellationToken.None);
|
||||
|
||||
_navigationManager.Uri.Should().Be($"{_navigationManager.BaseUri}player/{accountId}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user