Skip to content

feat: Add Asset handling#1847

Open
linkdotnet wants to merge 1 commit into
mainfrom
feat/#1846
Open

feat: Add Asset handling#1847
linkdotnet wants to merge 1 commit into
mainfrom
feat/#1846

Conversation

@linkdotnet
Copy link
Copy Markdown
Collaborator

This fixes #1846

The basic idea is to have a convenient AddAsset method that passes a collection (just like AddParameter) to the Renderer - ComponentBase then can "grab" those.

This allow emulation of "@assets"

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.AddAsset and renderer-side asset storage to expose a ResourceAssetCollection through ComponentBase.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 thread src/bunit/BunitContext.cs
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for Assets

2 participants