|
| 1 | +"""Test LinkedIn share and Copy Link markup.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import unittest |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from bs4 import BeautifulSoup |
| 10 | + |
| 11 | + |
| 12 | +class TestShareAndCopyLinks(unittest.TestCase): |
| 13 | + """Test LinkedIn share and Copy Link in templates and output.""" |
| 14 | + |
| 15 | + def setUp(self) -> None: |
| 16 | + """Set up test paths.""" |
| 17 | + self.root = Path(__file__).resolve().parents[1] |
| 18 | + self.theme_dir = self.root / "theme" |
| 19 | + |
| 20 | + def test_blog_post_template_markup(self) -> None: |
| 21 | + """Verify blog-post.html has correct LinkedIn and Copy Link tags.""" |
| 22 | + template_file = self.theme_dir / "blog-post.html" |
| 23 | + self.assertTrue(template_file.exists()) |
| 24 | + content = template_file.read_text(encoding="utf-8") |
| 25 | + |
| 26 | + # Check LinkedIn share URL endpoint |
| 27 | + expected_endpoint = ( |
| 28 | + "https://www.linkedin.com/sharing/share-offsite/" |
| 29 | + "?url={{ page.canonical_url or url }}" |
| 30 | + ) |
| 31 | + self.assertIn(expected_endpoint, content) |
| 32 | + self.assertNotIn('href="#linkedinshare"', content) |
| 33 | + |
| 34 | + # Check target and rel |
| 35 | + self.assertIn('target="_blank"', content) |
| 36 | + self.assertIn('rel="nofollow noopener"', content) |
| 37 | + |
| 38 | + # Check Copy Link button |
| 39 | + self.assertIn('class="link link_yank"', content) |
| 40 | + self.assertIn('href="{{ page.canonical_url or url }}"', content) |
| 41 | + |
| 42 | + def test_blog_list_template_markup(self) -> None: |
| 43 | + """Verify blog-list.html has correct LinkedIn and Copy Link tags.""" |
| 44 | + template_file = self.theme_dir / "blog-list.html" |
| 45 | + self.assertTrue(template_file.exists()) |
| 46 | + content = template_file.read_text(encoding="utf-8") |
| 47 | + |
| 48 | + # Check LinkedIn share URL endpoint |
| 49 | + expected_endpoint = ( |
| 50 | + "https://www.linkedin.com/sharing/share-offsite/?url={{ url }}" |
| 51 | + ) |
| 52 | + self.assertIn(expected_endpoint, content) |
| 53 | + self.assertNotIn('href="#linkedinshare"', content) |
| 54 | + |
| 55 | + # Check target and rel |
| 56 | + self.assertIn('target="_blank"', content) |
| 57 | + self.assertIn('rel="nofollow noopener"', content) |
| 58 | + |
| 59 | + # Check Copy Link button |
| 60 | + self.assertIn('class="link link_yank"', content) |
| 61 | + self.assertIn('href="{{ url }}"', content) |
| 62 | + |
| 63 | + def test_theme_js_copy_link_handler(self) -> None: |
| 64 | + """Verify theme.js has click handler for .link_yank and feedback.""" |
| 65 | + js_file = self.theme_dir / "js" / "theme.js" |
| 66 | + self.assertTrue(js_file.exists()) |
| 67 | + content = js_file.read_text(encoding="utf-8") |
| 68 | + |
| 69 | + self.assertIn(".link_yank", content) |
| 70 | + self.assertIn("preventDefault", content) |
| 71 | + self.assertIn("writeText", content) |
| 72 | + self.assertIn("copy-tooltip", content) |
| 73 | + self.assertIn("Copied!", content) |
| 74 | + self.assertIn("execCommand", content) |
| 75 | + |
| 76 | + def test_blog_css_copy_link_styles(self) -> None: |
| 77 | + """Verify blog.css styles .link_yank, .copied, and .copy-tooltip.""" |
| 78 | + css_file = self.theme_dir / "css" / "blog.css" |
| 79 | + self.assertTrue(css_file.exists()) |
| 80 | + content = css_file.read_text(encoding="utf-8") |
| 81 | + |
| 82 | + self.assertIn(".link_yank", content) |
| 83 | + self.assertIn(".link_yank.copied", content) |
| 84 | + self.assertIn(".copy-tooltip", content) |
| 85 | + |
| 86 | + def test_rendered_blog_list_html(self) -> None: |
| 87 | + """Verify rendered blog/index.html elements.""" |
| 88 | + build_html = self.root / "build" / "blog" / "index.html" |
| 89 | + if not build_html.exists(): |
| 90 | + return |
| 91 | + |
| 92 | + soup = BeautifulSoup( |
| 93 | + build_html.read_text(encoding="utf-8"), "html.parser" |
| 94 | + ) |
| 95 | + post_cards = soup.find_all("li", class_="post-card") |
| 96 | + self.assertGreater(len(post_cards), 0) |
| 97 | + |
| 98 | + for card in post_cards: |
| 99 | + share = card.find("div", class_="post_share") |
| 100 | + self.assertIsNotNone(share) |
| 101 | + |
| 102 | + # LinkedIn button |
| 103 | + linkedin = share.find("a", class_="linkedin") |
| 104 | + self.assertIsNotNone(linkedin) |
| 105 | + href = linkedin.get("href", "") |
| 106 | + share_prefix = ( |
| 107 | + "https://www.linkedin.com/sharing/share-offsite/?url=" |
| 108 | + ) |
| 109 | + self.assertTrue( |
| 110 | + href.startswith(share_prefix), |
| 111 | + f"Unexpected LinkedIn href: {href}", |
| 112 | + ) |
| 113 | + self.assertEqual(linkedin.get("target"), "_blank") |
| 114 | + self.assertEqual(linkedin.get("rel"), ["nofollow", "noopener"]) |
| 115 | + self.assertEqual(linkedin.get("title"), "Share on LinkedIn") |
| 116 | + svg_use = linkedin.find("use") |
| 117 | + self.assertIsNotNone(svg_use) |
| 118 | + self.assertEqual(svg_use.get("xlink:href"), "#linkedin") |
| 119 | + |
| 120 | + # Copy link button |
| 121 | + copy_btn = share.find("a", class_="link_yank") |
| 122 | + self.assertIsNotNone(copy_btn) |
| 123 | + self.assertTrue(bool(copy_btn.get("href"))) |
| 124 | + self.assertIn(copy_btn.get("title"), ["Copy link", "Copy Link"]) |
| 125 | + copy_svg_use = copy_btn.find("use") |
| 126 | + self.assertIsNotNone(copy_svg_use) |
| 127 | + self.assertEqual(copy_svg_use.get("xlink:href"), "#copy") |
| 128 | + |
| 129 | + def test_rendered_blog_post_html(self) -> None: |
| 130 | + """Verify rendered blog post pages share panel elements.""" |
| 131 | + build_dir = self.root / "build" / "blog" |
| 132 | + if not build_dir.exists(): |
| 133 | + return |
| 134 | + |
| 135 | + for post_html in build_dir.glob("*/index.html"): |
| 136 | + soup = BeautifulSoup( |
| 137 | + post_html.read_text(encoding="utf-8"), "html.parser" |
| 138 | + ) |
| 139 | + panel = soup.find("aside", class_="blog-share-panel") |
| 140 | + if not panel: |
| 141 | + continue |
| 142 | + |
| 143 | + # LinkedIn button |
| 144 | + linkedin = panel.find("a", class_="linkedin") |
| 145 | + self.assertIsNotNone( |
| 146 | + linkedin, f"Missing LinkedIn button in {post_html}" |
| 147 | + ) |
| 148 | + href = linkedin.get("href", "") |
| 149 | + share_prefix = ( |
| 150 | + "https://www.linkedin.com/sharing/share-offsite/?url=" |
| 151 | + ) |
| 152 | + self.assertTrue( |
| 153 | + href.startswith(share_prefix), |
| 154 | + f"Unexpected LinkedIn href in {post_html}: {href}", |
| 155 | + ) |
| 156 | + self.assertEqual(linkedin.get("target"), "_blank") |
| 157 | + self.assertEqual(linkedin.get("rel"), ["nofollow", "noopener"]) |
| 158 | + self.assertEqual(linkedin.get("title"), "Share on LinkedIn") |
| 159 | + svg_use = linkedin.find("use") |
| 160 | + self.assertIsNotNone(svg_use) |
| 161 | + self.assertEqual(svg_use.get("xlink:href"), "#linkedin") |
| 162 | + |
| 163 | + # Copy link button |
| 164 | + copy_btn = panel.find("a", class_="link_yank") |
| 165 | + self.assertIsNotNone( |
| 166 | + copy_btn, f"Missing Copy link button in {post_html}" |
| 167 | + ) |
| 168 | + self.assertTrue(bool(copy_btn.get("href"))) |
| 169 | + self.assertEqual(copy_btn.get("title"), "Copy link") |
| 170 | + copy_svg_use = copy_btn.find("use") |
| 171 | + self.assertIsNotNone(copy_svg_use) |
| 172 | + self.assertEqual(copy_svg_use.get("xlink:href"), "#copy") |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + unittest.main() |
0 commit comments