|
| 1 | +// Copyright (c) 2026 ObjectStack contributors. Apache-2.0 license. |
| 2 | +// |
| 3 | +// The extractor half of `action.cancelText`, the label of the dismiss button on |
| 4 | +// the dialog an action opens before it runs. |
| 5 | +// |
| 6 | +// `translateAction` overlays `objects.OBJECT._actions.ACTION.cancelText` |
| 7 | +// (`globalActions.ACTION.cancelText` for an object-less action) only onto an |
| 8 | +// action that opens one of the two dialogs carrying that button: the confirm |
| 9 | +// dialog of a declared `confirmText`, or the param dialog of a non-empty |
| 10 | +// `params`. `os i18n extract` scaffolds the slot on exactly that population: |
| 11 | +// there it is an optional slot like `submitText`, and anywhere else there is no |
| 12 | +// slot at all. The last test walks ONE fixture action through all three |
| 13 | +// surfaces — `ActionSchema` accepts it, the extractor emits its key, the |
| 14 | +// translation schema accepts the bundle written at that key — because each half |
| 15 | +// passing alone does not prove the addresses agree. |
| 16 | + |
| 17 | +import { describe, it, expect } from 'vitest'; |
| 18 | +import { collectExpectedEntries, extractTranslations } from '../src/utils/i18n-extract'; |
| 19 | +import { computeI18nCoverage } from '../src/utils/i18n-coverage'; |
| 20 | +import { ActionSchema } from '@objectstack/spec/ui'; |
| 21 | +import { ObjectTranslationDataSchema } from '@objectstack/spec/system'; |
| 22 | + |
| 23 | +const paths = (config: any) => collectExpectedEntries(config).map((e) => e.path.join('.')); |
| 24 | + |
| 25 | +const PARAMS = [{ name: 'reason', label: 'Reason', type: 'text' }]; |
| 26 | + |
| 27 | +describe('the extractor emits action-level `cancelText` beside `confirmText` or a non-empty `params`', () => { |
| 28 | + it('emits it for a TOP-LEVEL object action with a confirm dialog', () => { |
| 29 | + const keys = paths({ |
| 30 | + actions: [{ |
| 31 | + name: 'cancel_invitation', objectName: 'sys_invitation', label: 'Cancel Invitation', |
| 32 | + confirmText: 'Cancel this invitation?', cancelText: 'Keep invitation', |
| 33 | + }], |
| 34 | + }); |
| 35 | + |
| 36 | + expect(keys).toContain('objects.sys_invitation._actions.cancel_invitation.cancelText'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('emits it for a GLOBAL action with a param dialog', () => { |
| 40 | + const keys = paths({ |
| 41 | + actions: [{ name: 'close_period', label: 'Close Period', params: PARAMS, cancelText: 'Keep open' }], |
| 42 | + }); |
| 43 | + |
| 44 | + expect(keys).toContain('globalActions.close_period.cancelText'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('emits it for an action declared INLINE on the object', () => { |
| 48 | + const keys = paths({ |
| 49 | + objects: [ |
| 50 | + { |
| 51 | + name: 'sys_invitation', |
| 52 | + label: 'Invitation', |
| 53 | + actions: [{ name: 'cancel_invitation', label: 'Cancel Invitation', confirmText: 'Sure?', cancelText: 'Keep invitation' }], |
| 54 | + }, |
| 55 | + ], |
| 56 | + }); |
| 57 | + |
| 58 | + expect(keys).toContain('objects.sys_invitation._actions.cancel_invitation.cancelText'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('records a seed-less slot for a dialog action that names no label — the dialog keeps "Cancel"', () => { |
| 62 | + const config = { |
| 63 | + objects: [{ name: 'sys_invitation', label: 'Invitation', actions: [{ name: 'resend', label: 'Resend', confirmText: 'Resend it?' }] }], |
| 64 | + }; |
| 65 | + const key = 'objects.sys_invitation._actions.resend.cancelText'; |
| 66 | + const entry = collectExpectedEntries(config).find((e) => e.path.join('.') === key); |
| 67 | + |
| 68 | + expect(entry).toBeDefined(); |
| 69 | + expect(entry?.inline).toBeUndefined(); |
| 70 | + expect(entry?.sourceValue).toBeUndefined(); |
| 71 | + |
| 72 | + const { bundles } = extractTranslations(config, { defaultLocale: 'en', fill: 'default' }); |
| 73 | + expect(bundles.en.objects?.sys_invitation?._actions?.resend).not.toHaveProperty('cancelText'); |
| 74 | + expect(computeI18nCoverage({ ...config, translations: [{ 'zh-CN': {} }] }).issues.map((i) => i.key)) |
| 75 | + .not.toContain(key); |
| 76 | + }); |
| 77 | + |
| 78 | + it('emits NO slot for an action that opens no dialog — neither `confirmText` nor a non-empty `params`', () => { |
| 79 | + for (const params of [undefined, []]) { |
| 80 | + const keys = paths({ |
| 81 | + objects: [ |
| 82 | + { |
| 83 | + name: 'sys_invitation', |
| 84 | + label: 'Invitation', |
| 85 | + actions: [{ name: 'archive', label: 'Archive', ...(params ? { params } : {}) }], |
| 86 | + }, |
| 87 | + ], |
| 88 | + actions: [{ name: 'refresh_all', label: 'Refresh', ...(params ? { params } : {}) }], |
| 89 | + }); |
| 90 | + |
| 91 | + expect(keys).toContain('objects.sys_invitation._actions.archive.label'); |
| 92 | + expect(keys).not.toContain('objects.sys_invitation._actions.archive.cancelText'); |
| 93 | + expect(keys).toContain('globalActions.refresh_all.label'); |
| 94 | + expect(keys).not.toContain('globalActions.refresh_all.cancelText'); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + it('carries the declared label into the bundle, and gates it until it is translated', () => { |
| 99 | + const config: any = { |
| 100 | + objects: [ |
| 101 | + { |
| 102 | + name: 'sys_invitation', |
| 103 | + label: 'Invitation', |
| 104 | + actions: [{ name: 'cancel_invitation', label: 'Cancel Invitation', confirmText: 'Sure?', cancelText: 'Keep invitation' }], |
| 105 | + }, |
| 106 | + ], |
| 107 | + translations: [ |
| 108 | + { 'zh-CN': { objects: { sys_invitation: { label: '邀请', _actions: { cancel_invitation: { label: '取消邀请', confirmText: '确定?' } } } } } }, |
| 109 | + ], |
| 110 | + }; |
| 111 | + const key = 'objects.sys_invitation._actions.cancel_invitation.cancelText'; |
| 112 | + |
| 113 | + const entry = collectExpectedEntries(config).find((e) => e.path.join('.') === key); |
| 114 | + expect(entry?.source).toBe('action'); |
| 115 | + expect(entry?.inline).toBe('Keep invitation'); |
| 116 | + |
| 117 | + const { bundles } = extractTranslations(config, { defaultLocale: 'en', fill: 'default' }); |
| 118 | + expect(bundles.en.objects?.sys_invitation?._actions?.cancel_invitation.cancelText).toBe('Keep invitation'); |
| 119 | + |
| 120 | + expect(computeI18nCoverage(config).issues.map((i) => i.key)).toContain(key); |
| 121 | + config.translations[0]['zh-CN'].objects.sys_invitation._actions.cancel_invitation.cancelText = '保留邀请'; |
| 122 | + expect(computeI18nCoverage(config).issues.map((i) => i.key)).not.toContain(key); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('one fixture action, all three surfaces (cancelText end-to-end)', () => { |
| 127 | + const authored = { |
| 128 | + name: 'cancel_invitation', |
| 129 | + objectName: 'sys_invitation', |
| 130 | + label: 'Cancel Invitation', |
| 131 | + type: 'script', |
| 132 | + target: 'cancelInvitation', |
| 133 | + confirmText: 'Cancel this invitation?', |
| 134 | + cancelText: 'Keep invitation', |
| 135 | + }; |
| 136 | + |
| 137 | + it('parses as metadata, extracts to a key, and that key round-trips a bundle', () => { |
| 138 | + const parsed = ActionSchema.safeParse(authored); |
| 139 | + expect(parsed.success).toBe(true); |
| 140 | + expect(parsed.success && parsed.data.cancelText).toBe('Keep invitation'); |
| 141 | + |
| 142 | + const { bundles } = extractTranslations({ actions: [authored] }, { |
| 143 | + defaultLocale: 'en', |
| 144 | + locales: ['zh-CN'], |
| 145 | + fill: 'empty', |
| 146 | + }); |
| 147 | + const bundle: any = bundles['zh-CN']; |
| 148 | + expect(bundle.objects.sys_invitation._actions.cancel_invitation).toHaveProperty('cancelText'); |
| 149 | + |
| 150 | + bundle.objects.sys_invitation._actions.cancel_invitation.cancelText = '保留邀请'; |
| 151 | + const translated = ObjectTranslationDataSchema.safeParse(bundle.objects.sys_invitation); |
| 152 | + expect(translated.success).toBe(true); |
| 153 | + expect(translated.success && translated.data._actions?.cancel_invitation?.cancelText).toBe('保留邀请'); |
| 154 | + }); |
| 155 | +}); |
0 commit comments