|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack-ai/objectstack#27398 row 2 — an EMPTY currency box reads as |
| 11 | + * empty. |
| 12 | + * |
| 13 | + * The edit-mode input used to fall back to the placeholder `0.00` whenever the |
| 14 | + * field declared none, so an optional amount nobody filled in (a public form's |
| 15 | + * Annual Revenue) looked like a filled-in zero. The widget now shows the |
| 16 | + * field's own `placeholder`, or none — the shape `NumberField` already has. |
| 17 | + * |
| 18 | + * Every host draws this one widget with the field it was handed, so the pins |
| 19 | + * below cover the widget itself and the two package hosts that mount it: the |
| 20 | + * form renderer (record form) and `FieldEditWidget` (the grid's inline |
| 21 | + * editor). The public form page's own pin lives beside `FormPage.tsx`. |
| 22 | + */ |
| 23 | + |
| 24 | +import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest'; |
| 25 | +import React from 'react'; |
| 26 | +import { render, cleanup } from '@testing-library/react'; |
| 27 | +import '@testing-library/jest-dom'; |
| 28 | +import { ComponentRegistry } from '@object-ui/core'; |
| 29 | +// Module scope: pulls in the form renderer's registration side effect. |
| 30 | +import '@object-ui/components'; |
| 31 | +import { LocalizationProvider } from '@object-ui/i18n'; |
| 32 | +import { CurrencyField } from '../widgets/CurrencyField'; |
| 33 | +import { FieldEditWidget } from '../FieldEditWidget'; |
| 34 | + |
| 35 | +beforeAll(() => { |
| 36 | + ComponentRegistry.register('currency', CurrencyField as any, { |
| 37 | + namespace: 'field', |
| 38 | + skipFallback: true, |
| 39 | + }); |
| 40 | +}, 30000); |
| 41 | + |
| 42 | +afterEach(() => cleanup()); |
| 43 | + |
| 44 | +const box = (container: HTMLElement): HTMLInputElement => { |
| 45 | + const el = container.querySelector('input[type=number]'); |
| 46 | + if (!el) throw new Error('no currency input rendered'); |
| 47 | + return el as HTMLInputElement; |
| 48 | +}; |
| 49 | + |
| 50 | +/** The attribute as the browser would show it: absent and empty read alike. */ |
| 51 | +const shownPlaceholder = (container: HTMLElement) => box(container).getAttribute('placeholder') ?? ''; |
| 52 | + |
| 53 | +const renderWidget = (field: Record<string, unknown>, value: number | null = null) => |
| 54 | + render( |
| 55 | + <LocalizationProvider value={{ locale: 'en' }}> |
| 56 | + <CurrencyField value={value as any} onChange={vi.fn()} field={{ type: 'currency', ...field } as any} /> |
| 57 | + </LocalizationProvider>, |
| 58 | + ); |
| 59 | + |
| 60 | +describe('CurrencyField — an empty box shows no numeric stand-in (objectstack#27398 row 2)', () => { |
| 61 | + it('an empty field that declares no placeholder shows none', () => { |
| 62 | + const { container } = renderWidget({ name: 'annual_revenue', currency: 'USD' }); |
| 63 | + expect(box(container).value).toBe(''); |
| 64 | + expect(shownPlaceholder(container)).toBe(''); |
| 65 | + }); |
| 66 | + |
| 67 | + it('a zero-decimal currency is not offered a two-decimal stand-in either', () => { |
| 68 | + const { container } = renderWidget({ name: 'budget', currency: 'JPY' }); |
| 69 | + expect(shownPlaceholder(container)).toBe(''); |
| 70 | + }); |
| 71 | + |
| 72 | + it('an authored empty placeholder stays empty rather than being replaced', () => { |
| 73 | + const { container } = renderWidget({ name: 'annual_revenue', currency: 'USD', placeholder: '' }); |
| 74 | + expect(shownPlaceholder(container)).toBe(''); |
| 75 | + }); |
| 76 | + |
| 77 | + it("CONTROL — the field's own placeholder is still shown", () => { |
| 78 | + const { container } = renderWidget({ name: 'annual_revenue', currency: 'USD', placeholder: 'e.g. 250000' }); |
| 79 | + expect(shownPlaceholder(container)).toBe('e.g. 250000'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('CONTROL — a real zero still shows as a value', () => { |
| 83 | + const { container } = renderWidget({ name: 'annual_revenue', currency: 'USD' }, 0); |
| 84 | + expect(box(container).value).toBe('0'); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('the hosts that mount CurrencyField hand it no stand-in of their own (objectstack#27398 row 2)', () => { |
| 89 | + it('the form renderer (record form) draws an empty optional amount with no placeholder', () => { |
| 90 | + const Form = ComponentRegistry.get('form')!; |
| 91 | + const { container } = render( |
| 92 | + <Form |
| 93 | + schema={{ |
| 94 | + type: 'form', |
| 95 | + mode: 'create', |
| 96 | + showSubmit: true, |
| 97 | + showCancel: false, |
| 98 | + defaultValues: {}, |
| 99 | + fields: [{ name: 'annual_revenue', label: 'Annual Revenue', type: 'currency', currency: 'USD' }], |
| 100 | + onSubmit: () => {}, |
| 101 | + }} |
| 102 | + />, |
| 103 | + ); |
| 104 | + const row = container.querySelector('[data-field="annual_revenue"]') as HTMLElement | null; |
| 105 | + if (!row) throw new Error('the form renderer drew no annual_revenue row'); |
| 106 | + expect(shownPlaceholder(row)).toBe(''); |
| 107 | + }); |
| 108 | + |
| 109 | + it('FieldEditWidget (the inline editor) draws an empty amount with no placeholder', () => { |
| 110 | + const { container } = render( |
| 111 | + <FieldEditWidget |
| 112 | + field={{ name: 'annual_revenue', type: 'currency', label: 'Annual Revenue', currency: 'USD' } as never} |
| 113 | + value={null} |
| 114 | + onChange={() => {}} |
| 115 | + />, |
| 116 | + ); |
| 117 | + expect(shownPlaceholder(container)).toBe(''); |
| 118 | + }); |
| 119 | +}); |
0 commit comments