Skip to content

Commit

Permalink
tests & reactivity system validation
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVollmers committed Oct 5, 2023
1 parent 5767baf commit c9b15ad
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/Ignis.Components.Reactivity/ReactiveValue.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
namespace Ignis.Components.Reactivity;
using Ignis.Components.Extensions;

namespace Ignis.Components.Reactivity;

public sealed class ReactiveValue<T> where T : struct
{
private readonly IList<ReactiveSection<T>> _sections = new List<ReactiveSection<T>>();
private readonly IgnisComponentBase _owner;

private IgnisReactivityExtension? _extension;
private T _value;

public ReactiveValue(IgnisComponentBase owner, T value)
Expand All @@ -17,6 +20,8 @@ public T Value
{
get
{
Initialize();

return _value;
}
set
Expand All @@ -34,6 +39,18 @@ public T Value
}
}

private void Initialize()
{
if (_extension != null) return;

_extension = _owner.GetExtension<IgnisReactivityExtension>();
if (_extension == null)
{
throw new InvalidOperationException(
"To use the Ignis reactivity system please make sure to call AddIgnisReactivity() in your Program.cs.");
}
}

public void SetSilently(T value)
{
_value = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public void Test()
{
Services.AddIgnisTestServices();
Services.AddIgnisReactivity();

var cut = RenderComponent<AlternatingCounter>();

Expand Down
11 changes: 11 additions & 0 deletions tests/Ignis.Tests.Components.Reactivity/ReactiveValueTests.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

@code
{
[Fact]
public void MissingCallToAddIgnisReactivity()
{
Services.AddIgnisTestServices();

var exception = Assert.Throws<InvalidOperationException>(() => RenderComponent<SilentCounter>());
Assert.Equal("To use the Ignis reactivity system please make sure to call AddIgnisReactivity() in your Program.cs.", exception.Message);
}

[Fact]
public void SilentCounter()
{
Services.AddIgnisTestServices();
Services.AddIgnisReactivity();

var cut = RenderComponent<SilentCounter>();
Assert.Equal(1, cut.RenderCount);
Expand All @@ -25,6 +35,7 @@
public void ReactiveCounter()
{
Services.AddIgnisTestServices();
Services.AddIgnisReactivity();

var cut = RenderComponent<ReactiveCounter>();
Assert.Equal(1, cut.RenderCount);
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/Ignis.Tests.E2E.Website/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Ignis.Components;
using Ignis.Components.Reactivity;
using Ignis.Components.WebAssembly;
using Ignis.Tests.E2E.Website;
using Microsoft.AspNetCore.Components.Web;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
<ValidationMessage For="@(() => _model.SelectedType)"></ValidationMessage>
</p>
</div>

@if (_model.SelectedType != null)
{
<p>
@_model.SelectedType.Name
</p>
}
</EditForm>
</ChildContent>
<FooterContent>
Expand Down

0 comments on commit c9b15ad

Please sign in to comment.