Skip to content

Commit 1585415

Browse files
fix tests
1 parent daaf65e commit 1585415

File tree

5 files changed

+44
-23
lines changed

5 files changed

+44
-23
lines changed

Client.Tests/BaseTest.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@ namespace ICTAce.FileHub.Client.Tests;
44

55
public abstract class BaseTest : IDisposable
66
{
7+
private const string TestBaseUrl = "https://localhost:5001/";
78
private bool _disposed;
89

9-
protected BunitContext TestContext { get; private set; }
10-
protected Alias TestAlias { get; private set; }
11-
protected Site TestSite { get; private set; }
12-
protected Page TestPage { get; private set; }
10+
protected BunitContext TestContext { get; private set; } = null!;
11+
protected Alias TestAlias { get; private set; } = null!;
12+
protected Site TestSite { get; private set; } = null!;
13+
protected Page TestPage { get; private set; } = null!;
1314

1415
protected BaseTest()
16+
{
17+
InitializeTestContext();
18+
RegisterServices();
19+
InitializeCommonTestData();
20+
}
21+
22+
private void InitializeTestContext()
1523
{
1624
TestContext = new();
1725
TestContext.JSInterop.Mode = JSRuntimeMode.Loose;
26+
SetupJsInterop();
27+
}
28+
29+
private void SetupJsInterop()
30+
{
1831
TestContext.JSInterop.Setup<bool>("Oqtane.Interop.formValid", _ => true).SetResult(true);
1932
TestContext.JSInterop.Setup<bool>("formValid", _ => true).SetResult(true);
2033
TestContext.JSInterop.SetupVoid("Oqtane.Interop.setElementAttribute", _ => true);
@@ -31,7 +44,10 @@ protected BaseTest()
3144
TestContext.JSInterop.SetupVoid("Oqtane.Interop.includeMeta", _ => true);
3245
TestContext.JSInterop.Setup<int>("Oqtane.Interop.getScrollPosition", _ => true).SetResult(0);
3346
TestContext.JSInterop.SetupVoid("Oqtane.Interop.scrollTo", _ => true);
47+
}
3448

49+
private void RegisterServices()
50+
{
3551
TestContext.Services.AddLocalization();
3652
TestContext.Services.AddSingleton<Microsoft.Extensions.Localization.IStringLocalizerFactory, MockStringLocalizerFactory>();
3753

@@ -65,17 +81,14 @@ protected BaseTest()
6581
TestContext.Services.AddScoped<IServiceScopeFactory>(sp => sp.GetRequiredService<IServiceScopeFactory>());
6682

6783
// Add HttpClient for Blazor components
68-
TestContext.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri("https://localhost:5001/") });
84+
TestContext.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(TestBaseUrl) });
6985

7086
// Add PageState mock for cascading parameter
7187
TestContext.Services.AddScoped(_ => new Mocks.PageState
7288
{
7389
Action = "Index",
7490
QueryString = []
7591
});
76-
77-
// Initialize common test data
78-
InitializeCommonTestData();
7992
}
8093

8194
private void InitializeCommonTestData()
@@ -97,7 +110,9 @@ private void InitializeCommonTestData()
97110
LogoFileId = null,
98111
FaviconFileId = null,
99112
DefaultThemeType = "Test.Theme",
113+
#pragma warning disable CS0618 // Type or member is obsolete - Required for Oqtane compatibility
100114
DefaultLayoutType = "Test.Layout",
115+
#pragma warning restore CS0618
101116
DefaultContainerType = "Test.Container",
102117
};
103118

@@ -119,11 +134,11 @@ private void InitializeCommonTestData()
119134
};
120135
}
121136

