Skip to content

Commit edbb8cf

Browse files
committed
feat: add tests for ShellDocs service registration and slot splitting functionality, enhancing test coverage and reliability
1 parent ba94b27 commit edbb8cf

4 files changed

Lines changed: 149 additions & 0 deletions

File tree

shelldocs.slnx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<Solution>
2+
<Folder Name="/examples/">
3+
<Project Path="examples/ShellDocs.Preview/ShellDocs.Preview.csproj" />
4+
</Folder>
25
<Folder Name="/src/">
36
<Project Path="src/ShellDocs.CLI/ShellDocs.CLI.csproj" />
47
<Project Path="src/ShellDocs.Components/ShellDocs.Components.csproj" />
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using ShellDocs.Components;
3+
using ShellDocs.Core;
4+
using ShellDocs.Markdown;
5+
using Xunit;
6+
7+
namespace ShellDocs.Tests;
8+
9+
public class AddShellDocsTests
10+
{
11+
[Fact]
12+
public void AddShellDocs_RegistersCoreServices()
13+
{
14+
var services = new ServiceCollection();
15+
services.AddShellDocs(o => o.SiteName = "Test");
16+
var sp = services.BuildServiceProvider();
17+
18+
Assert.NotNull(sp.GetService<ShellDocsOptions>());
19+
Assert.NotNull(sp.GetService<TypeRegistry>());
20+
Assert.NotNull(sp.GetService<MarkdownRenderer>());
21+
Assert.NotNull(sp.GetService<NavigationGraph>());
22+
}
23+
24+
[Fact]
25+
public void AddShellDocs_MissingContentRoot_ReturnsEmptyGraph()
26+
{
27+
var services = new ServiceCollection();
28+
services.AddShellDocs(o => o.ContentRoot = "does-not-exist-" + Guid.NewGuid().ToString("N"));
29+
var sp = services.BuildServiceProvider();
30+
31+
var graph = sp.GetRequiredService<NavigationGraph>();
32+
Assert.Empty(graph.Root.Children);
33+
}
34+
35+
[Fact]
36+
public void AddShellDocs_RegisteredComponents_FlowIntoTypeRegistry()
37+
{
38+
var services = new ServiceCollection();
39+
services.AddShellDocs(o => o.RegisterComponent<Callout>());
40+
var sp = services.BuildServiceProvider();
41+
42+
var registry = sp.GetRequiredService<TypeRegistry>();
43+
Assert.Equal(typeof(Callout), registry.Resolve("Callout"));
44+
}
45+
46+
[Fact]
47+
public void ShellDocsOptions_FluentAddNavLink_AppendsInOrder()
48+
{
49+
var options = new ShellDocsOptions()
50+
.AddNavLink("Docs", "/docs")
51+
.AddNavLink("Blog", "/blog");
52+
53+
Assert.Equal(2, options.PrimaryNav.Count);
54+
Assert.Equal("Docs", options.PrimaryNav[0].Label);
55+
Assert.Equal("/blog", options.PrimaryNav[1].Href);
56+
}
57+
58+
public class Callout : Microsoft.AspNetCore.Components.ComponentBase { }
59+
}

tests/ShellDocs.Tests/ShellDocs.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ItemGroup>
99
<ProjectReference Include="..\..\src\ShellDocs.Core\ShellDocs.Core.csproj" />
1010
<ProjectReference Include="..\..\src\ShellDocs.Markdown\ShellDocs.Markdown.csproj" />
11+
<ProjectReference Include="..\..\src\ShellDocs.Components\ShellDocs.Components.csproj" />
1112
<ProjectReference Include="..\..\src\ShellDocs.Templates\ShellDocs.Templates.csproj" />
1213
</ItemGroup>
1314

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using ShellDocs.Components.Content;
2+
using ShellDocs.Core;
3+
using ShellDocs.Markdown;
4+
using Xunit;
5+
6+
namespace ShellDocs.Tests;
7+
8+
public class SlotSplitterTests
9+
{
10+
public class Fake : Microsoft.AspNetCore.Components.ComponentBase { }
11+
12+
private static RenderedDocument DocumentWith(string html, params Slot[] slots) =>
13+
new(html, slots, new ParsedDocument(new Dictionary<string, object?>(), ""), Array.Empty<Heading>());
14+
15+
[Fact]
16+
public void Split_HtmlOnly_ReturnsSingleHtmlPart()
17+
{
18+
var doc = DocumentWith("<p>Hello world</p>");
19+
var parts = SlotSplitter.Split(doc);
20+
21+
var only = Assert.Single(parts);
22+
var html = Assert.IsType<HtmlPart>(only);
23+
Assert.Equal("<p>Hello world</p>", html.Html);
24+
}
25+
26+
[Fact]
27+
public void Split_SlotOnly_ReturnsSingleSlotPart()
28+
{
29+
var slot = new ComponentSlot("sabc", typeof(Fake), new Dictionary<string, string>(), null);
30+
var doc = DocumentWith(
31+
"<div data-shelldocs-slot=\"component\" data-shelldocs-id=\"sabc\"></div>",
32+
slot);
33+
34+
var parts = SlotSplitter.Split(doc);
35+
36+
var only = Assert.Single(parts);
37+
var sp = Assert.IsType<SlotPart>(only);
38+
Assert.Same(slot, sp.Slot);
39+
}
40+
41+
[Fact]
42+
public void Split_MixedContent_InterleavesInOrder()
43+
{
44+
var slot1 = new ComponentSlot("s1", typeof(Fake), new Dictionary<string, string>(), null);
45+
var slot2 = new PreviewSlot("s2", typeof(Fake), new Dictionary<string, string>(), "code", "razor");
46+
var html =
47+
"<p>Before</p>" +
48+
"<div data-shelldocs-slot=\"component\" data-shelldocs-id=\"s1\"></div>" +
49+
"<p>Middle</p>" +
50+
"<div data-shelldocs-slot=\"preview\" data-shelldocs-id=\"s2\"></div>" +
51+
"<p>After</p>";
52+
var doc = DocumentWith(html, slot1, slot2);
53+
54+
var parts = SlotSplitter.Split(doc);
55+
56+
Assert.Equal(5, parts.Count);
57+
Assert.Equal("<p>Before</p>", ((HtmlPart)parts[0]).Html);
58+
Assert.Same(slot1, ((SlotPart)parts[1]).Slot);
59+
Assert.Equal("<p>Middle</p>", ((HtmlPart)parts[2]).Html);
60+
Assert.Same(slot2, ((SlotPart)parts[3]).Slot);
61+
Assert.Equal("<p>After</p>", ((HtmlPart)parts[4]).Html);
62+
}
63+
64+
[Fact]
65+
public void Split_UnknownSlotId_DropsPlaceholder()
66+
{
67+
var html =
68+
"<p>A</p>" +
69+
"<div data-shelldocs-slot=\"component\" data-shelldocs-id=\"missing\"></div>" +
70+
"<p>B</p>";
71+
var doc = DocumentWith(html);
72+
73+
var parts = SlotSplitter.Split(doc);
74+
75+
Assert.Equal(2, parts.Count);
76+
Assert.Equal("<p>A</p>", ((HtmlPart)parts[0]).Html);
77+
Assert.Equal("<p>B</p>", ((HtmlPart)parts[1]).Html);
78+
}
79+
80+
[Fact]
81+
public void Split_EmptyHtml_ReturnsEmpty()
82+
{
83+
var doc = DocumentWith("");
84+
Assert.Empty(SlotSplitter.Split(doc));
85+
}
86+
}

0 commit comments

Comments
 (0)