|
| 1 | +using Microsoft.AspNetCore.Components; |
| 2 | +using ShellDocs.Components; |
| 3 | +using ShellDocs.Core; |
| 4 | +using ShellDocs.Markdown; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace ShellDocs.Tests; |
| 8 | + |
| 9 | +public class DocsPageStateTests |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public void SetDocument_FiresOnChange_AndStoresDocument() |
| 13 | + { |
| 14 | + var (graph, nav) = MinimalGraphAndNav("http://localhost/docs/unknown"); |
| 15 | + var state = new DocsPageState(graph, nav); |
| 16 | + var doc = new RenderedDocument("<p>hi</p>", Array.Empty<Slot>(), null!, Array.Empty<Heading>()); |
| 17 | + |
| 18 | + var fired = 0; |
| 19 | + state.OnChange += () => fired++; |
| 20 | + |
| 21 | + state.SetDocument(doc); |
| 22 | + |
| 23 | + Assert.Equal(1, fired); |
| 24 | + Assert.Same(doc, state.Document); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void UnknownUrl_YieldsEmptyChromeState() |
| 29 | + { |
| 30 | + var (graph, nav) = MinimalGraphAndNav("http://localhost/docs/does-not-exist"); |
| 31 | + var state = new DocsPageState(graph, nav); |
| 32 | + |
| 33 | + state.SetDocument(null); |
| 34 | + |
| 35 | + Assert.Null(state.CurrentNode); |
| 36 | + Assert.Null(state.Prev); |
| 37 | + Assert.Null(state.Next); |
| 38 | + Assert.Empty(state.Breadcrumbs); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void Dispose_UnsubscribesFromNavigationManager() |
| 43 | + { |
| 44 | + var (graph, nav) = MinimalGraphAndNav("http://localhost/"); |
| 45 | + var state = new DocsPageState(graph, nav); |
| 46 | + |
| 47 | + // Should not throw; sanity that Dispose is idempotent-ish. |
| 48 | + state.Dispose(); |
| 49 | + } |
| 50 | + |
| 51 | + private static (NavigationGraph, NavigationManager) MinimalGraphAndNav(string uri) |
| 52 | + { |
| 53 | + var root = new NavigationNode { Kind = NodeKind.Section }; |
| 54 | + return (new NavigationGraph(root), new StubNavigationManager(uri)); |
| 55 | + } |
| 56 | + |
| 57 | + private sealed class StubNavigationManager : NavigationManager |
| 58 | + { |
| 59 | + public StubNavigationManager(string uri) => Initialize("http://localhost/", uri); |
| 60 | + protected override void NavigateToCore(string uri, bool forceLoad) { } |
| 61 | + } |
| 62 | +} |
0 commit comments