|
| 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