Skip to content

Commit 59d1f1c

Browse files
committed
test: 增加单元测试
1 parent 57355cb commit 59d1f1c

File tree

1 file changed

+104
-2
lines changed

1 file changed

+104
-2
lines changed

test/UnitTest/Components/TableLookupFilterTest.cs

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@ public async Task FilterAction_Ok()
3131
});
3232
var lookup = cut.FindComponent<LookupFilter>();
3333
var filter = lookup.Instance;
34-
3534
var newConditions = new FilterKeyValueAction()
3635
{
3736
Filters =
3837
[
39-
new FilterKeyValueAction() { FieldValue = "1" },
38+
new FilterKeyValueAction() { FieldValue = "2" },
4039
]
4140
};
4241
await cut.InvokeAsync(() => filter.SetFilterConditionsAsync(newConditions));
4342
var conditions = filter.GetFilterConditions();
4443
Assert.Single(conditions.Filters);
44+
Assert.Equal("2", conditions.Filters[0].FieldValue);
45+
4546
await cut.InvokeAsync(() => filter.Reset());
4647
conditions = filter.GetFilterConditions();
4748
Assert.Empty(conditions.Filters);
@@ -64,6 +65,51 @@ public async Task FilterAction_Ok()
6465
Assert.Empty(conditions.Filters);
6566
}
6667

68+
[Fact]
69+
public async Task LookupService_Ok()
70+
{
71+
var cut = Context.RenderComponent<TableColumnFilter>(pb =>
72+
{
73+
pb.Add(a => a.Table, new MockTable());
74+
pb.Add(a => a.Column, new MockLookupServiceColumn());
75+
});
76+
var lookup = cut.FindComponent<LookupFilter>();
77+
var filter = lookup.Instance;
78+
79+
// 由于 LookupFilter 默认值未设置使用候选项第一个
80+
cut.WaitForAssertion(() => cut.Contains("value=\"LookupService-Test-1-async\""), TimeSpan.FromMilliseconds(100));
81+
var newConditions = new FilterKeyValueAction()
82+
{
83+
Filters =
84+
[
85+
new FilterKeyValueAction() { FieldValue = "v2" },
86+
]
87+
};
88+
await cut.InvokeAsync(() => filter.SetFilterConditionsAsync(newConditions));
89+
var conditions = filter.GetFilterConditions();
90+
Assert.Single(conditions.Filters);
91+
Assert.Equal("v2", conditions.Filters[0].FieldValue);
92+
93+
await cut.InvokeAsync(() => filter.Reset());
94+
conditions = filter.GetFilterConditions();
95+
Assert.Empty(conditions.Filters);
96+
}
97+
98+
[Fact]
99+
public async Task LookupService_Empty()
100+
{
101+
var column = new MockEmptyLookupServiceColumn();
102+
var cut = Context.RenderComponent<TableColumnFilter>(pb =>
103+
{
104+
pb.Add(a => a.Table, new MockTable());
105+
pb.Add(a => a.Column, column);
106+
});
107+
var lookup = cut.FindComponent<LookupFilter>();
108+
var filter = lookup.Instance;
109+
110+
await column.Task;
111+
}
112+
67113
class MockTable : ITable
68114
{
69115
public Dictionary<string, IFilterAction> Filters { get; set; } = [];
@@ -89,4 +135,60 @@ public MockColumn()
89135
};
90136
}
91137
}
138+
139+
class MockLookupServiceColumn : TableColumn<Foo, string>
140+
{
141+
public MockLookupServiceColumn()
142+
{
143+
PropertyType = typeof(string);
144+
FieldName = "Lookup";
145+
LookupService = new LookupFilterService();
146+
LookupServiceKey = "LookupKey";
147+
}
148+
}
149+
150+
class MockEmptyLookupServiceColumn : TableColumn<Foo, string>
151+
{
152+
private LookupFilterService _service = new LookupFilterService();
153+
154+
public MockEmptyLookupServiceColumn()
155+
{
156+
PropertyType = typeof(string);
157+
FieldName = "Lookup";
158+
LookupService = _service;
159+
LookupServiceKey = "LookupEmptyKey";
160+
}
161+
162+
public Task Task => _service.Task;
163+
}
164+
165+
class LookupFilterService : LookupServiceBase
166+
{
167+
private TaskCompletionSource _taskCompletionSource = new();
168+
169+
public override IEnumerable<SelectedItem>? GetItemsByKey(string? key, object? data) => null;
170+
171+
public override async Task<IEnumerable<SelectedItem>?> GetItemsByKeyAsync(string? key, object? data)
172+
{
173+
IEnumerable<SelectedItem>? ret = null;
174+
175+
if (key == "LookupKey")
176+
{
177+
await Task.Delay(30);
178+
ret =
179+
[
180+
new SelectedItem("v1", "LookupService-Test-1-async"),
181+
new SelectedItem("v2", "LookupService-Test-2-async")
182+
];
183+
}
184+
else if (key == "LookupEmptyKey")
185+
{
186+
ret = [];
187+
_taskCompletionSource.TrySetResult();
188+
}
189+
return ret;
190+
}
191+
192+
public Task Task => _taskCompletionSource.Task;
193+
}
92194
}

0 commit comments

Comments
 (0)