Skip to content

Commit 10d6325

Browse files
committed
Add Unit tests
1 parent c6fcd92 commit 10d6325

4 files changed

+236
-21
lines changed

tests/Core/Components/Dialog/FluentDialogTests.razor

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@
8080
// Arrange
8181
var renderOptions = new Templates.DialogRenderOptions();
8282
var toggleArgs = new DialogToggleEventArgs()
83-
{
84-
Id = "my-id",
85-
Type = eventType,
86-
OldState = oldState,
87-
NewState = newState,
88-
};
83+
{
84+
Id = "my-id",
85+
Type = eventType,
86+
OldState = oldState,
87+
NewState = newState,
88+
};
8989

9090
DialogEventArgs? dialogEventArgs = null;
9191

@@ -108,7 +108,8 @@
108108

109109
// Find the dialog and close it
110110
var dialog = DialogProvider.FindComponent<FluentDialog>();
111-
await dialog.Instance.OnToggleAsync(toggleArgs);
111+
await dialog.Instance.OnToggleAsync(new() { Id = "NotCorrectId" }); // Incorrect dialog instance
112+
await dialog.Instance.OnToggleAsync(toggleArgs); // Correct dialog instance
112113
113114
// Assert
114115
Assert.NotNull(dialogEventArgs?.Instance);
@@ -121,9 +122,9 @@
121122
{
122123
// Arrange
123124
var renderOptions = new Templates.DialogRenderOptions()
124-
{
125-
AutoClose = true,
126-
};
125+
{
126+
AutoClose = true,
127+
};
127128

128129
// Act
129130
var dialogTask = DialogService.ShowDialogAsync<Templates.DialogRender>(options =>
@@ -209,11 +210,11 @@
209210
{
210211
// Arrange
211212
var renderOptions = new Templates.DialogRenderOptions()
212-
{
213-
AutoClose = true,
214-
AutoCloseDelay = 200,
215-
AutoCloseResult = DialogResult.Ok("AUTO_CLOSED"),
216-
};
213+
{
214+
AutoClose = true,
215+
AutoCloseDelay = 200,
216+
AutoCloseResult = DialogResult.Ok("AUTO_CLOSED"),
217+
};
217218

218219
// Act
219220
var dialogTask = DialogService.ShowDialogAsync<Templates.DialogRender>(options =>
@@ -478,12 +479,12 @@
478479
var renderOptions = new Templates.DialogRenderOptions();
479480

480481
var dialogOptions = new DialogOptions()
481-
{
482-
Class = "my-class",
483-
Style = "border: 1px solid red;",
484-
Margin = "10px",
485-
Padding = "20px",
486-
};
482+
{
483+
Class = "my-class",
484+
Style = "border: 1px solid red;",
485+
Margin = "10px",
486+
Padding = "20px",
487+
};
487488
dialogOptions.Parameters.Add(nameof(Templates.DialogRender.Options), renderOptions);
488489
dialogOptions.Parameters.Add(nameof(Templates.DialogRender.Name), "John");
489490

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
<div id="xxx" class="fluent-dialog-provider" style="z-index: 999;">
3+
<div class="fluent-inputfile-container" style="display: none;" blazor:elementreference="xxx">
4+
<div class="inputfile-content" style="z-index: 991">
5+
<div class="inputfile-progress" style="visibility: hidden;">
6+
<fluent-progress-bar min="0" max="100" value="0"></fluent-progress-bar>
7+
<br>
8+
</div>
9+
</div>
10+
<div style="grid-column: 1; grid-row: 1; ">
11+
<input id="xxx" accept="" type="file" blazor:elementreference="xxx">
12+
</div>
13+
</div>
14+
</div>
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
@using Xunit;
2+
@inherits Bunit.TestContext
3+
@code
4+
{
5+
public FluentInputFileDialogServiceTests()
6+
{
7+
JSInterop.Mode = JSRuntimeMode.Loose;
8+
Services.AddFluentUIComponents();
9+
10+
DialogService = Services.GetRequiredService<IDialogService>();
11+
DialogProvider = RenderComponent<FluentDialogProvider>();
12+
}
13+
14+
/// <summary>
15+
/// Gets the dialog service.
16+
/// </summary>
17+
public IDialogService DialogService { get; }
18+
19+
/// <summary>
20+
/// Gets the dialog provider.
21+
/// </summary>
22+
public IRenderedComponent<FluentDialogProvider> DialogProvider { get; }
23+
24+
[Fact]
25+
public async Task FluentInputFile_DialogService_Register()
26+
{
27+
// Arrange
28+
await DialogService.RegisterInputFileAsync("MyButton", (args) => Task.CompletedTask);
29+
30+
// Assert
31+
DialogProvider.Verify();
32+
}
33+
34+
[Fact]
35+
public async Task FluentInputFile_DialogService_Options()
36+
{
37+
// Arrange
38+
await DialogService.RegisterInputFileAsync("MyButton", (args) => Task.CompletedTask, options =>
39+
{
40+
options.Accept = "image/*";
41+
options.Multiple = true;
42+
options.MaximumFileSize = 12_345;
43+
options.MaximumFileCount = 5;
44+
options.BufferSize = 1_234;
45+
options.Mode = InputFileMode.Stream;
46+
options.OnFileErrorAsync = (args) => Task.CompletedTask;
47+
options.OnFileUploadedAsync = (args) => Task.CompletedTask;
48+
options.OnProgressChangeAsync = (args) => Task.CompletedTask;
49+
});
50+
51+
// Assert
52+
var inputFile = DialogProvider.FindComponent<FluentInputFile>();
53+
54+
Assert.Equal("image/*", inputFile.Instance.Accept);
55+
Assert.True(inputFile.Instance.Multiple);
56+
Assert.Equal(12_345, inputFile.Instance.MaximumFileSize);
57+
Assert.Equal(5, inputFile.Instance.MaximumFileCount);
58+
Assert.Equal(1_234u, inputFile.Instance.BufferSize);
59+
Assert.Equal(InputFileMode.Stream, inputFile.Instance.Mode);
60+
}
61+
62+
[Fact]
63+
public async Task FluentInputFile_DialogService_UploadCompleted()
64+
{
65+
//int ProgressPercent = 0;
66+
var fileToUpload = InputFileContent.CreateFromText("Text content", "Filename.txt");
67+
var filesDownloaded = new List<FluentInputFileEventArgs>();
68+
69+
// Arrange
70+
await DialogService.RegisterInputFileAsync("MyButton", CompletedAsync, options => { options.Multiple = true; });
71+
72+
// Act
73+
var inputFile = DialogProvider.FindComponent<InputFile>();
74+
inputFile.UploadFiles(fileToUpload);
75+
76+
// Assert
77+
Assert.Single(filesDownloaded);
78+
Assert.Equal(0, filesDownloaded[0].Index);
79+
Assert.False(filesDownloaded[0].IsCancelled);
80+
Assert.Equal("Filename.txt", filesDownloaded[0].Name);
81+
82+
async Task CompletedAsync(IEnumerable<FluentInputFileEventArgs> args)
83+
{
84+
filesDownloaded = args.ToList();
85+
await Task.CompletedTask;
86+
}
87+
}
88+
89+
[Fact]
90+
public async Task FluentInputFile_DialogService_Unregister()
91+
{
92+
// Arrange
93+
await DialogService.RegisterInputFileAsync("MyButton", (args) => Task.CompletedTask);
94+
95+
// Assert
96+
DialogProvider.Markup.Contains("<input");
97+
98+
// Act
99+
await DialogService.UnregisterInputFileAsync("MyButton");
100+
101+
// Assert
102+
Assert.Empty(DialogProvider.Find(".fluent-dialog-provider").InnerHtml);
103+
}
104+
105+
[Fact]
106+
public async Task FluentInputFile_DialogService_Instance()
107+
{
108+
// Arrange
109+
var instance = await DialogService.RegisterInputFileAsync("MyButton", (args) => Task.CompletedTask);
110+
111+
// Assert
112+
Assert.Equal("MyButton", instance.AnchorId);
113+
Assert.NotEmpty(instance.Id);
114+
115+
// Act
116+
await instance.UnregisterAsync();
117+
118+
// Assert
119+
Assert.Empty(DialogProvider.Find(".fluent-dialog-provider").InnerHtml);
120+
121+
await instance.DisposeAsync();
122+
}
123+
124+
[Fact]
125+
public async Task FluentInputFile_DialogService_RegisterWithoutProvider()
126+
{
127+
// Arrange
128+
DialogProvider.Instance.UpdateId(null);
129+
130+
// Act
131+
await Assert.ThrowsAsync<FluentServiceProviderException<FluentDialogProvider>>(async () =>
132+
{
133+
await DialogService.RegisterInputFileAsync("MyButton", (args) => Task.CompletedTask);
134+
});
135+
136+
await ((DialogService)DialogService).RemoveDialogFromProviderAsync(null);
137+
}
138+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// ------------------------------------------------------------------------
2+
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved.
3+
// ------------------------------------------------------------------------
4+
5+
using Microsoft.FluentUI.AspNetCore.Components;
6+
using Xunit;
7+
8+
namespace Microsoft.FluentUI.AspNetCore.Components.Tests.Components.TextArea;
9+
10+
public class FluentTextArea_KeyPress
11+
{
12+
[Fact]
13+
public void FluentTextArea_KeyPress_Combined_True()
14+
{
15+
var keys = KeyPress.For(AspNetCore.Components.KeyCode.KeyA)
16+
.AndAltKey()
17+
.AndCtrlKey()
18+
.AndMetaKey()
19+
.AndShiftKey()
20+
.WithPreventDefault();
21+
22+
Assert.True(keys.AltKey);
23+
Assert.True(keys.CtrlKey);
24+
Assert.True(keys.ShiftKey);
25+
Assert.True(keys.MetaKey);
26+
Assert.False(keys.PreventDefault);
27+
}
28+
29+
[Fact]
30+
public void FluentTextArea_KeyPress_Combined_False()
31+
{
32+
var keys = KeyPress.For(AspNetCore.Components.KeyCode.KeyA)
33+
.AndAltKey(pressed: false)
34+
.AndCtrlKey(pressed: false)
35+
.AndMetaKey(pressed: false)
36+
.AndShiftKey(pressed: false)
37+
.WithPreventDefault(prevent: true);
38+
39+
Assert.False(keys.AltKey);
40+
Assert.False(keys.CtrlKey);
41+
Assert.False(keys.ShiftKey);
42+
Assert.False(keys.MetaKey);
43+
Assert.True(keys.PreventDefault);
44+
}
45+
46+
[Fact]
47+
public void FluentTextArea_KeyPress_MultipleCombined()
48+
{
49+
var keys = KeyPress.For(AspNetCore.Components.KeyCode.KeyA)
50+
.AndAltKey(pressed: true).AndAltKey(pressed: false)
51+
.AndCtrlKey(pressed: true).AndCtrlKey(pressed: false).AndCtrlKey(pressed: true)
52+
.AndMetaKey(pressed: true).AndMetaKey(pressed: false)
53+
.AndShiftKey(pressed: true).AndShiftKey(pressed: false).AndShiftKey(pressed: true)
54+
.WithPreventDefault().WithPreventDefault(prevent: true);
55+
56+
Assert.False(keys.AltKey);
57+
Assert.True(keys.CtrlKey);
58+
Assert.False(keys.MetaKey);
59+
Assert.True(keys.ShiftKey);
60+
Assert.True(keys.PreventDefault);
61+
}
62+
}

0 commit comments

Comments
 (0)