|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | + |
| 3 | +// Set up browser globals before any imports that might need them |
| 4 | +// @ts-ignore - minimal mock for testing |
| 5 | +global.location = { |
| 6 | + origin: 'https://playground.wordpress.net', |
| 7 | + href: 'https://playground.wordpress.net/', |
| 8 | +}; |
| 9 | +// @ts-ignore - minimal mock for testing |
| 10 | +global.window = global.window || {}; |
| 11 | +// @ts-ignore |
| 12 | +global.window.self = global.window; |
| 13 | +// @ts-ignore |
| 14 | +global.window.top = global.window; |
| 15 | + |
| 16 | +// Mock fetch for potential network requests |
| 17 | +global.fetch = vi.fn(); |
| 18 | + |
| 19 | +// Now we can import the module |
| 20 | +const { resolveUrlParamsForExistingSite } = await import('./index'); |
| 21 | + |
| 22 | +describe('resolveUrlParamsForExistingSite', () => { |
| 23 | + it('returns null when no actionable URL params are present', async () => { |
| 24 | + const url = new URL('https://playground.wordpress.net/'); |
| 25 | + const result = await resolveUrlParamsForExistingSite(url); |
| 26 | + expect(result).toBeNull(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns null for non-actionable params like ?mode=', async () => { |
| 30 | + const url = new URL('https://playground.wordpress.net/?mode=seamless'); |
| 31 | + const result = await resolveUrlParamsForExistingSite(url); |
| 32 | + expect(result).toBeNull(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns blueprint with plugins property for ?plugin= param', async () => { |
| 36 | + const url = new URL( |
| 37 | + 'https://playground.wordpress.net/?plugin=woocommerce' |
| 38 | + ); |
| 39 | + const result = await resolveUrlParamsForExistingSite(url); |
| 40 | + |
| 41 | + expect(result).not.toBeNull(); |
| 42 | + expect(result?.plugins).toEqual(['woocommerce']); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns blueprint with multiple plugins for multiple ?plugin= params', async () => { |
| 46 | + const url = new URL( |
| 47 | + 'https://playground.wordpress.net/?plugin=woocommerce&plugin=jetpack' |
| 48 | + ); |
| 49 | + const result = await resolveUrlParamsForExistingSite(url); |
| 50 | + |
| 51 | + expect(result).not.toBeNull(); |
| 52 | + expect(result?.plugins).toEqual(['woocommerce', 'jetpack']); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns blueprint with installTheme steps for ?theme= param', async () => { |
| 56 | + const url = new URL('https://playground.wordpress.net/?theme=flavor'); |
| 57 | + const result = await resolveUrlParamsForExistingSite(url); |
| 58 | + |
| 59 | + expect(result).not.toBeNull(); |
| 60 | + expect(result?.steps).toBeDefined(); |
| 61 | + |
| 62 | + const themeStep = result?.steps?.find( |
| 63 | + (step: any) => step?.step === 'installTheme' |
| 64 | + ); |
| 65 | + expect(themeStep).toBeDefined(); |
| 66 | + expect((themeStep as any)?.themeData?.slug).toBe('flavor'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('sets landingPage from ?url= param via applyQueryOverrides', async () => { |
| 70 | + const url = new URL( |
| 71 | + 'https://playground.wordpress.net/?plugin=woocommerce&url=/wp-admin/plugins.php' |
| 72 | + ); |
| 73 | + const result = await resolveUrlParamsForExistingSite(url); |
| 74 | + |
| 75 | + expect(result).not.toBeNull(); |
| 76 | + expect(result?.landingPage).toBe('/wp-admin/plugins.php'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('returns null for ?gutenberg-pr= (not supported for existing sites)', async () => { |
| 80 | + const url = new URL( |
| 81 | + 'https://playground.wordpress.net/?gutenberg-pr=12345' |
| 82 | + ); |
| 83 | + const result = await resolveUrlParamsForExistingSite(url); |
| 84 | + expect(result).toBeNull(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments