-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevExpressComponentTest.cs
123 lines (104 loc) · 5.15 KB
/
DevExpressComponentTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bunit;
using Bunit.Extensions.WaitForHelpers;
using DevExpress.Blazor.bUnit.Internal;
using DevExpress.Blazor.Internal;
using DxTestProject.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.JSInterop;
using Bunit.Extensions.WaitForHelpers;
namespace DevExpressBunit.Test
{
public static class BUnitTestContextExtensions {
public static void AddDevExpressBlazorTesting(this TestContext testContext) {
testContext.Services.TryAddScoped<IEnvironmentInfoFactory, MockEnvironmentInfoFactory>();
testContext.Services.TryAddScoped<IEnvironmentInfo, MockEnvironmentInfo>();
testContext.Services.TryAddScoped<ISvgImagesLoader, FakeSvgImagesLoader>();
testContext.Services.TryAddScoped<IGlobalOptionsService, GlobalOptionsService>();
testContext.Services.AddSingleton<WeatherForecastService>();
testContext.Services.AddOptions();
testContext.Services.AddLogging();
testContext.Services.TryAddComponentRequiredServices();
testContext.JSInterop.ConfigureJSInterop();
}
public static async Task WaitForAssertionAsync(this IRenderedFragmentBase renderedFragment, Action assertion, TimeSpan? timeout = null) {
using var waiter = new WaitForAssertionHelper(renderedFragment, assertion, timeout);
await waiter.WaitTask;
}
static void ConfigureJSInterop(this BunitJSInterop interop) {
interop.Mode = JSRuntimeMode.Loose;
var rootModule = interop.SetupModule("./_content/DevExpress.Blazor/dx-blazor.js");
rootModule.Mode = JSRuntimeMode.Strict;
rootModule.Setup<DeviceInfo>("getDeviceInfo", _ => true).SetResult(new DeviceInfo(false));
}
sealed class FakeSvgImagesLoader : ISvgImagesLoader {
static readonly RenderFragment _emptyRenderFragment = b => { };
RenderFragment ISvgImagesLoader.GetLoaderRenderFragment(string spriteUrl, out bool useShortName) {
useShortName = false;
return _emptyRenderFragment;
}
}
public sealed class MockEnvironmentInfoFactory : IEnvironmentInfoFactory {
readonly IEnvironmentInfo _cached;
public MockEnvironmentInfoFactory(bool isWasm = false) {
_cached = new MockEnvironmentInfo(isWasm);
}
public IEnvironmentInfo CreateEnvironmentInfo() => _cached;
}
public class MockIJSRuntime : IJSRuntime, IEnvironmentInfoFactory {
public MockIJSRuntime() {
EnvironmentInfo = new MockEnvironmentInfo();
}
readonly List<IDisposable> _preventedFromGC = new List<IDisposable>();
public IEnvironmentInfo EnvironmentInfo { get; }
public List<string> InvokeHistory { get; } = new List<string>();
public async ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args) {
InvokeHistory.Add(identifier);
_preventedFromGC.AddRange(args.OfType<IDisposable>().Except(_preventedFromGC));
if (typeof(TValue) == typeof(ApiScheme))
return (TValue)(object)await EnvironmentInfo.ApiScheme;
if (typeof(TValue) == typeof(DeviceInfo))
return (TValue)(object)await EnvironmentInfo.DeviceInfo;
return default;
}
public ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object[] args) {
return InvokeAsync<TValue>(identifier, args);
}
public IEnvironmentInfo CreateEnvironmentInfo() {
return EnvironmentInfo;
}
}
public class MockEnvironmentInfo : IEnvironmentInfo {
public bool IsWasm { get; }
public CultureInfo CurrentCulture { get; }
public DateTime Now { get; }
public static readonly DateTime DateTimeNow = DateTime.Now.Date;
public MockEnvironmentInfo(bool isWasm = false, CultureInfo currentCulture = default, DateTime? dateTime = default) {
IsWasm = isWasm;
CurrentCulture = currentCulture ?? CultureInfo.CurrentCulture;
Now = dateTime ?? DateTimeNow;
}
public DateTime GetDateTimeNow() {
return Now;
}
public DateTime GetDateTimeUtcNow() {
return DateTime.UtcNow;
}
Task<ApiScheme> IEnvironmentInfo.ApiScheme => Task.FromResult(new ApiScheme(true));
public virtual Task<DeviceInfo> DeviceInfo => Task.FromResult(new DeviceInfo(false) {
ClientMinutesOffset = DateTimeOffset.Now.Offset.TotalMinutes,
HasAccurateInput = true
});
public Task InitializeRuntime() {
return Task.CompletedTask;
}
}
}
}