Skip to content

Commit 600285a

Browse files
committed
better docs, tweaked itestcontext by removing unneeded method.
1 parent 0095cd8 commit 600285a

File tree

8 files changed

+116
-29
lines changed

8 files changed

+116
-29
lines changed

.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
[*.cs]
22

33
# CA1822: Mark members as static
4-
dotnet_diagnostic.CA1822.severity = suggestion
4+
dotnet_diagnostic.CA1822.severity = suggestion
5+
6+
# CA1032: Implement standard exception constructors
7+
dotnet_diagnostic.CA1032.severity = suggestion

src/ClassDiagram.cd

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ClassDiagram MajorVersion="1" MinorVersion="1" MembersFormat="FullSignature">
3+
<Comment CommentText="IRazorTestContext is used in Razor-based tests.">
4+
<Position X="3.132" Y="3.984" Height="0.458" Width="1.7" />
5+
</Comment>
6+
<Comment CommentText="ITestContext is used in C#-based tests.">
7+
<Position X="2.656" Y="0.995" Height="0.458" Width="1.7" />
8+
</Comment>
9+
<Interface Name="Egil.RazorComponents.Testing.ITestContext">
10+
<Position X="2.5" Y="1.5" Width="6.5" />
11+
<TypeIdentifier>
12+
<HashCode>gAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAACACAEAAAA=</HashCode>
13+
<FileName>ITestContext.cs</FileName>
14+
</TypeIdentifier>
15+
</Interface>
16+
<Interface Name="Egil.RazorComponents.Testing.IRenderedFragment">
17+
<Position X="3.5" Y="7" Width="3.25" />
18+
<TypeIdentifier>
19+
<HashCode>CAgAAAAAAAAAAAAAABAQACAAAAAIAAAAAAAAAQQAAAA=</HashCode>
20+
<FileName>Rendering\IRenderedFragment.cs</FileName>
21+
</TypeIdentifier>
22+
</Interface>
23+
<Interface Name="Egil.RazorComponents.Testing.IRenderedComponent&lt;TComponent&gt;">
24+
<Position X="2.75" Y="10.25" Width="4.75" />
25+
<TypeIdentifier>
26+
<HashCode>AEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQBAAAAAAAAA=</HashCode>
27+
<FileName>Rendering\IRenderedComponent.cs</FileName>
28+
</TypeIdentifier>
29+
</Interface>
30+
<Interface Name="Egil.RazorComponents.Testing.IRazorTestContext">
31+
<Position X="3" Y="4.5" Width="5.5" />
32+
<TypeIdentifier>
33+
<HashCode>QAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=</HashCode>
34+
<FileName>IRazorTestContext.cs</FileName>
35+
</TypeIdentifier>
36+
</Interface>
37+
<Font Name="Segoe UI" Size="9" />
38+
</ClassDiagram>

src/ElementNotFoundException.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.Serialization;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Egil.RazorComponents.Testing
9+
{
10+
/// <summary>
11+
/// Represents a failure to find an element in the searched target
12+
/// using a css selector.
13+
/// </summary>
14+
public class ElementNotFoundException : Exception
15+
{
16+
/// <summary>
17+
/// The css selector used to search with.
18+
/// </summary>
19+
public string CssSelector { get; }
20+
21+
/// <inheritdoc/>
22+
public ElementNotFoundException(string cssSelector) : base($"No elements were found that matches the selector '{cssSelector}'")
23+
{
24+
CssSelector = cssSelector;
25+
}
26+
}
27+
}

src/ITestContext.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ public interface ITestContext
3333
/// <returns>The rendered <typeparamref name="TComponent"/></returns>
3434
IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) where TComponent : class, IComponent;
3535

36-
/// <summary>
37-
/// Instantiates and performs a first render of a component of type <typeparamref name="TComponent"/>.
38-
/// </summary>
39-
/// <typeparam name="TComponent">Type of the component to render</typeparam>
40-
/// <param name="childContent">Child content to pass to the component when it is rendered</param>
41-
/// <param name="parameters">Parameters to pass to the component when it is rendered</param>
42-
/// <returns>The rendered <typeparamref name="TComponent"/></returns>
43-
IRenderedComponent<TComponent> RenderComponent<TComponent>(RenderFragment childContent, params ComponentParameter[] parameters) where TComponent : class, IComponent;
44-
4536
/// <summary>
4637
/// Executes the provided <paramref name="renderTrigger"/> action and waits for a render to occur.
4738
/// Use this when you have a component that is awaiting e.g. a service to return data to it before rendering again.

