Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion docs/site/docs/test-doubles/input-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,45 @@ inputFile.UploadFile(fileToUpload);
// Assertions...
```

To upload binary content, create an `InputFileContent` with the `InputFileContent.CreateFromBinary()` method.
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
<InputFile OnChange="Upload" />
<img src="@imageBase64">

@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<ComponentThatUsesInputFile>();

cut.FindComponent<InputFile>().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.