This commit is contained in:
6d486f49
2026-08-14 15:56:17 -04:00
parent af64b9e95c
commit 09a8adfc4a
827 changed files with 65045 additions and 71849 deletions
+95
View File
@@ -0,0 +1,95 @@
namespace Tests;
using Microsoft.JSInterop;
using Web.Services;
[TestFixture]
public class NetworkStatusTests
{
private class MockJSRuntime : IJSRuntime
{
public bool ReturnOnlineValue { get; set; } = true;
public bool InitializeCalled { get; private set; }
public bool DisposeCalled { get; private set; }
public ValueTask<TValue> InvokeAsync<TValue>(string identifier, object?[]? args)
{
if (identifier == "networkStatus.initialize")
{
InitializeCalled = true;
return new ValueTask<TValue>((TValue)(object)ReturnOnlineValue);
}
return new ValueTask<TValue>(default(TValue)!);
}
public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object?[]? args)
{
return InvokeAsync<TValue>(identifier, args);
}
}
[Test]
public void NetworkStatusService_InitialState_IsOnlineByDefault()
{
var jsRuntime = new MockJSRuntime();
using var service = new NetworkStatusService(jsRuntime);
Assert.That(service.IsOnline, Is.True);
}
[Test]
public async Task NetworkStatusService_InitializeAsync_SetsStatusFromBrowser()
{
var jsRuntime = new MockJSRuntime { ReturnOnlineValue = false };
var service = new NetworkStatusService(jsRuntime);
var eventFired = false;
bool? reportedStatus = null;
service.StatusChanged += status =>
{
eventFired = true;
reportedStatus = status;
};
await service.InitializeAsync();
Assert.That(jsRuntime.InitializeCalled, Is.True);
Assert.That(service.IsOnline, Is.False);
Assert.That(eventFired, Is.True);
Assert.That(reportedStatus, Is.False);
await service.DisposeAsync();
}
[Test]
public void NetworkStatusService_SetOnlineStatus_FiresEventWhenChanged()
{
var jsRuntime = new MockJSRuntime();
using var service = new NetworkStatusService(jsRuntime);
var eventCount = 0;
bool? currentStatus = null;
service.StatusChanged += status =>
{
eventCount++;
currentStatus = status;
};
// Transition to offline
service.SetOnlineStatus(false);
Assert.That(service.IsOnline, Is.False);
Assert.That(eventCount, Is.EqualTo(1));
Assert.That(currentStatus, Is.False);
// Setting same status again should not re-trigger event
service.SetOnlineStatus(false);
Assert.That(eventCount, Is.EqualTo(1));
// Transition back to online
service.SetOnlineStatus(true);
Assert.That(service.IsOnline, Is.True);
Assert.That(eventCount, Is.EqualTo(2));
Assert.That(currentStatus, Is.True);
}
}