src/Rendering/IRenderedFragment.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface IRenderedFragment
1515
/// Gets the <see cref="ITestContext"/> which this rendered fragment belongs to.
1616
/// </summary>
1717
ITestContext TestContext { get; }
18-
18+
1919
/// <summary>
2020
/// Gets the HTML markup from the rendered fragment/component.
2121
/// </summary>
@@ -53,21 +53,29 @@ public interface IRenderedFragment
5353
void TakeSnapshot();
5454

5555
/// <summary>
56-
/// Returns the first element within the rendered fragment or component under test
57-
/// (using depth-first pre-order traversal of the document's nodes) that matches the
58-
/// specified group of selectors.
56+
/// Returns the first element from the rendered fragment or component under test,
57+
/// using the provided <paramref name="cssSelector"/>, in a depth-first pre-order traversal
58+
/// of the rendered nodes.
5959
/// </summary>
60-
/// <param name="selector">The group of selectors to use.</param>
61-
public IElement Find(string selector) => GetNodes().QuerySelector(selector);
60+
/// <param name="cssSelector">The group of selectors to use.</param>
61+
public IElement Find(string cssSelector)
62+
{
63+
var result = GetNodes().QuerySelector(cssSelector);
64+
if (result is null)
65+
throw new ElementNotFoundException(cssSelector);
66+
else
67+
return result;
68+
}
6269

6370
/// <summary>
64-
/// Returns a list of the elements within the rendered fragment or component under test,
65-
/// (using depth-first pre-order traversal of the document's nodes) that match the specified group of selectors.
71+
/// Returns a list of elements from the rendered fragment or component under test,
72+
/// using the provided <paramref name="cssSelector"/>, in a depth-first pre-order traversal
73+
/// of the rendered nodes.
6674
/// </summary>
67-
/// <param name="selector">The group of selectors to use.</param>
68-
public IHtmlCollection<IElement> FindAll(string selector)
75+
/// <param name="cssSelector">The group of selectors to use.</param>
76+
public IHtmlCollection<IElement> FindAll(string cssSelector)
6977
{
70-
return GetNodes().QuerySelectorAll(selector);
78+
return GetNodes().QuerySelectorAll(cssSelector);
7179
}
7280
}
7381
}

src/TestContext.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ public IRenderedComponent<TComponent> RenderComponent<TComponent>(params Compone
5252
return result;
5353
}
5454

55-
/// <inheritdoc/>
56-
public IRenderedComponent<TComponent> RenderComponent<TComponent>(RenderFragment childContent, params ComponentParameter[] parameters) where TComponent : class, IComponent
57-
{
58-
var pAndCC = parameters.Concat(new[] { ComponentParameter.CreateParameter("ChildContent", childContent) }).ToArray();
59-
return RenderComponent<TComponent>(pAndCC);
60-
}
61-
6255
/// <inheritdoc/>
6356
public IRenderedComponent<TComponent> RenderComponent<TComponent>(ParameterView parameters) where TComponent : class, IComponent
6457
{

tests/RenderedFragmentTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Egil.RazorComponents.Testing.SampleComponents;
2+
using Shouldly;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using Xunit;
7+
8+
namespace Egil.RazorComponents.Testing
9+
{
10+
public class RenderedFragmentTest : ComponentTestFixture
11+
{
12+
[Fact(DisplayName = "Find throws an exception if no element matches the css selector")]
13+
public void Test001()
14+
{
15+
var cut = RenderComponent<Wrapper>();
16+
Should.Throw<ElementNotFoundException>(() => cut.Find("div"));
17+
}
18+
19+
[Fact(DisplayName = "Find returns expected element that matches the css selector")]
20+
public void Test002()
21+
{
22+
var cut = RenderComponent<Wrapper>(ChildContent("<div>"));
23+
var result = cut.Find("div");
24+
result.ShouldNotBeNull();
25+
}
26+
}
27+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@ChildContent
22
@code{
3-
[Parameter] public RenderFragment ChildContent { get; set; } = default!;
3+
[Parameter] public RenderFragment? ChildContent { get; set; }
44
}

0 commit comments

Comments
 (0)