Skip to content

Commit 455d962

Browse files
committed
test: 更新单元测试
1 parent cac7b10 commit 455d962

File tree

3 files changed

+135
-380
lines changed

3 files changed

+135
-380
lines changed

test/UnitTest/Components/AutoCompleteTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ public void ShowDropdownListOnFocus_Ok()
134134
{
135135
pb.Add(a => a.Items, items);
136136
});
137-
cut.DoesNotContain("data-bb-auto-dropdown-focus");
137+
cut.Contains("data-bb-auto-dropdown-focus=\"true\"");
138138

139139
cut.SetParametersAndRender(pb =>
140140
{
141141
pb.Add(a => a.ShowDropdownListOnFocus, false);
142142
});
143-
cut.Contains("data-bb-auto-dropdown-focus=\"false\"");
143+
cut.DoesNotContain("data-bb-auto-dropdown-focus");
144144
}
145145

146146
[Fact]

test/UnitTest/Components/AutoFillTest.cs

Lines changed: 74 additions & 199 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ public AutoFillTest()
2323
Items = Foo.GenerateFoo(Localizer, 2);
2424
}
2525

26+
[Fact]
27+
public void Items_Ok()
28+
{
29+
var cut = Context.RenderComponent<AutoFill<Foo>>();
30+
Assert.Contains("<div class=\"auto-complete auto-fill\"", cut.Markup);
31+
var menus = cut.FindAll(".dropdown-item");
32+
Assert.Single(menus);
33+
34+
cut.SetParametersAndRender(pb =>
35+
{
36+
pb.Add(a => a.ShowNoDataTip, false);
37+
});
38+
menus = cut.FindAll(".dropdown-item");
39+
Assert.Empty(menus);
40+
}
41+
2642
[Fact]
2743
public void ShowLabel_Ok()
2844
{
@@ -37,180 +53,107 @@ public void ShowLabel_Ok()
3753
}
3854

3955
[Fact]
40-
public void NullItems_Ok()
56+
public void ItemTemplate_Ok()
4157
{
42-
var cut = Context.RenderComponent<AutoFill<Foo>>();
43-
Assert.Contains("dropdown-menu", cut.Markup);
58+
var items = new List<Foo>() { new() { Name = "test1" }, new() { Name = "test2" } };
59+
var cut = Context.RenderComponent<AutoFill<Foo>>(pb =>
60+
{
61+
pb.Add(a => a.Items, items);
62+
pb.Add(a => a.ItemTemplate, item => builder =>
63+
{
64+
builder.AddContent(0, $"Template-{item.Name}");
65+
});
66+
});
67+
68+
Assert.Contains("Template-test1", cut.Markup);
69+
Assert.Contains("Template-test2", cut.Markup);
4470
}
4571

4672
[Fact]
47-
public void OnCustomFilter_Ok()
73+
public async Task OnCustomFilter_Ok()
4874
{
4975
var filtered = false;
5076
var cut = Context.RenderComponent<AutoFill<Foo>>(pb =>
5177
{
5278
pb.Add(a => a.Value, Model);
5379
pb.Add(a => a.Items, Items);
54-
pb.Add(a => a.OnCustomFilter, new Func<string, Task<IEnumerable<Foo>>>(key =>
80+
pb.Add(a => a.OnCustomFilter, key =>
5581
{
5682
filtered = true;
5783
var items = Foo.GenerateFoo(Localizer, 3);
5884
return Task.FromResult(items.AsEnumerable());
59-
}));
85+
});
6086
});
61-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("t"));
87+
await cut.InvokeAsync(() => cut.Instance.TriggerOnChange("t"));
6288
Assert.True(filtered);
6389
}
6490

91+
6592
[Fact]
66-
public void Escape_Ok()
93+
public void SkipEnter_Ok()
6794
{
68-
var escTrigger = false;
69-
var cut = Context.RenderComponent<AutoFill<Foo>>(pb =>
95+
var cut = Context.RenderComponent<AutoComplete>(pb =>
7096
{
71-
pb.Add(a => a.Value, Model);
72-
pb.Add(a => a.Items, Items);
73-
pb.Add(a => a.SkipEsc, true);
74-
pb.Add(a => a.OnEscAsync, new Func<Foo, Task>(foo =>
75-
{
76-
escTrigger = true;
77-
return Task.CompletedTask;
78-
}));
97+
pb.Add(a => a.SkipEnter, false);
7998
});
80-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("t"));
81-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("Escape"));
82-
Assert.False(escTrigger);
99+
cut.DoesNotContain("data-bb-skip-enter");
83100

84101
cut.SetParametersAndRender(pb =>
85102
{
86-
pb.Add(a => a.SkipEsc, false);
103+
pb.Add(a => a.SkipEnter, true);
87104
});
88-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("t"));
89-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("Escape"));
90-
Assert.True(escTrigger);
105+
cut.Contains("data-bb-skip-enter=\"true\"");
91106
}
92107

