Files
ipal-kit/test/totpTextsAndCookieButton.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 2886c89d8a 2FA screens: plain en/pl texts and tab title; CookieButton moves up above the footer
- TOTP_TEXTS (admin-ux.md wording) merged over @clocklimited/payload-2fa's
  long, informal texts; the project's i18n.translations still win;
  twoFactor.texts: false keeps the package's. Setup/verify screens titled
  "Two-step verification" / "Weryfikacja dwuetapowa" instead of "Payload".
- CookieButton: position via --ipal-cookie-button-offset / -inset and
  --ipal-z-header; lifts above the footer (avoid, default COOKIE_BUTTON_AVOID)
  while it is on screen; side prop; className still replaces the look.
- dev: a footer in the frontend layout to check the button against.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-29 15:34:01 +01:00

81 lines
3.6 KiB
TypeScript

import type { Config } from 'payload'
import { describe, expect, test } from 'vitest'
import { bottomOverlap } from '../src/modules/consent/CookieButton.js'
import { TOTP_TEXTS } from '../src/modules/security/totp/totpTexts.js'
import { ipalKit } from '../src/plugin.js'
const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }, { code: 'en', label: 'English' }] }
/** Stand-in for @clocklimited/payload-2fa: its own texts and the two views, as the package adds them. */
const fakeTotp = () => (config: Config) => ({
...config,
admin: {
...config.admin,
components: {
...config.admin?.components,
views: {
...config.admin?.components?.views,
SetupTOTP: { Component: { path: 'pkg#TOTPSetup' }, path: 'setup-totp' },
VerifyTOTP: { Component: { path: 'pkg#TOTPVerify' }, path: 'verify-totp' },
},
},
},
i18n: {
...config.i18n,
translations: {
...config.i18n?.translations,
en: { ...config.i18n?.translations?.en, totpPlugin: { verify: { title: 'Verification' } } },
pl: { ...config.i18n?.translations?.pl, totpPlugin: { verify: { title: 'Weryfikacja' } } },
},
},
})
const base = (translations?: Record<string, unknown>): Config =>
({ collections: [{ slug: 'users', auth: true, fields: [] }], i18n: translations ? { translations } : undefined, secret: 'test' }) as unknown as Config
type Translations = Record<string, { totpPlugin: { setup: Record<string, string>; verify: { title: string } } }>
describe('2FA texts', () => {
test('ipal-kit texts go over the package, the project over both; screens get a tab title', async () => {
const config = await ipalKit({ i18n, twoFactor: { issuer: 'Acme', plugin: fakeTotp } } as never)(
base({ en: { totpPlugin: { verify: { title: 'Project check' } } } }),
)
const t = config.i18n!.translations as Translations
expect(t.pl.totpPlugin.verify.title).toBe('Weryfikacja dwuetapowa')
expect(t.pl.totpPlugin.setup.enterCode).toBe('Wpisz {digits}-cyfrowy kod z aplikacji:')
expect(t.en.totpPlugin.verify.title).toBe('Project check')
const views = config.admin!.components!.views as Record<string, { meta?: { title?: string } }>
expect(views.VerifyTOTP.meta?.title).toBe('Weryfikacja dwuetapowa')
expect(views.SetupTOTP.meta?.title).toBe('Weryfikacja dwuetapowa')
})
test("texts: false keeps the package's", async () => {
const config = await ipalKit({ i18n, twoFactor: { issuer: 'Acme', plugin: fakeTotp, texts: false } } as never)(base())
expect((config.i18n!.translations as Translations).pl.totpPlugin.verify.title).toBe('Weryfikacja')
})
test('same keys in English and Polish, short and formal', () => {
const keys = (o: object, p = ''): string[] =>
Object.entries(o).flatMap(([k, v]) => (typeof v === 'object' ? keys(v as object, `${p}${k}.`) : [`${p}${k}`]))
expect(keys(TOTP_TEXTS.pl)).toEqual(keys(TOTP_TEXTS.en))
const all = [...Object.values(TOTP_TEXTS.pl.totpPlugin.setup), TOTP_TEXTS.pl.totpPlugin.fieldDescription]
expect(all.some((s) => /\b(?:Tw[oó]j|Będziesz|Masz)\b/.test(s))).toBe(false)
expect(TOTP_TEXTS.en.totpPlugin.forceTotp.label.split(' ').length).toBeLessThanOrEqual(3)
})
})
describe('CookieButton lift above the footer', () => {
test('moves up by the visible part of the footer', () => {
expect(bottomOverlap([], 800)).toBe(0)
expect(bottomOverlap([1200], 800)).toBe(0) // footer below the fold
expect(bottomOverlap([650], 800)).toBe(150)
expect(bottomOverlap([700, 650], 800)).toBe(150) // the highest one counts
})
test('never pushes the button off the screen', () => {
expect(bottomOverlap([-500], 800)).toBe(704)
})
})