|
| 1 | +@code{ |
| 2 | + #if NET9_0_OR_GREATER |
| 3 | +} |
| 4 | +@using Bunit.TestAssets.Assets; |
| 5 | +@inherits BunitContext |
| 6 | +@code { |
| 7 | + [Fact(DisplayName = "Assets defaults to an empty collection where the indexer returns the key unchanged")] |
| 8 | + public void Test001() |
| 9 | + { |
| 10 | + var cut = Render<AssetsIndexerComponent>(); |
| 11 | + |
| 12 | + cut.MarkupMatches(@<img src="img.png" />); |
| 13 | + } |
| 14 | + |
| 15 | + [Fact(DisplayName = "Iterating Assets without any added assets renders nothing")] |
| 16 | + public void Test002() |
| 17 | + { |
| 18 | + var cut = Render<AssetsIterationComponent>(); |
| 19 | + |
| 20 | + cut.MarkupMatches(string.Empty); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact(DisplayName = "AddAsset with label maps the asset key to the fingerprinted url")] |
| 24 | + public void Test003() |
| 25 | + { |
| 26 | + AddAsset("img.abc123.png", label: "img.png"); |
| 27 | + |
| 28 | + var cut = Render<AssetsIndexerComponent>(); |
| 29 | + |
| 30 | + cut.MarkupMatches(@<img src="img.abc123.png" />); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact(DisplayName = "Components can iterate added assets and read their label property")] |
| 34 | + public void Test004() |
| 35 | + { |
| 36 | + AddAsset("css/app.abc123.css", label: "css/app.css"); |
| 37 | + AddAsset("js/app.def456.js", label: "js/app.js"); |
| 38 | + |
| 39 | + var cut = Render<AssetsIterationComponent>(); |
| 40 | + |
| 41 | + cut.MarkupMatches( |
| 42 | + @<text> |
| 43 | + <span>css/app.css</span> |
| 44 | + <span>js/app.js</span> |
| 45 | + </text>); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact(DisplayName = "AddAsset without label does not map the asset key")] |
| 49 | + public void Test005() |
| 50 | + { |
| 51 | + AddAsset("img.png"); |
| 52 | + |
| 53 | + var cut = Render<AssetsIndexerComponent>(); |
| 54 | + |
| 55 | + cut.MarkupMatches(@<img src="img.png" />); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact(DisplayName = "AddAsset exposes additional properties to components")] |
| 59 | + public void Test006() |
| 60 | + { |
| 61 | + AddAsset("js/app.js", label: "app.js", new ResourceAssetProperty("integrity", "sha256-abc")); |
| 62 | + |
| 63 | + var cut = Render<AssetsPropertiesComponent>(); |
| 64 | + |
| 65 | + cut.MarkupMatches( |
| 66 | + @<ul data-url="js/app.js"> |
| 67 | + <li>label=app.js</li> |
| 68 | + <li>integrity=sha256-abc</li> |
| 69 | + </ul>); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact(DisplayName = "Assets added after the initial render are available to components rendered afterwards")] |
| 73 | + public void Test007() |
| 74 | + { |
| 75 | + var first = Render<AssetsIterationComponent>(); |
| 76 | + first.MarkupMatches(string.Empty); |
| 77 | + |
| 78 | + AddAsset("img.abc123.png", label: "img.png"); |
| 79 | + |
| 80 | + var second = Render<AssetsIterationComponent>(); |
| 81 | + second.MarkupMatches(@<span>img.png</span>); |
| 82 | + } |
| 83 | + |
| 84 | + [Theory(DisplayName = "AddAsset rejects empty or whitespace-only labels")] |
| 85 | + [InlineData("")] |
| 86 | + [InlineData(" ")] |
| 87 | + [InlineData("\t")] |
| 88 | + public void Test008(string label) |
| 89 | + { |
| 90 | + var ex = Assert.Throws<ArgumentException>(() => AddAsset("app.js", label: label)); |
| 91 | + |
| 92 | + ex.ParamName.ShouldBe("label"); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact(DisplayName = "AddAsset rejects explicit label properties when label is provided")] |
| 96 | + public void Test009() |
| 97 | + { |
| 98 | + var ex = Assert.Throws<ArgumentException>( |
| 99 | + () => AddAsset("app.js", label: "app.js", new ResourceAssetProperty("label", "other"))); |
| 100 | + |
| 101 | + ex.ParamName.ShouldBe("properties"); |
| 102 | + } |
| 103 | +} |
| 104 | +@code{ |
| 105 | + #endif |
| 106 | +} |
0 commit comments