feat: Add Asset handling#1847
Open
linkdotnet wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds support for seeding .NET 9+ ComponentBase.Assets in bUnit via a new AddAsset API, enabling tests to emulate @Assets/MapStaticAssets behavior.
Changes:
- Introduces
BunitContext.AddAssetand renderer-side asset storage to expose aResourceAssetCollectionthroughComponentBase.Assets. - Adds new test assets/components and a new test suite covering indexer behavior, iteration, and custom properties.
- Documents the feature (“Seeding static assets”) and links it from the docs TOC and changelog.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/bunit.tests/Rendering/AssetsTest.razor | New tests validating default/seeded Assets behavior via AddAsset. |
| tests/bunit.testassets/Assets/AssetsPropertiesComponent.razor | Test component for iterating assets and rendering properties. |
| tests/bunit.testassets/Assets/AssetsIterationComponent.razor | Test component for reading label property from assets and rendering labels. |
| tests/bunit.testassets/Assets/AssetsIndexerComponent.razor | Test component validating Assets["key"] indexer mapping. |
| src/bunit/Rendering/BunitRenderer.cs | Adds backing store + override for Assets and implements AddAsset. |
| src/bunit/BunitContext.cs | Exposes AddAsset on BunitContext delegating to the renderer. |
| docs/site/docs/toc.md | Adds link to new “Seeding static assets” documentation. |
| docs/site/docs/providing-input/seeding-assets.md | New documentation explaining how to seed Assets in tests. |
| CHANGELOG.md | Notes addition of AddAsset in “Added”. |
Comment on lines
+1
to
+6
| @code{ | ||
| #if NET9_0_OR_GREATER | ||
| } | ||
| @using Bunit.TestAssets.Assets; | ||
| @inherits BunitContext | ||
| @code { |
Comment on lines
+84
to
+86
| @code{ | ||
| #endif | ||
| } |
Comment on lines
+104
to
+115
| public void AddAsset(string url, string? label = null, params ResourceAssetProperty[] properties) | ||
| { | ||
| ArgumentException.ThrowIfNullOrEmpty(url); | ||
| ArgumentNullException.ThrowIfNull(properties); | ||
|
|
||
| var props = new List<ResourceAssetProperty>(properties.Length + 1); | ||
| if (label is not null) | ||
| { | ||
| props.Add(new ResourceAssetProperty("label", label)); | ||
| } | ||
|
|
||
| props.AddRange(properties); |
Comment on lines
+16
to
+19
| => SubresourceNames = Assets | ||
| .Select(asset => asset.Properties?.SingleOrDefault(property => property.Name == "label")?.Value) | ||
| .OfType<string>() | ||
| .ToList(); |
Comment on lines
+192
to
+195
| /// <remarks> | ||
| /// Pass a <paramref name="label"/> to map a stable asset key to its (fingerprinted) <paramref name="url"/>, | ||
| /// i.e. <c>AddAsset("img.abc123.png", label: "img.png")</c> makes <c>Assets["img.png"]</c> return <c>img.abc123.png</c>. | ||
| /// </remarks> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes #1846
The basic idea is to have a convenient
AddAssetmethod that passes a collection (just likeAddParameter) to the Renderer -ComponentBasethen can "grab" those.This allow emulation of "@assets"