@@ -31,17 +31,18 @@ public async Task FilterAction_Ok()
31
31
} ) ;
32
32
var lookup = cut . FindComponent < LookupFilter > ( ) ;
33
33
var filter = lookup . Instance ;
34
-
35
34
var newConditions = new FilterKeyValueAction ( )
36
35
{
37
36
Filters =
38
37
[
39
- new FilterKeyValueAction ( ) { FieldValue = "1 " } ,
38
+ new FilterKeyValueAction ( ) { FieldValue = "2 " } ,
40
39
]
41
40
} ;
42
41
await cut . InvokeAsync ( ( ) => filter . SetFilterConditionsAsync ( newConditions ) ) ;
43
42
var conditions = filter . GetFilterConditions ( ) ;
44
43
Assert . Single ( conditions . Filters ) ;
44
+ Assert . Equal ( "2" , conditions . Filters [ 0 ] . FieldValue ) ;
45
+
45
46
await cut . InvokeAsync ( ( ) => filter . Reset ( ) ) ;
46
47
conditions = filter . GetFilterConditions ( ) ;
47
48
Assert . Empty ( conditions . Filters ) ;
@@ -64,6 +65,51 @@ public async Task FilterAction_Ok()
64
65
Assert . Empty ( conditions . Filters ) ;
65
66
}
66
67
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
+
67
113
class MockTable : ITable
68
114
{
69
115
public Dictionary < string , IFilterAction > Filters { get ; set ; } = [ ] ;
@@ -89,4 +135,60 @@ public MockColumn()
89
135
} ;
90
136
}
91
137
}
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
+ }
92
194
}
0 commit comments