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,29 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Refit;
using Start.Shared;
using Start.Shared.Api;
namespace Start_Tests.Client.MockApis {
public class MockBookmarkContainersApi : IBookmarkContainersApi {
public Task<ApiResponse<IEnumerable<BookmarkContainerDto>>> GetAllBookmarkContainers() {
throw new NotImplementedException();
}
public Task<ApiResponse<BookmarkContainerDto>> GetBookmarkContainer(
int bookmarkContainerId) {
throw new NotImplementedException();
}
public Task<ApiResponse<BookmarkContainerDto>> CreateBookmarkContainer(string title,
int sortOrder) {
throw new NotImplementedException();
}
public Task<HttpResponseMessage> DeleteBookmarkContainer(int bookmarkContainerId) {
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Refit;
using Start.Shared;
using Start.Shared.Api;
namespace Start_Tests.Client.MockApis {
public class MockBookmarkGroupsApi : IBookmarkGroupsApi {
public Task<ApiResponse<BookmarkGroupDto>> GetBookmarkGroup(int bookmarkGroupId) {
throw new NotImplementedException();
}
public Task<ApiResponse<BookmarkGroupDto>> CreateBookmarkGroup(string title, string color,
int sortOrder, int bookmarkContainerId) {
throw new NotImplementedException();
}
public Task<HttpResponseMessage> DeleteBookmarkGroup(int bookmarkGroupId) {
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Refit;
using Start.Shared;
using Start.Shared.Api;
namespace Start_Tests.Client.MockApis {
public class MockBookmarksApi : IBookmarksApi {
public Task<ApiResponse<BookmarkDto>> GetBookmark(int bookmarkId) {
throw new NotImplementedException();
}
public Task<ApiResponse<BookmarkDto>> CreateBookmark(string title, string url,
string notes, int sortOrder, int bookmarkGroupId) {
throw new NotImplementedException();
}
public Task<HttpResponseMessage> DeleteBookmark(int bookmarkId) {
throw new NotImplementedException();
}
}
}

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();
}
}
}