|
| 1 | +import { test, expect } from '../../fixtures.js' |
| 2 | +import { ADMIN_HANDLE } from '../../support/i18nHelpers.js' |
| 3 | + |
| 4 | +// Regression coverage for PR #304. |
| 5 | +// |
| 6 | +// The bug: FontAwesome::maybe_enqueue_admin_assets() called |
| 7 | +// wp_set_script_translations() for the admin bundle BEFORE the script was |
| 8 | +// registered/enqueued, so WordPress never attached the textdomain, never printed |
| 9 | +// the setLocaleData bridge, and the React admin UI never received its |
| 10 | +// translations. The fix moved the call to run right after |
| 11 | +// enqueue_admin_js_assets(). |
| 12 | +// |
| 13 | +// setup/i18n.js forces the locale to de_DE and installs a fixture translation |
| 14 | +// file, so with the fix in place these assertions pass; if the bug regresses the |
| 15 | +// bridge disappears and the strings stay in English, failing the test. |
| 16 | +test.describe('admin JS translations (PR #304)', () => { |
| 17 | + test.beforeEach(async ({ page }) => { |
| 18 | + await page.goto('/wp-admin/admin.php?page=font-awesome') |
| 19 | + }) |
| 20 | + |
| 21 | + test('prints the script-translations bridge for the admin bundle', async ({ page }) => { |
| 22 | + // The admin bundle must be on the page, otherwise the rest is meaningless. |
| 23 | + await expect(page.locator(`#${ADMIN_HANDLE}-js`)).toHaveCount(1) |
| 24 | + |
| 25 | + // The heart of the fix: WordPress only prints this inline setLocaleData |
| 26 | + // script when wp_set_script_translations() ran AFTER registration. It's a |
| 27 | + // <script> element (no rendered text), so read its textContent directly. |
| 28 | + const bridge = page.locator(`#${ADMIN_HANDLE}-js-translations`) |
| 29 | + await expect(bridge).toHaveCount(1) |
| 30 | + const bridgeSource = await bridge.evaluate((el) => el.textContent) |
| 31 | + expect(bridgeSource).toContain('wp.i18n.setLocaleData') |
| 32 | + expect(bridgeSource).toContain('font-awesome') |
| 33 | + }) |
| 34 | + |
| 35 | + test('renders translated strings in the admin UI', async ({ page }) => { |
| 36 | + await expect( |
| 37 | + page.getByRole('heading', { name: 'TEST-DE — Wie verwendest du Font Awesome?' }) |
| 38 | + ).toBeVisible() |
| 39 | + await expect(page.getByText('TEST-DE — Kit verwenden')).toBeVisible() |
| 40 | + await expect(page.getByText('TEST-DE — CDN verwenden')).toBeVisible() |
| 41 | + }) |
| 42 | + |
| 43 | + test('resolves the plugin textdomain through the wp.i18n runtime', async ({ page }) => { |
| 44 | + const translated = await page.evaluate(() => |
| 45 | + window.wp.i18n.__('How are you using Font Awesome?', 'font-awesome') |
| 46 | + ) |
| 47 | + expect(translated).toBe('TEST-DE — Wie verwendest du Font Awesome?') |
| 48 | + }) |
| 49 | +}) |
0 commit comments