Skip to content

Commit 78b58a3

Browse files
committed
checkpoint
1 parent 91fa784 commit 78b58a3

22 files changed

+675
-48
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<AspNetCoreVersion>3.0.0-preview9.19424.4</AspNetCoreVersion>
3+
<AspNetCoreVersion>3.0.0</AspNetCoreVersion>
44
</PropertyGroup>
55
</Project>

src/AngleSharp.Diffing/Diff.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Egil.RazorComponents.Testing.AngleSharpDiffing
8+
{
9+
public class Diff
10+
{
11+
12+
}
13+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using AngleSharp.Dom;
7+
8+
namespace Egil.RazorComponents.Testing.AngleSharpDiffing
9+
{
10+
public class DiffBuilder
11+
{
12+
private readonly INode _controlNode;
13+
14+
private DiffBuilder(INode controlNode) {
15+
_controlNode = controlNode;
16+
}
17+
18+
public static DiffBuilder Compare(INode controlNode)
19+
{
20+
if(controlNode is null) throw new ArgumentNullException(nameof(controlNode));
21+
return new DiffBuilder(controlNode);
22+
}
23+
24+
public DiffBuilder WithTest(INode testNode)
25+
{
26+
if(testNode is null) throw new ArgumentNullException(nameof(testNode));
27+
return this;
28+
}
29+
30+
public Diff Build() { return null; }
31+
}
32+
33+
public class DifferenceEngine
34+
{
35+
36+
}
37+
}

src/Egil.RazorComponents.Testing.Library.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26+
<PackageReference Include="AngleSharp" Version="0.13.0" />
2627
<PackageReference Include="Microsoft.AspNetCore.Components" Version="$(AspNetCoreVersion)" />
2728
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="$(AspNetCoreVersion)" />
2829
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.4">

src/Fact.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3+
using AngleSharp.Dom;
34
using Microsoft.AspNetCore.Components;
45
using Microsoft.AspNetCore.Components.Rendering;
56
using Microsoft.AspNetCore.Components.RenderTree;

src/Render/ContainerComponent.cs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Components;
22
using Microsoft.AspNetCore.Components.RenderTree;
33
using System;
4+
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Threading.Tasks;
@@ -39,38 +40,35 @@ public Task SetParametersAsync(ParameterView parameters)
3940
throw new NotImplementedException($"{nameof(ContainerComponent)} shouldn't receive any parameters");
4041
}
4142

42-
public (int Id, TComponent Component) FindComponentUnderTest<TComponent>() where TComponent : IComponent
43+
public void RenderComponentUnderTest(RenderFragment renderFragment)
44+
{
45+
_renderer.DispatchAndAssertNoSynchronousErrors(() =>
46+
{
47+
_renderHandle.Render(renderFragment);
48+
});
49+
}
50+
51+
public List<(int Id, T Component)> Find<T>()
4352
{
4453
var ownFrames = _renderer.GetCurrentRenderTreeFrames(_componentId);
4554
if (ownFrames.Count == 0)
4655
{
4756
throw new InvalidOperationException($"{nameof(ContainerComponent)} hasn't yet rendered");
4857
}
4958

50-
ref var childComponentFrame = ref ownFrames.Array[0];
51-
if (childComponentFrame.FrameType == RenderTreeFrameType.Component && childComponentFrame.Component is TComponent component)
59+
var result = new List<(int Id, T Component)>();
60+
for (int i = 0; i < ownFrames.Count; i++)
5261
{
53-
return (childComponentFrame.ComponentId, component);
62+
ref var frame = ref ownFrames.Array[i];
63+
if (frame.FrameType == RenderTreeFrameType.Component &&
64+
frame.Component != null &&
65+
frame.Component is T factFrame)
66+
{
67+
result.Add((frame.ComponentId, factFrame));
68+
}
5469
}
55-
else throw new Exception("Component not found");
70+
return result;
5671
}
5772

58-
public void RenderComponentUnderTest(Type componentType, ParameterView parameters)
59-
{
60-
_renderer.DispatchAndAssertNoSynchronousErrors(() =>
61-
{
62-
_renderHandle.Render(builder =>
63-
{
64-
builder.OpenComponent(0, componentType);
65-
66-
foreach (var parameterValue in parameters)
67-
{
68-
builder.AddAttribute(1, parameterValue.Name, parameterValue.Value);
69-
}
70-
71-
builder.CloseComponent();
72-
});
73-
});
74-
}
7573
}
7674
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using AngleSharp.Html.Dom;
7+
8+
namespace Egil.RazorComponents.Testing.Render
9+
{
10+
public static class HtmlElementExtensions
11+
{
12+
public static void Click(this IHtmlElement element, string selector) { }
13+
}
14+
}

