-
-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathTableDialogTest.cs
More file actions
178 lines (151 loc) · 6.74 KB
/
TableDialogTest.cs
File metadata and controls
178 lines (151 loc) · 6.74 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using AngleSharp.Dom;
using BootstrapBlazor.Shared;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
namespace UnitTest.Components;
public class TableDialogTest : TableDialogTestBase
{
[Fact]
public async Task EditAsync_Ok()
{
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var items = Foo.GenerateFoo(localizer, 2);
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.RenderMode, TableRenderMode.Table);
pb.Add(a => a.Items, items);
pb.Add(a => a.EditDialogIsDraggable, true);
pb.Add(a => a.EditDialogShowMaximizeButton, false);
pb.Add(a => a.EditDialogFullScreenSize, FullScreenSize.None);
pb.Add(a => a.EditDialogSize, Size.Large);
pb.Add(a => a.EditDialogSaveButtonText, "test-save");
pb.Add(a => a.EditDialogCloseButtonText, "test-close");
pb.Add(a => a.EditDialogItemsPerRow, 2);
pb.Add(a => a.EditDialogRowType, RowType.Inline);
pb.Add(a => a.EditDialogLabelAlign, Alignment.Center);
pb.Add(a => a.IsMultipleSelect, true);
pb.Add(a => a.ShowToolbar, true);
pb.Add(a => a.TableColumns, foo => builder =>
{
builder.OpenComponent<TableColumn<Foo, string>>(0);
builder.AddAttribute(1, "Field", "Name");
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
builder.CloseComponent();
});
pb.Add(a => a.OnSaveAsync, (foo, itemType) => Task.FromResult(true));
});
});
var table = cut.FindComponent<Table<Foo>>();
// 选一个
var input = cut.Find("tbody tr input");
await cut.InvokeAsync(() => input.Click());
await cut.InvokeAsync(() => table.Instance.EditAsync());
cut.Contains("test-save");
cut.Contains("test-close");
cut.Contains("modal-lg");
cut.DoesNotContain("btn-maximize");
cut.Contains("is-draggable");
// 编辑弹窗逻辑
var form = cut.Find(".modal-body form");
await cut.InvokeAsync(() => form.Submit());
var modal = cut.FindComponent<Modal>();
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
// 内置数据服务取消回调
await cut.InvokeAsync(() => table.Instance.EditAsync());
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
// 自定义数据服务取消回调测试
table.SetParametersAndRender(pb =>
{
pb.Add(a => a.DataService, new MockEFCoreDataService(localizer));
});
await cut.InvokeAsync(() => table.Instance.EditAsync());
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
// Add 弹窗
await cut.InvokeAsync(() => table.Instance.AddAsync());
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
// 自定义数据服务取消回调测试
table.SetParametersAndRender(pb =>
{
pb.Add(a => a.EditDialogFullScreenSize, FullScreenSize.Always);
});
await cut.InvokeAsync(() => table.Instance.AddAsync());
Assert.Contains(" modal-fullscreen ", cut.Markup);
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
var closed = false;
// 测试 CloseCallback
table.SetParametersAndRender(pb =>
{
pb.Add(a => a.EditDialogCloseAsync, (model, result) =>
{
closed = true;
return Task.CompletedTask;
});
});
await cut.InvokeAsync(() => table.Instance.AddAsync());
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
Assert.True(closed);
// IsTracking mode
table.SetParametersAndRender(pb =>
{
pb.Add(a => a.IsTracking, true);
});
// Add 弹窗
await cut.InvokeAsync(() => table.Instance.AddAsync());
// 编辑弹窗逻辑
input = cut.Find(".modal-body form input.form-control");
await cut.InvokeAsync(() => input.Change("Test_Name"));
form = cut.Find(".modal-body form");
await cut.InvokeAsync(() => form.Submit());
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
var itemsChanged = false;
// 更新插入模式
table.SetParametersAndRender(pb =>
{
pb.Add(a => a.InsertRowMode, InsertRowMode.First);
pb.Add(a => a.ItemsChanged, foo =>
{
itemsChanged = true;
});
pb.Add(a => a.EditFooterTemplate, foo => builder => builder.AddContent(0, "test_edit_footer"));
});
// Add 弹窗
await cut.InvokeAsync(() => table.Instance.AddAsync());
cut.Contains("test_edit_footer");
// 编辑弹窗逻辑
input = cut.Find(".modal-body form input.form-control");
await cut.InvokeAsync(() => input.Change("Test_Name"));
form = cut.Find(".modal-body form");
await cut.InvokeAsync(() => form.Submit());
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
Assert.True(itemsChanged);
}
private class MockEFCoreDataService : IDataService<Foo>, IEntityFrameworkCoreDataService
{
IStringLocalizer<Foo> Localizer { get; set; }
public MockEFCoreDataService(IStringLocalizer<Foo> localizer) => Localizer = localizer;
public Task<bool> AddAsync(Foo model) => Task.FromResult(true);
public Task<bool> DeleteAsync(IEnumerable<Foo> models) => Task.FromResult(true);
public Task<QueryData<Foo>> QueryAsync(QueryPageOptions option)
{
var foos = Foo.GenerateFoo(Localizer, 2);
var ret = new QueryData<Foo>()
{
Items = foos,
TotalCount = 2,
IsAdvanceSearch = true,
IsFiltered = true,
IsSearch = true,
IsSorted = true
};
return Task.FromResult(ret);
}
public Task<bool> SaveAsync(Foo model, ItemChangedType changedType) => Task.FromResult(true);
public Task CancelAsync() => Task.CompletedTask;
public Task EditAsync(object model) => Task.CompletedTask;
}
}