-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* allow prerendering using the PrerenderAttribute * code format * tests * document PrerenderAttribute
- Loading branch information
1 parent
faf98e0
commit 64b737c
Showing
4 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Ignis.Components; | ||
|
||
[AttributeUsage(AttributeTargets.Class)] | ||
public sealed class PrerenderAttribute : Attribute | ||
{ | ||
} |
59 changes: 59 additions & 0 deletions
59
tests/Ignis.Tests.Components/PrerenderComponentTests.razor
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
@using Ignis.Components.Extensions | ||
@using Microsoft.AspNetCore.Components.Rendering | ||
@inherits TestContext | ||
|
||
@code | ||
{ | ||
[Fact] | ||
public void Cycle() | ||
{ | ||
Services.AddIgnis(); | ||
|
||
var context = new PrerenderHostContext(); | ||
Services.AddSingleton<IHostContext>(context); | ||
|
||
context.IsPrerenderingValue = true; | ||
|
||
var cut = RenderComponent<PrerenderComponent>(); | ||
|
||
var result = cut.Markup; | ||
Assert.Equal("OnPrerender", result); | ||
|
||
context.IsPrerenderingValue = false; | ||
|
||
cut.Render(); | ||
|
||
result = cut.Markup; | ||
|
||
Assert.Equal("OnInitialized", result); | ||
} | ||
|
||
[Prerender] | ||
class PrerenderComponent : IgnisComponentBase | ||
{ | ||
private string _message = "OnPrerender"; | ||
|
||
protected override void OnInitialized() | ||
{ | ||
_message = "OnInitialized"; | ||
} | ||
|
||
protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
{ | ||
builder.AddContent(0, _message); | ||
} | ||
} | ||
|
||
class PrerenderHostContext : HostContextBase | ||
{ | ||
public bool IsPrerenderingValue { get; set; } | ||
|
||
public override bool IsPrerendering => IsPrerenderingValue; | ||
|
||
public override bool IsServerSide => false; | ||
|
||
public PrerenderHostContext() : base(Array.Empty<IComponentExtension>()) | ||
{ | ||
} | ||
} | ||
} |