122-
protected Mocks.PageState CreatePageState(string action, Dictionary<string, string>? queryString = null)
137+
protected Mocks.PageState CreatePageState(string action, IDictionary<string, string>? queryString = null)
123138
=> new Mocks.PageState
124139
{
125140
Action = action,
126-
QueryString = queryString ?? [],
141+
QueryString = queryString ?? new Dictionary<string, string>(StringComparer.Ordinal),
127142
Page = TestPage,
128143
Alias = TestAlias,
129144
Site = TestSite,
@@ -159,8 +174,8 @@ protected Module CreateModuleState(int moduleId = 1, int pageId = 1, string titl
159174
PackageName = "ICTAce.FileHub",
160175
SiteId = 1
161176
},
162-
PermissionList = new List<Permission>
163-
{
177+
PermissionList =
178+
[
164179
new Permission
165180
{
166181
PermissionName = "View",
@@ -181,8 +196,8 @@ protected Module CreateModuleState(int moduleId = 1, int pageId = 1, string titl
181196
UserId = null,
182197
IsAuthorized = true
183198
}
184-
},
185-
Settings = new()
199+
],
200+
Settings = new Dictionary<string, string>(StringComparer.Ordinal)
186201
};
187202

188203
protected virtual void Dispose(bool disposing)

Client.Tests/GlobalSetup.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Licensed to ICTAce under the MIT license.
2+
3+
// You can use attributes at the assembly level to apply to all tests in the assembly
4+
[assembly: System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]

Client.Tests/Mocks/MockSettingService.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace ICTAce.FileHub.Client.Tests.Mocks;
1111
/// </summary>
1212
public class MockSettingService : ISettingService
1313
{
14-
private readonly Dictionary<string, Dictionary<int, Dictionary<string, string>>> _settings = new();
15-
private readonly List<Setting> _settingsList = new();
14+
private readonly Dictionary<string, Dictionary<int, Dictionary<string, string>>> _settings = new(StringComparer.Ordinal);
15+
private readonly List<Setting> _settingsList = [];
1616
private int _nextId = 1;
1717

1818
public Task<Dictionary<string, string>> GetModuleSettingsAsync(int moduleId)
@@ -45,22 +45,22 @@ public Task<Dictionary<string, string>> GetSettingsAsync(string entityName, int
4545
if (_settings.TryGetValue(entityName, out var entitySettings)
4646
&& entitySettings.TryGetValue(entityId, out var settings))
4747
{
48-
return Task.FromResult(new Dictionary<string, string>(settings));
48+
return Task.FromResult(new Dictionary<string, string>(settings, StringComparer.Ordinal));
4949
}
50-
return Task.FromResult(new Dictionary<string, string>());
50+
return Task.FromResult(new Dictionary<string, string>(StringComparer.Ordinal));
5151
}
5252

5353
public Task<List<Setting>> GetSettingsAsync(string entityName, int entityId, string settingName)
5454
{
5555
var matchingSettings = _settingsList
56-
.Where(s => s.EntityName == entityName && s.EntityId == entityId && s.SettingName == settingName)
56+
.Where(s => string.Equals(s.EntityName, entityName, StringComparison.Ordinal) && s.EntityId == entityId && string.Equals(s.SettingName, settingName, StringComparison.Ordinal))
5757
.ToList();
5858
return Task.FromResult(matchingSettings);
5959
}
6060

6161
public Task<Setting> GetSettingAsync(int entityId, string settingName)
6262
{
63-
var setting = _settingsList.FirstOrDefault(s => s.EntityId == entityId && s.SettingName == settingName);
63+
var setting = _settingsList.FirstOrDefault(s => s.EntityId == entityId && string.Equals(s.SettingName, settingName, StringComparison.Ordinal));
6464
return Task.FromResult(setting ?? null!);
6565
}
6666

@@ -128,8 +128,8 @@ public Task AddOrUpdateSettingAsync(string entityName, int entityId, string sett
128128
EnsureEntity(entityName, entityId);
129129
_settings[entityName][entityId][settingName] = settingValue;
130130

131-
var existing = _settingsList.FirstOrDefault(s => s.EntityName == entityName
132-
&& s.EntityId == entityId && s.SettingName == settingName);
131+
var existing = _settingsList.FirstOrDefault(s => string.Equals(s.EntityName, entityName, StringComparison.Ordinal)
132+
&& s.EntityId == entityId && string.Equals(s.SettingName, settingName, StringComparison.Ordinal));
133133
if (existing != null)
134134
{
135135
existing.SettingValue = settingValue;
@@ -336,3 +336,4 @@ private void EnsureEntity(string entityName, int entityId)
336336
}
337337
}
338338
}
339+

Directory.Build.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@
6565
</ItemGroup>
6666

6767
</Project>
68+
69+

EndToEnd.Tests/GlobalUsings.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to ICTAce under the MIT license.
22

33
global using System.Diagnostics;
4-
global using System.Text.RegularExpressions;
54
global using Microsoft.Playwright;
65
global using TUnit.Assertions.Extensions;
76
global using TUnit.Core;

0 commit comments

Comments
 (0)