|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +using Microsoft.AspNetCore.Components.Forms; |
| 7 | +using System.ComponentModel.DataAnnotations; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace UnitTest.Components; |
| 11 | + |
| 12 | +public class UploadDropTest : BootstrapBlazorTestBase |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public void DropUpload_BodyTemplate_Ok() |
| 16 | + { |
| 17 | + var cut = Context.RenderComponent<DropUpload>(pb => |
| 18 | + { |
| 19 | + pb.Add(a => a.BodyTemplate, b => b.AddContent(0, "drop-upload-body-template")); |
| 20 | + }); |
| 21 | + cut.MarkupMatches("<div class=\"upload is-drop\" diff:ignore><div class=\"upload-drop-body\">drop-upload-body-template</div><ul class=\"upload-drop-list\"></ul><input hidden=\"hidden\" type=\"file\" diff:ignore></input></div>"); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public void DropUpload_IconTemplate_Ok() |
| 26 | + { |
| 27 | + var cut = Context.RenderComponent<DropUpload>(pb => |
| 28 | + { |
| 29 | + pb.Add(a => a.IconTemplate, b => b.AddContent(0, "drop-upload-icon-template")); |
| 30 | + }); |
| 31 | + cut.Contains("<div class=\"upload-drop-icon\">drop-upload-icon-template</div>"); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void DropUpload_TextTemplate_Ok() |
| 36 | + { |
| 37 | + var cut = Context.RenderComponent<DropUpload>(pb => |
| 38 | + { |
| 39 | + pb.Add(a => a.TextTemplate, b => b.AddContent(0, "drop-upload-text-template")); |
| 40 | + }); |
| 41 | + cut.Contains("<div class=\"upload-drop-text\">drop-upload-text-template</div>"); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void DropUpload_Footer_Ok() |
| 46 | + { |
| 47 | + var cut = Context.RenderComponent<DropUpload>(pb => |
| 48 | + { |
| 49 | + pb.Add(a => a.ShowFooter, true); |
| 50 | + }); |
| 51 | + cut.Contains("<div class=\"upload-drop-footer\"></div>"); |
| 52 | + |
| 53 | + cut.SetParametersAndRender(pb => |
| 54 | + { |
| 55 | + pb.Add(a => a.FooterTemplate, b => b.AddContent(0, "drop-upload-footer-text")); |
| 56 | + }); |
| 57 | + cut.Contains("<div class=\"upload-drop-footer\">drop-upload-footer-text</div>"); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public async Task DropUpload_OnChanged_Ok() |
| 62 | + { |
| 63 | + var cut = Context.RenderComponent<DropUpload>(pb => |
| 64 | + { |
| 65 | + pb.Add(a => a.ShowLabel, true); |
| 66 | + pb.Add(a => a.ShowProgress, true); |
| 67 | + pb.Add(a => a.OnChange, async files => |
| 68 | + { |
| 69 | + await files.SaveToFileAsync("1.text"); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + var input = cut.FindComponent<InputFile>(); |
| 74 | + await cut.InvokeAsync(() => input.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs(new List<MockBrowserFile>() |
| 75 | + { |
| 76 | + new() |
| 77 | + }))); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public async Task ShowUploadList_Ok() |
| 82 | + { |
| 83 | + UploadFile? file = null; |
| 84 | + var cut = Context.RenderComponent<DropUpload>(pb => |
| 85 | + { |
| 86 | + pb.Add(a => a.ShowUploadFileList, true); |
| 87 | + pb.Add(a => a.ShowDownloadButton, true); |
| 88 | + pb.Add(a => a.OnDownload, f => |
| 89 | + { |
| 90 | + file = f; |
| 91 | + return Task.CompletedTask; |
| 92 | + }); |
| 93 | + pb.Add(a => a.DefaultFileList, |
| 94 | + [ |
| 95 | + new() { FileName = "Test-File1.text" }, |
| 96 | + new() { FileName = "Test-File2.jpg" }, |
| 97 | + ]); |
| 98 | + }); |
| 99 | + cut.Contains("upload-body is-list"); |
| 100 | + |
| 101 | + var button = cut.Find(".download-icon"); |
| 102 | + await cut.InvokeAsync(() => button.Click()); |
| 103 | + Assert.NotNull(file); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void ButtonUpload_OnGetFileFormat_Ok() |
| 108 | + { |
| 109 | + var cut = Context.RenderComponent<DropUpload>(pb => |
| 110 | + { |
| 111 | + pb.Add(a => a.LoadingIcon, "fa-loading"); |
| 112 | + pb.Add(a => a.DeleteIcon, "fa-delte"); |
| 113 | + pb.Add(a => a.CancelIcon, "fa-cancel"); |
| 114 | + pb.Add(a => a.DownloadIcon, "fa-download"); |
| 115 | + pb.Add(a => a.InvalidStatusIcon, "fa-invalid"); |
| 116 | + pb.Add(a => a.ValidStatusIcon, "fa-valid"); |
| 117 | + |
| 118 | + pb.Add(a => a.FileIconArchive, "fa-file-text"); |
| 119 | + pb.Add(a => a.FileIconExcel, "fa-file-excel"); |
| 120 | + pb.Add(a => a.FileIconFile, "fa-file"); |
| 121 | + pb.Add(a => a.FileIconDocx, "fa-file-word"); |
| 122 | + pb.Add(a => a.FileIconPPT, "fa-file-powerpoint"); |
| 123 | + pb.Add(a => a.FileIconAudio, "fa-file-audio"); |
| 124 | + pb.Add(a => a.FileIconVideo, "fa-file-video"); |
| 125 | + pb.Add(a => a.FileIconCode, "fa-file-code"); |
| 126 | + pb.Add(a => a.FileIconPdf, "fa-file-pdf"); |
| 127 | + pb.Add(a => a.FileIconImage, "fa-file-image"); |
| 128 | + pb.Add(a => a.FileIconZip, "fa-file-archive"); |
| 129 | + pb.Add(a => a.DefaultFileList, |
| 130 | + [ |
| 131 | + new() { FileName = "1.csv" }, |
| 132 | + new() { FileName = "1.xls" }, |
| 133 | + new() { FileName = "1.xlsx" }, |
| 134 | + new() { FileName = "1.doc" }, |
| 135 | + new() { FileName = "1.docx" }, |
| 136 | + new() { FileName = "1.dot" }, |
| 137 | + new() { FileName = "1.ppt" }, |
| 138 | + new() { FileName = "1.pptx" }, |
| 139 | + new() { FileName = "1.wav" }, |
| 140 | + new() { FileName = "1.mp3" }, |
| 141 | + new() { FileName = "1.mp4" }, |
| 142 | + new() { FileName = "1.mov" }, |
| 143 | + new() { FileName = "1.mkv" }, |
| 144 | + new() { FileName = "1.cs" }, |
| 145 | + new() { FileName = "1.html" }, |
| 146 | + new() { FileName = "1.vb" }, |
| 147 | + new() { FileName = "1.pdf" }, |
| 148 | + new() { FileName = "1.zip" }, |
| 149 | + new() { FileName = "1.rar" }, |
| 150 | + new() { FileName = "1.iso" }, |
| 151 | + new() { FileName = "1.txt" }, |
| 152 | + new() { FileName = "1.log" }, |
| 153 | + new() { FileName = "1.jpg" }, |
| 154 | + new() { FileName = "1.jpeg" }, |
| 155 | + new() { FileName = "1.png" }, |
| 156 | + new() { FileName = "1.bmp" }, |
| 157 | + new() { FileName = "1.gif" }, |
| 158 | + new() { FileName = "1.test" }, |
| 159 | + new() { FileName = "1" } |
| 160 | + ]); |
| 161 | + |
| 162 | + }); |
| 163 | + cut.Contains("fa-file-excel"); |
| 164 | + cut.Contains("fa-file-word"); |
| 165 | + cut.Contains("fa-file-powerpoint"); |
| 166 | + cut.Contains("fa-file-audio"); |
| 167 | + cut.Contains("fa-file-video"); |
| 168 | + cut.Contains("fa-file-code"); |
| 169 | + cut.Contains("fa-file-pdf"); |
| 170 | + cut.Contains("fa-file-archive"); |
| 171 | + cut.Contains("fa-file-text"); |
| 172 | + cut.Contains("fa-file-image"); |
| 173 | + cut.Contains("fa-file-archive"); |
| 174 | + cut.Contains("fa-file"); |
| 175 | + |
| 176 | + cut.SetParametersAndRender(pb => |
| 177 | + { |
| 178 | + pb.Add(a => a.OnGetFileFormat, extensions => |
| 179 | + { |
| 180 | + return "fa-format-test"; |
| 181 | + }); |
| 182 | + }); |
| 183 | + cut.Contains("fa-format-test"); |
| 184 | + } |
| 185 | + |
| 186 | + private class MockBrowserFile(string name = "UploadTestFile", string contentType = "text") : IBrowserFile |
| 187 | + { |
| 188 | + public string Name { get; } = name; |
| 189 | + |
| 190 | + public DateTimeOffset LastModified { get; } = DateTimeOffset.Now; |
| 191 | + |
| 192 | + public long Size { get; } = 10; |
| 193 | + |
| 194 | + public string ContentType { get; } = contentType; |
| 195 | + |
| 196 | + public Stream OpenReadStream(long maxAllowedSize = 512000, CancellationToken cancellationToken = default) |
| 197 | + { |
| 198 | + return new MemoryStream([0x01, 0x02]); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments