Skip to content

Commit

Permalink
add tests, working dev test server
Browse files Browse the repository at this point in the history
  • Loading branch information
Brunwo committed Oct 8, 2024
1 parent 8531390 commit 235a440
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
36 changes: 31 additions & 5 deletions e2e/share-target.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
const { test, expect } = require('@playwright/test');

// Add a helper function to check if the server is up
async function isServerUp(page) {
try {
const response = await page.goto('http://localhost:5173');
return response.status() === 200;
} catch (error) {
console.error('Server check failed:', error);
return false;
}
}

test('share-target redirects with URL parameter', async ({ page }) => {
// Check if the server is up
const serverUp = await isServerUp(page);
console.log('Is server up?', serverUp);

const sharedUrl = 'https://example.com/shared-page';
await page.goto(`share-target.html?url=${encodeURIComponent(sharedUrl)}`);

// Wait for navigation to complete
await page.waitForURL(`index.html?url=${encodeURIComponent(sharedUrl)}`);
// Wait for navigation to complete with a shorter timeout
await page.waitForNavigation({ timeout: 5000 }).catch(e => console.log('Navigation timeout:', e));

// Log the current URL for debugging
console.log('Current URL:', page.url());

// Check if we've been redirected to the correct URL
expect(page.url()).toContain(`index.html?url=${encodeURIComponent(sharedUrl)}`);
expect(page.url()).toContain(`index.html`);
expect(page.url()).toContain(`url=${encodeURIComponent(sharedUrl)}`);
});

test('share-target redirects to index.html when no URL is shared', async ({ page }) => {
// Check if the server is up
const serverUp = await isServerUp(page);
console.log('Is server up?', serverUp);

await page.goto(`share-target.html`);

// Wait for navigation to complete
await page.waitForURL(`index.html`);
// Wait for navigation to complete with a shorter timeout
await page.waitForNavigation({ timeout: 5000 }).catch(e => console.log('Navigation timeout:', e));

// Log the current URL for debugging
console.log('Current URL:', page.url());

// Check if we've been redirected to the index.html page
expect(page.url()).toContain(`index.html`);
Expand Down
2 changes: 1 addition & 1 deletion playwright.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { defineConfig } = require('@playwright/test');
module.exports = defineConfig({
testDir: './e2e',
use: {
baseURL: 'http://localhost:3000/listen-UI/',
baseURL: 'http://localhost:5173/listen-UI/',
},
webServer: {
command: 'npm run dev',
Expand Down
8 changes: 4 additions & 4 deletions public/share-target.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This function will be called when the PWA receives a share
export function handleShareTarget() {
function handleShareTarget() {
const urlParams = new URLSearchParams(window.location.search);
const sharedUrl = urlParams.get('url') || urlParams.get('text');

Expand All @@ -12,7 +12,7 @@ export function handleShareTarget() {
}
}

// Call the function when the page loads
if (typeof window !== 'undefined') {
// // Call the function when the page loads
// if (typeof window !== 'undefined') {
handleShareTarget();
}
// }

0 comments on commit 235a440

Please sign in to comment.