From 191c1f849197003f933791d1ecc52519c4d11a9e Mon Sep 17 00:00:00 2001
From: Sid Vishnoi <8426945+sidvishnoi@users.noreply.github.com>
Date: Mon, 13 Jan 2025 16:45:53 +0530
Subject: [PATCH] add e2e tests
---
src/pages/popup/components/NotMonetized.tsx | 7 +-
tests/e2e/special.spec.ts | 140 ++++++++++++++++++++
2 files changed, 146 insertions(+), 1 deletion(-)
create mode 100644 tests/e2e/special.spec.ts
diff --git a/src/pages/popup/components/NotMonetized.tsx b/src/pages/popup/components/NotMonetized.tsx
index a51b0ca4..60a6a9c4 100644
--- a/src/pages/popup/components/NotMonetized.tsx
+++ b/src/pages/popup/components/NotMonetized.tsx
@@ -7,7 +7,12 @@ export const NotMonetized = ({ text }: { text: string }) => {
- {text}
+
+ {text}
+
);
};
diff --git a/tests/e2e/special.spec.ts b/tests/e2e/special.spec.ts
new file mode 100644
index 00000000..2f274e53
--- /dev/null
+++ b/tests/e2e/special.spec.ts
@@ -0,0 +1,140 @@
+import { spy } from 'tinyspy';
+import { test, expect } from './fixtures/connected';
+
+test('iframe add/remove does not de-monetize main page', async ({
+ page,
+ background,
+ popup,
+}) => {
+ const walletAddressUrl = process.env.TEST_WALLET_ADDRESS_URL;
+ const playgroundUrl = 'https://webmonetization.org/play/';
+
+ const monetizationCallback = await test.step('prepare', async () => {
+ await expect(popup.getByTestId('not-monetized-message')).toBeVisible();
+
+ await page.goto(playgroundUrl);
+
+ const monetizationCallback = spy<[Event], void>();
+ await page.exposeFunction('monetizationCallback', monetizationCallback);
+ await page.evaluate(() => {
+ window.addEventListener('monetization', monetizationCallback);
+ });
+
+ await page
+ .getByLabel('Wallet address/Payment pointer')
+ .fill(walletAddressUrl);
+ await page.getByRole('button', { name: 'Add monetization link' }).click();
+
+ await page.$eval(
+ 'link[rel="monetization"]',
+ (el) => new Promise((res) => el.addEventListener('load', res)),
+ );
+
+ await page.waitForTimeout(2000);
+ await expect(monetizationCallback).toHaveBeenCalledTimes(1);
+ await expect(monetizationCallback).toHaveBeenLastCalledWithMatching({
+ paymentPointer: walletAddressUrl,
+ });
+
+ await expect(popup.getByTestId('home-page')).toBeVisible();
+
+ return monetizationCallback;
+ });
+
+ await test.step('add iframe', async () => {
+ await page.evaluate(() => {
+ return new Promise((resolve) => {
+ const iframe = document.createElement('iframe');
+ iframe.id = 'cross-origin-iframe';
+ iframe.src = 'https://example.com';
+ document.body.prepend(iframe);
+ iframe.addEventListener('load', resolve);
+ });
+ });
+
+ await expect(page.locator('#cross-origin-iframe')).toBeVisible();
+ await expect(popup.getByTestId('home-page')).toBeVisible();
+ });
+
+ await test.step('remove iframe', async () => {
+ await page.evaluate(() => {
+ const iframe = document.getElementById('cross-origin-iframe');
+ iframe?.remove();
+ });
+ await expect(page.locator('#cross-origin-iframe')).not.toBeAttached();
+
+ await expect(popup.getByTestId('not-monetized-message')).not.toBeVisible();
+ await expect(popup.getByTestId('home-page')).toBeVisible();
+ });
+});
+
+test('iframe navigate does not de-monetize main page', async ({
+ page,
+ background,
+ popup,
+}) => {
+ const walletAddressUrl = process.env.TEST_WALLET_ADDRESS_URL;
+ const playgroundUrl = 'https://webmonetization.org/play/';
+
+ const monetizationCallback = await test.step('prepare', async () => {
+ await expect(popup.getByTestId('not-monetized-message')).toBeVisible();
+
+ await page.goto(playgroundUrl);
+
+ const monetizationCallback = spy<[Event], void>();
+ await page.exposeFunction('monetizationCallback', monetizationCallback);
+ await page.evaluate(() => {
+ window.addEventListener('monetization', monetizationCallback);
+ });
+
+ await page
+ .getByLabel('Wallet address/Payment pointer')
+ .fill(walletAddressUrl);
+ await page.getByRole('button', { name: 'Add monetization link' }).click();
+
+ await page.$eval(
+ 'link[rel="monetization"]',
+ (el) => new Promise((res) => el.addEventListener('load', res)),
+ );
+
+ await page.waitForTimeout(2000);
+ await expect(monetizationCallback).toHaveBeenCalledTimes(1);
+ await expect(monetizationCallback).toHaveBeenLastCalledWithMatching({
+ paymentPointer: walletAddressUrl,
+ });
+
+ await expect(popup.getByTestId('home-page')).toBeVisible();
+
+ return monetizationCallback;
+ });
+
+ await test.step('add iframe', async () => {
+ await page.evaluate(() => {
+ return new Promise((resolve) => {
+ const iframe = document.createElement('iframe');
+ iframe.id = 'cross-origin-iframe';
+ iframe.src = 'https://example.com';
+ document.body.prepend(iframe);
+ iframe.addEventListener('load', resolve, { once: true });
+ });
+ });
+ });
+
+ await test.step('navigate iframe', async () => {
+ await page.evaluate(() => {
+ return new Promise((resolve, reject) => {
+ const iframe = document.getElementById('cross-origin-iframe');
+ if (!iframe) {
+ reject(new Error('iframe not found'));
+ return;
+ }
+ iframe.addEventListener('load', resolve, { once: true });
+ iframe.addEventListener('error', resolve, { once: true });
+ iframe.setAttribute('src', 'https://example.com/test');
+ });
+ });
+
+ await expect(popup.getByTestId('not-monetized-message')).not.toBeVisible();
+ await expect(popup.getByTestId('home-page')).toBeVisible();
+ });
+});