93108
[Fact]
94-
public void Enter_Ok()
109+
public void SkipEsc_Ok()
95110
{
96-
var enterTrigger = false;
97111
var cut = Context.RenderComponent<AutoFill<Foo>>(pb =>
98112
{
99-
pb.Add(a => a.Value, Model);
100-
pb.Add(a => a.Items, Items);
101-
pb.Add(a => a.SkipEnter, true);
102-
pb.Add(a => a.OnEnterAsync, new Func<Foo, Task>(foo =>
103-
{
104-
enterTrigger = true;
105-
return Task.CompletedTask;
106-
}));
113+
pb.Add(a => a.SkipEsc, false);
107114
});
108-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("t"));
109-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("Enter"));
110-
Assert.False(enterTrigger);
115+
cut.DoesNotContain("data-bb-skip-esc");
111116

112-
Foo? selectedItem = null;
113117
cut.SetParametersAndRender(pb =>
114118
{
115-
pb.Add(a => a.SkipEnter, false);
116-
pb.Add(a => a.OnSelectedItemChanged, new Func<Foo, Task>(foo =>
117-
{
118-
selectedItem = foo;
119-
return Task.CompletedTask;
120-
}));
119+
pb.Add(a => a.SkipEsc, true);
121120
});
122-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("t"));
123-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("Enter"));
124-
Assert.True(enterTrigger);
125-
Assert.NotNull(selectedItem);
121+
cut.Contains("data-bb-skip-esc=\"true\"");
126122
}
127123

128124
[Fact]
129-
public void OnSelectedItemChanged_Ok()
125+
public void ScrollIntoViewBehavior_Ok()
130126
{
131-
Foo? selectedItem = null;
132127
var cut = Context.RenderComponent<AutoFill<Foo>>(pb =>
133128
{
134-
pb.Add(a => a.Value, Model);
135-
pb.Add(a => a.Items, Items);
136-
pb.Add(a => a.OnSelectedItemChanged, new Func<Foo, Task>(foo =>
137-
{
138-
selectedItem = foo;
139-
return Task.CompletedTask;
140-
}));
129+
pb.Add(a => a.ScrollIntoViewBehavior, ScrollIntoViewBehavior.Smooth);
141130
});
142-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("t"));
143-
cut.Find(".dropdown-item").MouseDown(new MouseEventArgs());
144-
Assert.NotNull(selectedItem);
145-
}
131+
cut.DoesNotContain("data-bb-scroll-behavior");
146132

147-
[Fact]
148-
public void KeyUp_Test()
149-
{
150-
var cut = Context.RenderComponent<AutoFill<Foo>>(pb =>
133+
cut.SetParametersAndRender(pb =>
151134
{
152-
pb.Add(a => a.Value, Model);
153-
pb.Add(a => a.Items, Items);
135+
pb.Add(a => a.ScrollIntoViewBehavior, ScrollIntoViewBehavior.Auto);
154136
});
155-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("t"));
156-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("ArrowUp"));
157-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("ArrowUp"));
158-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("ArrowUp"));
159-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("ArrowDown"));
160-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("ArrowDown"));
161-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("ArrowDown"));
137+
cut.Contains("data-bb-scroll-behavior=\"auto\"");
162138
}
163139

164140
[Fact]
165-
public void DisplayCount_Ok()
141+
public async Task OnSelectedItemChanged_Ok()
166142
{
143+
Foo? selectedItem = null;
167144
var cut = Context.RenderComponent<AutoFill<Foo>>(pb =>
168145
{
169146
pb.Add(a => a.Value, Model);
170147
pb.Add(a => a.Items, Items);
171-
pb.Add(a => a.IsLikeMatch, true);
172-
pb.Add(a => a.IgnoreCase, false);
173-
pb.Add(a => a.DisplayCount, 2);
174-
});
175-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("Z"));
176-
Assert.Equal(2, cut.FindAll(".dropdown-item").Count);
177-
}
178-
179-
[Fact]
180-
public void OnGetDisplayText_Null()
181-
{
182-
var v = new AutoFillNullStringMock();
183-
var cut = Context.RenderComponent<AutoFill<AutoFillNullStringMock?>>(pb =>
184-
{
185-
pb.Add(a => a.IgnoreCase, true);
186-
pb.Add(a => a.Value, v);
187-
pb.Add(a => a.Items, new List<AutoFillNullStringMock>
188-
{
189-
new() { Value = "1" },
190-
new() { Value = "2" },
191-
});
192-
pb.Add(a => a.Template, v => builder =>
148+
pb.Add(a => a.OnSelectedItemChanged, foo =>
193149
{
194-
builder.OpenElement(0, "div");
195-
builder.AddContent(1, v!.Value);
196-
builder.CloseElement();
197-
});
198-
});
199-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("1"));
200-
cut.InvokeAsync(() => cut.Find(".dropdown-item").MouseDown(new MouseEventArgs()));
201-
Assert.Equal("1", cut.Instance.Value!.Value);
202-
203-
cut.SetParametersAndRender(pb =>
204-
{
205-
pb.Add(a => a.Items, new List<AutoFillNullStringMock?>
206-
{
207-
null,
208-
new() { Value = "2" },
150+
selectedItem = foo;
151+
return Task.CompletedTask;
209152
});
210-
pb.Add(a => a.Template, (RenderFragment<AutoFillNullStringMock?>?)null);
211153
});
212-
cut.InvokeAsync(() => cut.Instance.OnKeyUp("2"));
213-
Assert.Equal(2, cut.FindAll(".dropdown-item").Count);
154+
var item = cut.Find(".dropdown-item");
155+
await cut.InvokeAsync(() => item.Click());
156+
Assert.NotNull(selectedItem);
214157
}
215158

