Testing components that uses the <InputFile> component #415
egil
started this conversation in
Show and tell
Replies: 2 comments 4 replies
-
can you please let me know What will be inside OpenReadStream? |
Beta Was this translation helpful? Give feedback.
3 replies
-
Hi @egil ! I know this is an old thread, but may I suggest the documentation is updated to include this example? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Here is a quick guide for writing tests of components that uses the
<InputFile>
component.The
<FileUpload1>
component, our component under test in this example, heavily inspired by the one listed in the asp.net core docs for the<InputFile>
component, looks like this:This
<FileUpload1>
component doesn't process the uploaded files itself directly, since we do not want to write tests that e.g. upload files to Azure Blob Storage or save them to the file system. That is pretty likely to not be good test experience. Instead, the component is passed aIBrowserFileProcessor
(listed below) that we can mock during testing and have a proper implementation of in production. As always, keep your concerns separated!Testing this component now looks like this:
There are two tests. The first test just verifies that we are able to render the
<InputFile>
component. To do so, we have to provide aIOptions<RemoteBrowserFileStreamOptions>
to it, as well as set up a simple JSInterop handler for its "init" call.Then, in our individual tests, we pass a mock of the
IBrowserFileProcessor
services.In the second test, we actually emulate a user uploading files through the
<InputFile>
component. This is done simply by calling theOnChange
event callback that is passed to the component. That event callback is accessed by first finding the<InputFile>
component in the render tree, and then invoking the event callback. This has to happen on the renderers thread, so we wrap the call in acut.InvokeAsync
call.The .csproj used to build the files and run the tests looks like this:
NOTE, while this uses NET6, it should work the same with NET5.
Hope this helps.
Related: #318 and #317.
Beta Was this translation helpful? Give feedback.
All reactions