Skip to content

Commit dcf92a4

Browse files
authored
docs: Added Limitation for UploadFiles
1 parent fa26750 commit dcf92a4

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

docs/site/docs/test-doubles/input-file.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,45 @@ inputFile.UploadFile(fileToUpload);
2525
// Assertions...
2626
```
2727

28-
To upload binary content, create an `InputFileContent` with the `InputFileContent.CreateFromBinary()` method.
28+
To upload binary content, create an `InputFileContent` with the `InputFileContent.CreateFromBinary()` method.
29+
30+
## Known limitations
31+
bUnit's support for the `InputFile` component is limited when uploading and resizing images (using the provided stream).
32+
33+
```razor
34+
<InputFile OnChange="Upload" />
35+
<img src="@imageBase64">
36+
37+
@code {
38+
private string imageBase64 = string.Empty;
39+
40+
private async Task Upload(InputFileChangeEventArgs args)
41+
{
42+
var file = args.File;
43+
var preview = await file.RequestImageFileAsync("image/png", 100, 100);
44+
await using var stream = preview.OpenReadStream();
45+
var buffer = new byte[stream.Length];
46+
await using var memoryStream = new MemoryStream(buffer);
47+
await stream.CopyToAsync(memoryStream);
48+
var base64 = Convert.ToBase64String(buffer);
49+
imageBase64 = $"data:image/png;base64,{base64}";
50+
}
51+
}
52+
```
53+
54+
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:
55+
56+
```csharp
57+
[Fact]
58+
public void UploadFileTest()
59+
{
60+
var cut = Render<ComponentThatUsesInputFile>();
61+
62+
cut.FindComponent<InputFile>().UploadFiles(InputFileContent.CreateFromBinary([1,2], "test.png"));
63+
64+
cut.Find("img").GetAttribute("src").Should().NotBeNullOrEmpty(); // Will fail
65+
Renderer.UnhandledException.Should().BeNull(); // Will fail
66+
}
67+
```
68+
69+
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.

0 commit comments

Comments
 (0)