src/Render/Htmlizer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private static int RenderChildren(HtmlRenderingContext context, ArrayRange<Rende
161161

162162
private static int RenderAttributes(
163163
HtmlRenderingContext context,
164-
ArrayRange<RenderTreeFrame> frames, int position, int maxElements, out string capturedValueAttribute)
164+
ArrayRange<RenderTreeFrame> frames, int position, int maxElements, out string? capturedValueAttribute)
165165
{
166166
capturedValueAttribute = null;
167167

@@ -225,7 +225,7 @@ public HtmlRenderingContext(TestRenderer renderer)
225225

226226
public List<string> Result { get; } = new List<string>();
227227

228-
public string ClosestSelectValueAsString { get; set; }
228+
public string? ClosestSelectValueAsString { get; set; }
229229
}
230230
}
231231
}

src/Render/RenderedComponent.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
using Microsoft.AspNetCore.Components;
1+
using AngleSharp;
2+
using AngleSharp.Dom;
3+
using AngleSharp.Html.Parser;
4+
using AngleSharp.Io;
5+
using Microsoft.AspNetCore.Components;
26
using System.Collections.Generic;
37
using System.Linq;
48

59
namespace Egil.RazorComponents.Testing.Render
610
{
7-
public class RenderedComponent<TComponent> where TComponent : IComponent
11+
public class RenderedComponent<TComponent> where TComponent : class, IComponent
812
{
13+
private readonly IBrowsingContext _browsingContext = BrowsingContext.New(Configuration.Default);
914
private readonly TestRenderer _renderer;
1015
private readonly ContainerComponent _containerTestRootComponent;
1116
private int _testComponentId;
12-
private TComponent _testComponentInstance;
17+
private TComponent? _testComponentInstance;
1318

1419
internal RenderedComponent(TestRenderer renderer)
1520
{
1621
_renderer = renderer;
1722
_containerTestRootComponent = new ContainerComponent(_renderer);
1823
}
1924

20-
public TComponent Instance => _testComponentInstance;
25+
public TComponent? Instance => _testComponentInstance;
2126

2227
public string GetMarkup()
2328
{
@@ -26,31 +31,30 @@ public string GetMarkup()
2631

2732
internal void SetParametersAndRender(ParameterView parameters)
2833
{
29-
_containerTestRootComponent.RenderComponentUnderTest(typeof(TComponent), parameters);
30-
var foundTestComponent = _containerTestRootComponent.FindComponentUnderTest<TComponent>();
31-
_testComponentId = foundTestComponent.Id;
32-
_testComponentInstance = foundTestComponent.Component;
34+
//_containerTestRootComponent.RenderComponentUnderTest(typeof(TComponent), parameters);
35+
//(_testComponentId, _testComponentInstance) = _containerTestRootComponent.FindComponentUnderTest<TComponent>();
3336
}
3437

35-
public HtmlNode Find(string selector)
38+
public IElement Find(string selector)
3639
{
3740
return FindAll(selector).FirstOrDefault();
3841
}
3942

40-
public ICollection<HtmlNode> FindAll(string selector)
43+
public IHtmlCollection<IElement> FindAll(string selector)
4144
{
42-
// Rather than using HTML strings, it would be faster and more powerful
43-
// to implement Fizzler's APIs for walking directly over the rendered
44-
// frames, since Fizzler's core isn't tied to HTML (or HtmlAgilityPack).
45-
// The most awkward part of this will be handling Markup frames, since
46-
// they are HTML strings so would need to be parsed, or perhaps you can
47-
// pass through those calls into Fizzler.Systems.HtmlAgilityPack.
48-
4945
var markup = GetMarkup();
50-
var html = new TestHtmlDocument(_renderer);
5146

52-
html.LoadHtml(markup);
53-
return html.DocumentNode.QuerySelectorAll(selector).ToList();
47+
var document = _browsingContext.OpenAsync(x => x.Content(markup)).GetAwaiter().GetResult();
48+
return document.QuerySelectorAll(selector);
49+
50+
//var parser = context.GetService<IHtmlParser>();
51+
//var rootElement = document.QuerySelector(root) ?? document.CreateElement(root);
52+
//var actualFragment = parser.ParseFragment(actual, rootElement);
53+
54+
//var html = new TestHtmlDocument(_renderer);
55+
56+
//html.LoadHtml(markup);
57+
//return html.DocumentNode.QuerySelectorAll(selector).ToList();
5458
}
5559
}
5660
}

0 commit comments

Comments
 (0)