Skip to content

Commit 1153ac1

Browse files
committed
Set OgData for archive
1 parent a6a3ef6 commit 1153ac1

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

src/LinkDotNet.Blog.Web/Features/Archive/ArchivePage.razor

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
@using LinkDotNet.Blog.Infrastructure.Persistence
33
@using LinkDotNet.Blog.Domain
44
@inject IRepository<BlogPost> repository
5+
6+
<OgData Title="Archive"></OgData>
7+
58
<div class="ps-2">
69
<h3 class="pb-3">Archive</h3>
710

src/LinkDotNet.Blog.Web/Features/Components/OgData.razor

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
<HeadContent>
44
<meta name="title" property="og:title" content="@Title" />
5-
<meta name="image" property="og:image" content="@AbsolutePreviewImageUrl" />
5+
@if (AbsolutePreviewImageUrl != null)
6+
{
7+
<meta name="image" property="og:image" content="@AbsolutePreviewImageUrl"/>
8+
}
69
<meta property="og:type" content="article" />
710
<meta property="og:url" content="@navigationManager.Uri" />
811
@if (Keywords != null)

tests/LinkDotNet.Blog.UnitTests/Web/Features/Archive/ArchivePageTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,23 @@ public void ShouldShowLoading()
3434

3535
cut.FindComponents<Loading>().Count.Should().Be(1);
3636
}
37+
38+
[Fact]
39+
public void ShouldSetOgData()
40+
{
41+
var repository = new Mock<IRepository<BlogPost>>();
42+
Services.AddScoped(_ => repository.Object);
43+
repository.Setup(r => r.GetAllAsync(
44+
It.IsAny<Expression<Func<BlogPost, bool>>>(),
45+
It.IsAny<Expression<Func<BlogPost, object>>>(),
46+
It.IsAny<bool>(),
47+
It.IsAny<int>(),
48+
It.IsAny<int>()))
49+
.ReturnsAsync(new PagedList<BlogPost>(Array.Empty<BlogPost>(), 1, 1));
50+
51+
var cut = RenderComponent<ArchivePage>();
52+
53+
var ogData = cut.FindComponent<OgData>().Instance;
54+
ogData.Title.Should().Contain("Archive");
55+
}
3756
}

tests/LinkDotNet.Blog.UnitTests/Web/Features/Components/OgDataTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ public void ShouldRenderMetaTagsWhenSet()
2525
AssertMetaTagExistsWithValue(cut, "description", "Description", "og:description");
2626
}
2727

28+
[Fact]
29+
public void ShouldNotSetMetaInformationWhenNotProvided()
30+
{
31+
ComponentFactories.AddStub<HeadContent>(ps => ps.Get(p => p.ChildContent));
32+
33+
var cut = RenderComponent<OgData>(p => p
34+
.Add(s => s.Title, "Title"));
35+
36+
GetMetaTagExists(cut, "image").Should().BeFalse();
37+
GetMetaTagExists(cut, "keywords").Should().BeFalse();
38+
GetMetaTagExists(cut, "description").Should().BeFalse();
39+
}
40+
2841
private static void AssertMetaTagExistsWithValue(
2942
IRenderedFragment cut,
3043
string metaTag,
@@ -41,4 +54,12 @@ private static void AssertMetaTagExistsWithValue(
4154
titleMetaTag.Attributes.Any(a => a.Value == ogPropertyName).Should().BeTrue();
4255
}
4356
}
57+
58+
private static bool GetMetaTagExists(
59+
IRenderedFragment cut,
60+
string metaTag)
61+
{
62+
var metaTags = cut.FindAll("meta");
63+
return metaTags.Any(m => m.Attributes.Any(a => a.Value == metaTag));
64+
}
4465
}

0 commit comments

Comments
 (0)