216159
[Fact]
@@ -227,102 +170,34 @@ public void OnGetDisplayText_Ok()
227170
}
228171

229172
[Fact]
230-
public void Debounce_Ok()
173+
public async Task DisplayCount_Ok()
231174
{
232-
var cut = Context.RenderComponent<AutoFill<Foo>>(pb =>
175+
var items = new List<Foo>() { new() { Name = "task1" }, new() { Name = "Task2" }, new() { Name = "task3" }, new() { Name = "Task4" } };
176+
var cut = Context.RenderComponent<AutoFill<Foo>>(builder =>
233177
{
234-
pb.Add(a => a.Value, Model);
235-
pb.Add(a => a.Items, Items);
236-
pb.Add(a => a.Debounce, 200);
178+
builder.Add(a => a.Items, items);
179+
builder.Add(a => a.DisplayCount, 2);
237180
});
181+
182+
await cut.InvokeAsync(() => cut.Instance.TriggerOnChange("t"));
183+
var menus = cut.FindAll(".dropdown-item");
184+
Assert.Equal(2, menus.Count);
238185
}
239186

240187
[Fact]
241-
public async Task ShowDropdownListOnFocus_Ok()
188+
public void ShowDropdownListOnFocus_Ok()
242189
{
190+
var items = new List<Foo>() { new() { Name = "test1" }, new() { Name = "test2" } };
243191
var cut = Context.RenderComponent<AutoFill<Foo>>(pb =>
244192
{
245-
pb.Add(a => a.Value, Model);
246-
pb.Add(a => a.Items, Items);
247-
pb.Add(a => a.ShowDropdownListOnFocus, false);
193+
pb.Add(a => a.Items, items);
248194
});
195+
cut.Contains("data-bb-auto-dropdown-focus=\"true\"");
249196

250-
// 获得焦点时不会自动弹出下拉框
251-
var input = cut.Find("input");
252-
await cut.InvokeAsync(() => input.FocusAsync(new FocusEventArgs()));
253-
254-
var menu = cut.Find("ul");
255-
Assert.Equal("dropdown-menu", menu.ClassList.ToString());
256-
257-
// 获得焦点时自动弹出下拉框
258197
cut.SetParametersAndRender(pb =>
259198
{
260-
pb.Add(a => a.ShowDropdownListOnFocus, true);
261-
});
262-
input = cut.Find("input");
263-
await cut.InvokeAsync(() => input.FocusAsync(new FocusEventArgs()));
264-
}
265-
266-
[Fact]
267-
public async Task ValidateForm_Ok()
268-
{
269-
var v = "";
270-
var trigger = false;
271-
IEnumerable<string> items = new List<string>() { "test1", "test2" };
272-
var cut = Context.RenderComponent<ValidateForm>(pb =>
273-
{
274-
pb.Add(a => a.Model, new Foo());
275-
pb.AddChildContent<AutoFill<string>>(pb =>
276-
{
277-
pb.Add(a => a.Items, items);
278-
pb.Add(a => a.OnCustomFilter, key =>
279-
{
280-
v = key;
281-
trigger = true;
282-
return Task.FromResult(items);
283-
});
284-
});
285-
});
286-
287-
// Trigger js invoke
288-
var comp = cut.FindComponent<AutoFill<string>>().Instance;
289-
comp.TriggerOnChange("v");
290-
await cut.InvokeAsync(() => comp.OnKeyUp("v"));
291-
Assert.Equal("v", v);
292-
Assert.True(trigger);
293-
294-
// not trigger OnKeyUp
295-
v = "";
296-
trigger = false;
297-
await cut.InvokeAsync(() => comp.OnKeyUp("Enter"));
298-
Assert.False(trigger);
299-
}
300-
301-
[Fact]
302-
public async Task OnBlurAsync_Ok()
303-
{
304-
var blur = false;
305-
var cut = Context.RenderComponent<AutoFill<Foo>>(builder =>
306-
{
307-
builder.Add(a => a.OnBlurAsync, v =>
308-
{
309-
blur = true;
310-
return Task.CompletedTask;
311-
});
199+
pb.Add(a => a.ShowDropdownListOnFocus, false);
312200
});
313-
var input = cut.Find("input");
314-
await cut.InvokeAsync(() => { input.Blur(); });
315-
Assert.True(blur);
316-
}
317-
318-
class AutoFillNullStringMock
319-
{
320-
[NotNull]
321-
public string? Value { get; set; }
322-
323-
public override string? ToString()
324-
{
325-
return null;
326-
}
201+
cut.DoesNotContain("data-bb-auto-dropdown-focus");
327202
}
328203
}

0 commit comments

Comments
 (0)