Add some service unit tests; add base for Fluxor tests

This commit is contained in:
Neil Brommer 2022-04-20 15:34:13 -07:00
parent 4e2b4d3806
commit 96553c3e2b
9 changed files with 289 additions and 23 deletions

View file

@ -0,0 +1,25 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Start.Client.Store.Features.ContainersList;
namespace Start_Tests.Client.Store {
[TestClass]
public class ContainerListTests : UnitTestWithFluxor {
public TestContext TestContext { get; set; }
// Only RootState is needed, so no need to get child objects
[TestMethod]
public void OnFetchContainersList() {
base.Store.Dispatch(new FetchContainerListAction());
Assert.IsTrue(base.State.Value.ContainerListState.IsLoadingContainersList);
Assert.AreEqual(0, this.State.Value.ContainerListState.Containers.Count);
}
[TestInitialize]
public void InitializeState() {
TestContext.WriteLine("Resetting Fluxor state");
base.ResetState();
}
}
}

View file

@ -0,0 +1,37 @@
using System;
using Blazored.LocalStorage;
using Fluxor;
using Microsoft.Extensions.DependencyInjection;
using Start.Client.Store.State;
using Start.Shared.Api;
using Start_Tests.Client.MockApis;
namespace Start_Tests.Client.Store {
public abstract class UnitTestWithFluxor {
protected IServiceProvider ServiceProvider { get; set; }
protected IStore Store { get; set; }
protected IState<RootState> State { get; set; }
// Add child states in the individual tests
protected Bunit.TestContext BunitTc { get; set; }
public UnitTestWithFluxor() {
this.ResetState();
}
public void ResetState() {
this.BunitTc = new Bunit.TestContext();
BunitTc.Services.AddBlazoredLocalStorage();
BunitTc.Services.AddFluxor(config => config.ScanAssemblies(typeof(RootState).Assembly));
BunitTc.Services.AddScoped<IBookmarksApi>(sp => new MockBookmarksApi());
BunitTc.Services.AddScoped<IBookmarkGroupsApi>(sp => new MockBookmarkGroupsApi());
BunitTc.Services
.AddScoped<IBookmarkContainersApi>(sp => new MockBookmarkContainersApi());
this.Store = this.BunitTc.Services.GetRequiredService<IStore>();
this.State = this.BunitTc.Services.GetRequiredService<IState<RootState>>();
this.Store.InitializeAsync().Wait();
}
}
}