feat(frontend): pass locale to iframe embedded pages via lang parameter

Embedded pages (purchase subscription, custom pages) now receive the
current user locale through a `lang` URL parameter, allowing iframe
content to match the user's language preference.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
erio
2026-03-09 19:38:23 +08:00
parent c8eff34388
commit ebc6755b33
5 changed files with 85 additions and 10 deletions

View File

@@ -0,0 +1,67 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { buildEmbeddedUrl, detectTheme } from '../embedded-url'
describe('embedded-url', () => {
const originalLocation = window.location
beforeEach(() => {
Object.defineProperty(window, 'location', {
value: {
origin: 'https://app.example.com',
href: 'https://app.example.com/user/purchase',
},
writable: true,
configurable: true,
})
})
afterEach(() => {
Object.defineProperty(window, 'location', {
value: originalLocation,
writable: true,
configurable: true,
})
document.documentElement.classList.remove('dark')
vi.restoreAllMocks()
})
it('adds embedded query parameters including locale and source context', () => {
const result = buildEmbeddedUrl(
'https://pay.example.com/checkout?plan=pro',
42,
'token-123',
'dark',
'zh-CN',
)
const url = new URL(result)
expect(url.searchParams.get('plan')).toBe('pro')
expect(url.searchParams.get('user_id')).toBe('42')
expect(url.searchParams.get('token')).toBe('token-123')
expect(url.searchParams.get('theme')).toBe('dark')
expect(url.searchParams.get('lang')).toBe('zh-CN')
expect(url.searchParams.get('ui_mode')).toBe('embedded')
expect(url.searchParams.get('src_host')).toBe('https://app.example.com')
expect(url.searchParams.get('src_url')).toBe('https://app.example.com/user/purchase')
})
it('omits optional params when they are empty', () => {
const result = buildEmbeddedUrl('https://pay.example.com/checkout', undefined, '', 'light')
const url = new URL(result)
expect(url.searchParams.get('theme')).toBe('light')
expect(url.searchParams.get('ui_mode')).toBe('embedded')
expect(url.searchParams.has('user_id')).toBe(false)
expect(url.searchParams.has('token')).toBe(false)
expect(url.searchParams.has('lang')).toBe(false)
})
it('returns original string for invalid url input', () => {
expect(buildEmbeddedUrl('not a url', 1, 'token')).toBe('not a url')
})
it('detects dark mode from document root class', () => {
document.documentElement.classList.add('dark')
expect(detectTheme()).toBe('dark')
})
})