123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- using Microsoft.AspNetCore.StaticWebAssets.Tasks;
- using Microsoft.Build.Framework;
- using Microsoft.Build.Utilities;
- using Moq;
- namespace Microsoft.NET.Sdk.Razor.Tests
- {
- public class ApplyAllCssScopesTest
- {
- [Fact]
- public void ApplyAllCssScopes_AppliesScopesToRazorComponentFiles()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes()
- {
- RazorComponents = new[]
- {
- new TaskItem("TestFiles/Pages/Counter.razor"),
- new TaskItem("TestFiles/Pages/Index.razor"),
- },
- RazorGenerate = Array.Empty<ITaskItem>(),
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Index.razor.css", new Dictionary<string, string> { ["CssScope"] = "index-scope" }),
- new TaskItem("TestFiles/Pages/Counter.razor.css", new Dictionary<string, string> { ["CssScope"] = "counter-scope" }),
- }
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- result.Should().BeTrue();
- taskInstance.RazorComponentsWithScopes.Should().HaveCount(2);
- taskInstance.RazorComponentsWithScopes.Should().ContainSingle(rcws => rcws.ItemSpec == "TestFiles/Pages/Index.razor" && rcws.GetMetadata("CssScope") == "index-scope");
- taskInstance.RazorComponentsWithScopes.Should().ContainSingle(rcws => rcws.ItemSpec == "TestFiles/Pages/Counter.razor" && rcws.GetMetadata("CssScope") == "counter-scope");
- }
- [Fact]
- public void ApplyAllCssScopes_AppliesScopesToRazorViewFiles()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes()
- {
- RazorGenerate = new[]
- {
- new TaskItem("TestFiles/Pages/Counter.cshtml"),
- new TaskItem("TestFiles/Pages/Index.cshtml"),
- },
- RazorComponents = Array.Empty<ITaskItem>(),
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Index.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "index-scope" }),
- new TaskItem("TestFiles/Pages/Counter.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "counter-scope" }),
- }
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- result.Should().BeTrue();
- taskInstance.RazorGenerateWithScopes.Should().HaveCount(2);
- taskInstance.RazorGenerateWithScopes.Should().ContainSingle(rcws => rcws.ItemSpec == "TestFiles/Pages/Index.cshtml" && rcws.GetMetadata("CssScope") == "index-scope");
- taskInstance.RazorGenerateWithScopes.Should().ContainSingle(rcws => rcws.ItemSpec == "TestFiles/Pages/Counter.cshtml" && rcws.GetMetadata("CssScope") == "counter-scope");
- }
- [Fact]
- public void DoesNotApplyCssScopes_ToRazorComponentsWithoutAssociatedFiles()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes()
- {
- RazorComponents = new[]
- {
- new TaskItem("TestFiles/Pages/Counter.razor"),
- new TaskItem("TestFiles/Pages/Index.razor"),
- new TaskItem("TestFiles/Pages/FetchData.razor"),
- },
- RazorGenerate = Array.Empty<ITaskItem>(),
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Index.razor.css", new Dictionary<string, string> { ["CssScope"] = "index-scope" }),
- new TaskItem("TestFiles/Pages/Counter.razor.css", new Dictionary<string, string> { ["CssScope"] = "counter-scope" })
- }
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- Assert.True(result);
- result.Should().BeTrue();
- taskInstance.RazorComponentsWithScopes.Should().NotContain(rcws => rcws.ItemSpec == "TestFiles/Pages/Fetchdata.razor");
- }
- [Fact]
- public void DoesNotApplyCssScopes_ToRazorViewsWithoutAssociatedFiles()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes()
- {
- RazorGenerate = new[]
- {
- new TaskItem("TestFiles/Pages/Counter.cshtml"),
- new TaskItem("TestFiles/Pages/Index.cshtml"),
- new TaskItem("TestFiles/Pages/FetchData.cshtml"),
- },
- RazorComponents = Array.Empty<ITaskItem>(),
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Index.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "index-scope" }),
- new TaskItem("TestFiles/Pages/Counter.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "counter-scope" })
- }
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- Assert.True(result);
- result.Should().BeTrue();
- taskInstance.RazorGenerateWithScopes.Should().NotContain(rcws => rcws.ItemSpec == "TestFiles/Pages/Fetchdata.razor");
- }
- [Fact]
- public void ApplyAllCssScopes_FailsWhenTheScopedCss_DoesNotMatchTheRazorComponent()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes
- {
- RazorComponents = new[]
- {
- new TaskItem("TestFiles/Pages/Counter.razor"),
- new TaskItem("TestFiles/Pages/Index.razor"),
- },
- RazorGenerate = Array.Empty<ITaskItem>(),
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Index.razor.css", new Dictionary<string, string> { ["CssScope"] = "index-scope" }),
- new TaskItem("TestFiles/Pages/Counter.razor.css", new Dictionary<string, string> { ["CssScope"] = "counter-scope" }),
- new TaskItem("TestFiles/Pages/Profile.razor.css", new Dictionary<string, string> { ["CssScope"] = "profile-scope" }),
- },
- BuildEngine = Mock.Of<IBuildEngine>()
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- result.Should().BeFalse();
- }
- [Fact]
- public void ApplyAllCssScopes_FailsWhenTheScopedCss_DoesNotMatchTheRazorView()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes
- {
- RazorGenerate = new[]
- {
- new TaskItem("TestFiles/Pages/Counter.cshtml"),
- new TaskItem("TestFiles/Pages/Index.cshtml"),
- },
- RazorComponents = Array.Empty<ITaskItem>(),
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Index.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "index-scope" }),
- new TaskItem("TestFiles/Pages/Counter.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "counter-scope" }),
- new TaskItem("TestFiles/Pages/Profile.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "profile-scope" }),
- },
- BuildEngine = Mock.Of<IBuildEngine>()
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- result.Should().BeFalse();
- }
- [Fact]
- public void ScopedCssCanDefineAssociatedRazorComponentFile()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes()
- {
- RazorComponents = new[]
- {
- new TaskItem("TestFiles/Pages/FetchData.razor")
- },
- RazorGenerate = Array.Empty<ITaskItem>(),
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Profile.razor.css", new Dictionary<string, string>
- {
- ["CssScope"] = "fetchdata-scope",
- ["RazorComponent"] = "TestFiles/Pages/FetchData.razor"
- })
- }
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- result.Should().BeTrue();
- taskInstance.RazorComponentsWithScopes.Should().ContainSingle(rcws => rcws.ItemSpec == "TestFiles/Pages/FetchData.razor" && rcws.GetMetadata("CssScope") == "fetchdata-scope");
- }
- [Fact]
- public void ScopedCssCanDefineAssociatedRazorGenerateFile()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes()
- {
- RazorGenerate = new[]
- {
- new TaskItem("TestFiles/Pages/FetchData.cshtml")
- },
- RazorComponents = Array.Empty<ITaskItem>(),
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Profile.cshtml.css", new Dictionary<string, string>
- {
- ["CssScope"] = "fetchdata-scope",
- ["View"] = "TestFiles/Pages/FetchData.cshtml"
- })
- }
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- result.Should().BeTrue();
- taskInstance.RazorGenerateWithScopes.Should().ContainSingle(rcws => rcws.ItemSpec == "TestFiles/Pages/FetchData.cshtml" && rcws.GetMetadata("CssScope") == "fetchdata-scope");
- }
- [Fact]
- public void ApplyAllCssScopes_FailsWhenMultipleScopedCssFiles_MatchTheSameRazorComponent()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes
- {
- RazorComponents = new[]
- {
- new TaskItem("TestFiles/Pages/Counter.razor"),
- new TaskItem("TestFiles/Pages/Index.razor"),
- },
- RazorGenerate = Array.Empty<ITaskItem>(),
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Index.razor.css", new Dictionary<string, string> { ["CssScope"] = "index-scope" }),
- new TaskItem("TestFiles/Pages/Counter.razor.css", new Dictionary<string, string> { ["CssScope"] = "counter-scope" }),
- new TaskItem("TestFiles/Pages/Profile.razor.css", new Dictionary<string, string>
- {
- ["CssScope"] = "conflict-scope",
- ["RazorComponent"] = "TestFiles/Pages/Index.razor"
- }),
- },
- BuildEngine = Mock.Of<IBuildEngine>()
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- result.Should().BeFalse();
- }
- [Fact]
- public void ApplyAllCssScopes_FailsWhenMultipleScopedCssFiles_MatchTheSameRazorView()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes
- {
- RazorGenerate = new[]
- {
- new TaskItem("TestFiles/Pages/Counter.cshtml"),
- new TaskItem("TestFiles/Pages/Index.cshtml"),
- },
- RazorComponents = Array.Empty<ITaskItem>(),
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Index.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "index-scope" }),
- new TaskItem("TestFiles/Pages/Counter.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "counter-scope" }),
- new TaskItem("TestFiles/Pages/Profile.cshtml.css", new Dictionary<string, string>
- {
- ["CssScope"] = "conflict-scope",
- ["View"] = "TestFiles/Pages/Index.cshtml"
- }),
- },
- BuildEngine = Mock.Of<IBuildEngine>()
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- result.Should().BeFalse();
- }
- [Fact]
- public void ApplyAllCssScopes_AppliesScopesToRazorComponentAndViewFiles()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes()
- {
- RazorComponents = new[]
- {
- new TaskItem("TestFiles/Pages/Counter.razor"),
- new TaskItem("TestFiles/Pages/Index.razor"),
- },
- RazorGenerate = new[]
- {
- new TaskItem("TestFiles/Pages/Home.cshtml"),
- new TaskItem("TestFiles/Pages/_Host.cshtml"),
- },
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Home.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "home-scope" }),
- new TaskItem("TestFiles/Pages/_Host.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "_host-scope" }),
- new TaskItem("TestFiles/Pages/Index.razor.css", new Dictionary<string, string> { ["CssScope"] = "index-scope" }),
- new TaskItem("TestFiles/Pages/Counter.razor.css", new Dictionary<string, string> { ["CssScope"] = "counter-scope" }),
- }
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- result.Should().BeTrue();
- taskInstance.RazorComponentsWithScopes.Should().HaveCount(2);
- taskInstance.RazorComponentsWithScopes.Should().ContainSingle(rcws => rcws.ItemSpec == "TestFiles/Pages/Index.razor" && rcws.GetMetadata("CssScope") == "index-scope");
- taskInstance.RazorComponentsWithScopes.Should().ContainSingle(rcws => rcws.ItemSpec == "TestFiles/Pages/Counter.razor" && rcws.GetMetadata("CssScope") == "counter-scope");
- taskInstance.RazorGenerateWithScopes.Should().HaveCount(2);
- taskInstance.RazorGenerateWithScopes.Should().ContainSingle(rcws => rcws.ItemSpec == "TestFiles/Pages/Home.cshtml" && rcws.GetMetadata("CssScope") == "home-scope");
- taskInstance.RazorGenerateWithScopes.Should().ContainSingle(rcws => rcws.ItemSpec == "TestFiles/Pages/_Host.cshtml" && rcws.GetMetadata("CssScope") == "_host-scope");
- }
- [Fact]
- public void ApplyAllCssScopes_ScopedCssComponentsDontMatchWithScopedCssViewStylesAndViceversa()
- {
- // Arrange
- var taskInstance = new ApplyCssScopes
- {
- RazorComponents = new[]
- {
- new TaskItem("TestFiles/Pages/Counter.razor"),
- new TaskItem("TestFiles/Pages/Index.razor"),
- },
- RazorGenerate = new[]
- {
- new TaskItem("TestFiles/Pages/Home.cshtml"),
- new TaskItem("TestFiles/Pages/_Host.cshtml"),
- },
- ScopedCss = new[]
- {
- new TaskItem("TestFiles/Pages/Home.razor.css", new Dictionary<string, string> { ["CssScope"] = "home-scope" }),
- new TaskItem("TestFiles/Pages/_Host.razor.css", new Dictionary<string, string> { ["CssScope"] = "_host-scope" }),
- new TaskItem("TestFiles/Pages/Index.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "index-scope" }),
- new TaskItem("TestFiles/Pages/Counter.cshtml.css", new Dictionary<string, string> { ["CssScope"] = "counter-scope" }),
- },
- BuildEngine = Mock.Of<IBuildEngine>()
- };
- // Act
- var result = taskInstance.Execute();
- // Assert
- result.Should().BeFalse();
- }
- }
- }
|