|
| 1 | +#if NET5_0_OR_GREATER |
| 2 | +using System; |
| 3 | +using Bunit.TestAssets.SampleComponents; |
| 4 | +using Moq; |
| 5 | +using Shouldly; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Bunit.ComponentFactories |
| 9 | +{ |
| 10 | + public class InstanceComponentFactoryTest : TestContext |
| 11 | + { |
| 12 | + [Fact(DisplayName = "Add throws when factories is null")] |
| 13 | + public void Test001() |
| 14 | + => Should.Throw<ArgumentNullException>(() => ComponentFactoryCollectionExtensions.Add<Simple1>(default, default)); |
| 15 | + |
| 16 | + [Fact(DisplayName = "Add throws when instance is null")] |
| 17 | + public void Test002() |
| 18 | + => Should.Throw<ArgumentNullException>(() => ComponentFactories.Add<Simple1>(default)); |
| 19 | + |
| 20 | + [Fact(DisplayName = "Factory replaces one TComponent with instance in the render tree")] |
| 21 | + public void Test010() |
| 22 | + { |
| 23 | + var simple1Mock = new Mock<Simple1>(); |
| 24 | + ComponentFactories.Add<Simple1>(simple1Mock.Object); |
| 25 | + |
| 26 | + var cut = RenderComponent<Wrapper>(ps => ps.AddChildContent<Simple1>()); |
| 27 | + |
| 28 | + cut.FindComponent<Simple1>().Instance |
| 29 | + .ShouldBeSameAs(simple1Mock.Object); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact(DisplayName = "Factory throws if component instance is requested twice for TComponent that inherits from ComponentBase")] |
| 33 | + public void Test020() |
| 34 | + { |
| 35 | + var simple1Mock = new Mock<Simple1>(); |
| 36 | + ComponentFactories.Add<Simple1>(simple1Mock.Object); |
| 37 | + |
| 38 | + Should.Throw<InvalidOperationException>(() => RenderComponent<TwoComponentWrapper>(ps => ps |
| 39 | + .Add<Simple1>(p => p.First) |
| 40 | + .Add<Simple1>(p => p.Second))); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact(DisplayName = "Factory throws if component instance is requested twice for TComponent that implements from IComponent")] |
| 44 | + public void Test021() |
| 45 | + { |
| 46 | + var simple1Mock = new Mock<BasicComponent>(); |
| 47 | + ComponentFactories.Add<BasicComponent>(simple1Mock.Object); |
| 48 | + |
| 49 | + Should.Throw<InvalidOperationException>(() => RenderComponent<TwoComponentWrapper>(ps => ps |
| 50 | + .Add<BasicComponent>(p => p.First) |
| 51 | + .Add<BasicComponent>(p => p.Second))); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +#endif |
0 commit comments