Files
ipal-kit/test/plugin.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 38c1d9215e enabled: false keeps the plugin's schema, drops only its behaviour
Returning the incoming config dropped the plugin's collections and globals,
so a Postgres migration would drop their tables. Now collections, globals
and fields stay; endpoints, onInit and admin components are left out.
Also: ipalKit({ forms: { consentFieldName } }) is stored in config.custom.ipal.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 17:35:08 +02:00

89 lines
3.9 KiB
TypeScript

import type { Config } from 'payload'
import { describe, expect, test, vi } from 'vitest'
import type { I18nConfig } from '../src/modules/i18n/types.js'
import { ipalKit } from '../src/plugin.js'
const i18n: I18nConfig = {
defaultLocale: 'pl',
locales: [
{ code: 'pl', label: 'Polski' },
{ code: 'en', label: 'English' },
],
}
const base = (): Config =>
({
collections: [{ slug: 'users', auth: true, fields: [] }],
secret: 'test',
}) as unknown as Config
describe('ipalKit — config transform', () => {
test('adds globals, endpoint, localization and roles', async () => {
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(base())
expect(config.globals?.map((g) => g.slug)).toEqual(
expect.arrayContaining(['site-settings', 'site-integrations']),
)
expect(config.endpoints?.some((e) => e.path === '/ipal/test-email')).toBe(true)
expect(config.localization && config.localization.defaultLocale).toBe('pl')
const users = config.collections?.find((c) => c.slug === 'users')
expect(users?.fields.some((f) => 'name' in f && f.name === 'roles')).toBe(true)
})
test('enforces 2FA unless explicitly disabled', async () => {
await expect(ipalKit({ i18n } as never)(base())).rejects.toThrow(/twoFactor\.issuer is required/)
await expect(ipalKit({ i18n, twoFactor: { issuer: 'Acme' } } as never)(base())).rejects.toThrow(
/no 2FA plugin was injected/,
)
const totp = vi.fn(() => (c: Config) => c)
await ipalKit({ i18n, twoFactor: { issuer: 'Acme', plugin: totp as never } })(base())
expect(totp).toHaveBeenCalledWith(expect.objectContaining({ forceSetup: true, totp: { issuer: 'Acme' } }))
})
test('enabled: false keeps the schema (collections, globals) but adds no endpoints, onInit or admin components', async () => {
const config = await ipalKit({ access: { authCollection: 'users' }, consentLog: true, enabled: false, i18n, stats: true, twoFactor: false })(base())
expect(config.globals?.map((g) => g.slug)).toEqual(expect.arrayContaining(['site-settings', 'site-integrations', 'cookie-settings', 'notifications']))
expect(config.collections?.map((c) => c.slug)).toEqual(expect.arrayContaining(['users', 'consent-records', 'stats-visits']))
expect(config.endpoints ?? []).toEqual([])
expect(config.onInit).toBeUndefined()
expect(config.admin?.components).toBeUndefined()
})
test('forms.consentFieldName reaches submitForm through config.custom.ipal', async () => {
const config = await ipalKit({ forms: { consentFieldName: 'zgoda' }, i18n, twoFactor: false })(base())
expect((config.custom as { ipal?: { consentFieldName?: string } }).ipal?.consentFieldName).toBe('zgoda')
})
test('invalid i18n fails fast', () => {
expect(() => ipalKit({ i18n: { defaultLocale: 'de', locales: i18n.locales }, twoFactor: false })).toThrow()
})
})
describe('ipalKit — auth collection', () => {
test('injects a users auth collection when the project has none, with roles', async () => {
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })({
collections: [],
secret: 'x',
} as unknown as Config)
const users = config.collections?.find((c) => c.slug === 'users')
expect(users?.auth).toBe(true)
expect(users?.fields.some((f) => 'name' in f && f.name === 'roles')).toBe(true)
})
test("keeps the project's own users collection", async () => {
const own = { slug: 'users', auth: true, fields: [{ name: 'fullName', type: 'text' }] }
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })({
collections: [own],
secret: 'x',
} as unknown as Config)
const users = config.collections?.filter((c) => c.slug === 'users')
expect(users).toHaveLength(1)
expect(users?.[0].fields.some((f) => 'name' in f && f.name === 'fullName')).toBe(true)
})
})