From dcf92a49cfe392d367a8fafc718de615bb61a5e9 Mon Sep 17 00:00:00 2001 From: Steven Giesel Date: Mon, 19 Aug 2024 17:42:52 +0000 Subject: [PATCH] docs: Added Limitation for UploadFiles --- docs/site/docs/test-doubles/input-file.md | 43 ++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/site/docs/test-doubles/input-file.md b/docs/site/docs/test-doubles/input-file.md index bae083b5d..c34442d0f 100644 --- a/docs/site/docs/test-doubles/input-file.md +++ b/docs/site/docs/test-doubles/input-file.md @@ -25,4 +25,45 @@ inputFile.UploadFile(fileToUpload); // Assertions... ``` -To upload binary content, create an `InputFileContent` with the `InputFileContent.CreateFromBinary()` method. \ No newline at end of file +To upload binary content, create an `InputFileContent` with the `InputFileContent.CreateFromBinary()` method. + +## Known limitations +bUnit's support for the `InputFile` component is limited when uploading and resizing images (using the provided stream). + +```razor + + + +@code { + private string imageBase64 = string.Empty; + + private async Task Upload(InputFileChangeEventArgs args) + { + var file = args.File; + var preview = await file.RequestImageFileAsync("image/png", 100, 100); + await using var stream = preview.OpenReadStream(); + var buffer = new byte[stream.Length]; + await using var memoryStream = new MemoryStream(buffer); + await stream.CopyToAsync(memoryStream); + var base64 = Convert.ToBase64String(buffer); + imageBase64 = $"data:image/png;base64,{base64}"; + } +} +``` + +When using the `RequestImageFileAsync` method, the `UploadFiles` method will not be able to upload the file inside a test. Blazor has some internal checks, bUnit can not overcome easily. So the following test will fail: + +```csharp +[Fact] +public void UploadFileTest() +{ + var cut = Render(); + + cut.FindComponent().UploadFiles(InputFileContent.CreateFromBinary([1,2], "test.png")); + + cut.Find("img").GetAttribute("src").Should().NotBeNullOrEmpty(); // Will fail + Renderer.UnhandledException.Should().BeNull(); // Will fail +} +``` + +To work around this limitation, refactoring the logic into a service that is injected into the component and then mocking the service in the test is a possible solution. \ No newline at end of file