|
| 1 | +import { test, expect } from "./_shared/fixtures"; |
| 2 | +import { Locator, Page } from "@playwright/test"; |
| 3 | + |
| 4 | +async function openShareModal(page: Page): Promise<Locator> { |
| 5 | + const shareButton = page.getByRole("button", { name: "Share" }); |
| 6 | + |
| 7 | + await expect(shareButton).toBeVisible(); |
| 8 | + await shareButton.click(); |
| 9 | + |
| 10 | + const modal = page.getByRole("dialog"); |
| 11 | + await expect(modal).toBeVisible(); |
| 12 | + |
| 13 | + return modal; |
| 14 | +} |
| 15 | + |
| 16 | +async function getSharePopupFor(label: string, page: Page) { |
| 17 | + const [popup] = await Promise.all([ |
| 18 | + page.waitForEvent("popup"), |
| 19 | + page.getByLabel(label).click(), |
| 20 | + ]); |
| 21 | + return popup; |
| 22 | +} |
| 23 | + |
| 24 | +function getUrlParam(url: string, name: string) { |
| 25 | + const params = new URLSearchParams(new URL(url).search); |
| 26 | + return params.get(name); |
| 27 | +} |
| 28 | + |
| 29 | +test.describe("ShareModal", () => { |
| 30 | + let blogPostUrl: string; |
| 31 | + let blogPostTitle: string; |
| 32 | + |
| 33 | + test.beforeEach(async ({ page }) => { |
| 34 | + await page.goto("/"); |
| 35 | + |
| 36 | + const blogPosts = page.getByTestId("featured-post-link"); |
| 37 | + const firstBlogPost = blogPosts.first(); |
| 38 | + |
| 39 | + blogPostUrl = (await firstBlogPost.getAttribute("href")) || ""; |
| 40 | + |
| 41 | + await page.goto(blogPostUrl); |
| 42 | + |
| 43 | + blogPostTitle = await page.getByTestId("blog-post-title").textContent(); |
| 44 | + }); |
| 45 | + |
| 46 | + test("should open share modal and display share options", async ({ |
| 47 | + page, |
| 48 | + }) => { |
| 49 | + await expect( |
| 50 | + page.getByRole("heading", { name: blogPostTitle }), |
| 51 | + ).toBeVisible(); |
| 52 | + |
| 53 | + const modal = await openShareModal(page); |
| 54 | + |
| 55 | + await expect(modal.getByText("Share this post")).toBeVisible(); |
| 56 | + await expect( |
| 57 | + modal.getByRole("button", { name: "Copy Link" }), |
| 58 | + ).toBeVisible(); |
| 59 | + await expect(modal.getByLabel("Share via Email")).toBeVisible(); |
| 60 | + await expect(modal.getByLabel("Share on Twitter")).toBeVisible(); |
| 61 | + await expect(modal.getByLabel("Share on Telegram")).toBeVisible(); |
| 62 | + await expect(modal.getByLabel("Share on LinkedIn")).toBeVisible(); |
| 63 | + await expect(modal.getByLabel("Share on WhatsApp")).toBeVisible(); |
| 64 | + }); |
| 65 | + |
| 66 | + test("should copy the post link to clipboard", async ({ page }, testInfo) => { |
| 67 | + if ( |
| 68 | + ["webkit", "mobile-chrome", "mobile-safari", "microsoft-edge"].includes( |
| 69 | + testInfo.project.name, |
| 70 | + ) |
| 71 | + ) { |
| 72 | + test.skip(); |
| 73 | + } |
| 74 | + |
| 75 | + await openShareModal(page); |
| 76 | + |
| 77 | + await page.getByRole("button", { name: "Copy Link" }).click(); |
| 78 | + const clipboardText = await page.evaluate(() => |
| 79 | + navigator.clipboard.readText(), |
| 80 | + ); |
| 81 | + expect(clipboardText).toContain(blogPostUrl); |
| 82 | + }); |
| 83 | + |
| 84 | + test("should share on Email", async ({ page }) => { |
| 85 | + await openShareModal(page); |
| 86 | + |
| 87 | + const emailButton = page.getByLabel("Share via Email"); |
| 88 | + await expect(emailButton).toBeVisible(); |
| 89 | + await emailButton.click(); |
| 90 | + }); |
| 91 | + |
| 92 | + test("should share on Twitter", async ({ page }) => { |
| 93 | + await openShareModal(page); |
| 94 | + |
| 95 | + const popup = await getSharePopupFor("Share on Twitter", page); |
| 96 | + |
| 97 | + expect(popup.url()).toContain("x.com/intent/tweet?url="); |
| 98 | + expect(popup.url()).toContain(`${encodeURIComponent(blogPostTitle)}`); |
| 99 | + expect(popup.url()).toContain(`${encodeURIComponent(blogPostUrl)}`); |
| 100 | + }); |
| 101 | + |
| 102 | + test("should share on Telegram", async ({ page }) => { |
| 103 | + await openShareModal(page); |
| 104 | + |
| 105 | + const popup = await getSharePopupFor("Share on Telegram", page); |
| 106 | + |
| 107 | + expect(popup.url()).toContain("telegram.me/share/url?url="); |
| 108 | + expect(popup.url()).toContain(`${encodeURIComponent(blogPostTitle)}`); |
| 109 | + expect(popup.url()).toContain(`${encodeURIComponent(blogPostUrl)}`); |
| 110 | + }); |
| 111 | + |
| 112 | + test("should share on LinkedIn", async ({ page }) => { |
| 113 | + await openShareModal(page); |
| 114 | + |
| 115 | + const popup = await getSharePopupFor("Share on LinkedIn", page); |
| 116 | + |
| 117 | + const sessionRedirect = getUrlParam(popup.url(), "session_redirect"); |
| 118 | + expect(sessionRedirect).not.toBeNull(); |
| 119 | + |
| 120 | + const decodedRedirectUrl = decodeURIComponent(sessionRedirect as string); |
| 121 | + |
| 122 | + expect(decodedRedirectUrl).toContain("linkedin.com/shareArticle"); |
| 123 | + expect(decodedRedirectUrl).toContain(blogPostTitle); |
| 124 | + expect(decodedRedirectUrl).toContain(blogPostUrl); |
| 125 | + }); |
| 126 | + |
| 127 | + test("should share on WhatsApp", async ({ page }) => { |
| 128 | + await openShareModal(page); |
| 129 | + |
| 130 | + const popup = await getSharePopupFor("Share on WhatsApp", page); |
| 131 | + |
| 132 | + expect(popup.url()).toContain("whatsapp.com/send"); |
| 133 | + expect(popup.url()).toContain(`${encodeURIComponent(blogPostTitle)}`); |
| 134 | + expect(popup.url()).toContain(`${encodeURIComponent(blogPostUrl)}`); |
| 135 | + }); |
| 136 | +}); |
0 commit comments