Skip to content

Commit

Permalink
✨ [#2] currency tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Oct 26, 2023
1 parent 5079107 commit 239043d
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/formio/components/currency.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {InputComponentSchema, MultipleCapable, PrefillConfig} from '..';

type Validator = 'required' | 'maxLength' | 'pattern';
type TranslatableKeys = 'label' | 'description' | 'tooltip' | 'defaultValue' | 'placeholder';
type Validator = 'required' | 'min' | 'max';
type TranslatableKeys = 'label' | 'description' | 'tooltip' | 'suffix';
type AvailableCurrencies =
| 'USD'
| 'EUR'
Expand Down Expand Up @@ -185,7 +185,7 @@ export interface BaseCurrencyComponentSchema
PrefillConfig {
type: 'currency';
// additional properties
currency?: AvailableCurrencies;
currency: AvailableCurrencies;
decimalLimit?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
allowNegative?: boolean;
// re-add
Expand Down
1 change: 1 addition & 0 deletions src/formio/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export * from './checkbox';
export * from './textfield';
export * from './email';
export * from './currency';
export * from './date';
export * from './datetime';
export * from './time';
Expand Down
2 changes: 2 additions & 0 deletions src/formio/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CheckboxComponentSchema,
ContentComponentSchema,
CurrencyComponentSchema,
DateComponentSchema,
DateTimeComponentSchema,
EmailComponentSchema,
Expand Down Expand Up @@ -51,6 +52,7 @@ export type AnyComponentSchema =
| CheckboxComponentSchema
| SelectboxesComponentSchema
| RadioComponentSchema
| CurrencyComponentSchema
// layout
| ContentComponentSchema;

Expand Down
136 changes: 136 additions & 0 deletions test-d/formio/components/currency.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import {expectAssignable, expectNotAssignable} from 'tsd';

import {CurrencyComponentSchema} from '../../../lib/';

// minimal currency component schema
expectAssignable<CurrencyComponentSchema>({
id: '123',
type: 'currency',
key: 'aCurrency',
label: 'A currency',
currency: 'EUR',
} as const);

// with additional, number-component specific properties
expectAssignable<CurrencyComponentSchema>({
id: '123',
type: 'currency',
key: 'aCurrency',
label: 'A currency',
currency: 'EUR',
decimalLimit: 3,
allowNegative: true,
} as const);

// multiple false (implicit) and appropriate default value type
expectAssignable<CurrencyComponentSchema>({
id: '123',
type: 'currency',
key: 'aCurrency',
label: 'A currency',
currency: 'EUR',
defaultValue: 3,
} as const);

// different component type
expectNotAssignable<CurrencyComponentSchema>({
type: 'textfield',
} as const);

// using unsupported properties
expectNotAssignable<CurrencyComponentSchema>({
id: '123',
type: 'currency',
key: 'aCurrency',
label: 'A currency',
currency: 'EUR',
hideLabel: true,
} as const);

// no multiple support -> no array defaultValue
expectNotAssignable<CurrencyComponentSchema>({
id: '123',
type: 'currency',
key: 'aCurrency',
label: 'A currency',
currency: 'EUR',
defaultValue: [],
} as const);

expectNotAssignable<CurrencyComponentSchema>({
id: '123',
type: 'currency',
key: 'aCurrency',
label: 'A currency',
currency: 'EUR',
multiple: true,
defaultValue: [],
} as const);

// invalid, only the number validators may be assignable
expectNotAssignable<CurrencyComponentSchema>({
id: '123',
type: 'currency',
key: 'aCurrency',
label: 'A currency',
currency: 'EUR',
validate: {
maxLength: 100,
},
} as const);

// full, correct schema
expectAssignable<CurrencyComponentSchema>({
id: '8aosjaw',
type: 'currency',
// basic tab in builder form
label: 'A sample currency',
currency: 'EUR',
key: 'test',
description: 'Sample description',
tooltip: 'A tooltip',
showInSummary: true,
showInEmail: false,
showInPDF: true,
// multiple: false,
hidden: false,
clearOnHide: true,
isSensitiveData: false,
defaultValue: undefined,
decimalLimit: undefined,
allowNegative: false,
// advanced tab in builder form
conditional: {
show: undefined,
when: undefined,
eq: undefined,
},
// validation tab in builder form
validate: {
required: false,
plugins: undefined,
min: undefined,
max: undefined,
},
translatedErrors: {
nl: {
required: 'Je moet een waarde opgeven!!!',
min: 'Je kan niet minder dan 1 dag per week bestaan.',
},
},
errors: {
// translatedErrors is converted into errors by the backend
required: 'Je moet een waarde opgeven!!!',
min: 'Je kan niet minder dan 1 dag per week bestaan.',
},
// registration tab in builder form
registration: {
attribute: '',
},
// translations tab in builder form
openForms: {
translations: {
nl: {suffix: 'foo', tooltip: 'bar'},
},
},
});
44 changes: 22 additions & 22 deletions test-d/formio/components/radio.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {RadioComponentSchema} from '../../../lib';
expectAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
key: 'someRadio',
label: 'Some radio',
key: 'aRadio',
label: 'A radio',
openForms: {
dataSrc: 'manual',
translations: {},
Expand All @@ -24,8 +24,8 @@ expectAssignable<RadioComponentSchema>({
expectAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
key: 'someRadio',
label: 'Some radio',
key: 'aRadio',
label: 'A radio',
openForms: {
dataSrc: 'variable',
itemsExpression: 'dummy',
Expand All @@ -37,8 +37,8 @@ expectAssignable<RadioComponentSchema>({
expectAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
key: 'someRadio',
label: 'Some radio',
key: 'aRadio',
label: 'A radio',
multiple: false,
defaultValue: 'dummy',
openForms: {
Expand All @@ -52,8 +52,8 @@ expectAssignable<RadioComponentSchema>({
expectAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
key: 'someRadio',
label: 'Some radio',
key: 'aRadio',
label: 'A radio',
multiple: false,
defaultValue: null,
openForms: {
Expand All @@ -67,8 +67,8 @@ expectAssignable<RadioComponentSchema>({
expectAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
key: 'someRadio',
label: 'Some radio',
key: 'aRadio',
label: 'A radio',
multiple: false,
defaultValue: 'dummy',
openForms: {
Expand Down Expand Up @@ -98,8 +98,8 @@ expectAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
// basic tab
label: 'Some radio',
key: 'someRadio',
label: 'A radio',
key: 'aRadio',
description: '',
tooltip: 'A tooltip',
showInSummary: true,
Expand Down Expand Up @@ -143,16 +143,16 @@ expectAssignable<RadioComponentSchema>({
expectNotAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
key: 'someRadio',
label: 'Some radio',
key: 'aRadio',
label: 'A radio',
});

// multiple true not allowed
expectNotAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
key: 'someRadio',
label: 'Some radio',
key: 'aRadio',
label: 'A radio',
multiple: true,
openForms: {
dataSrc: 'variable',
Expand All @@ -165,8 +165,8 @@ expectNotAssignable<RadioComponentSchema>({
expectNotAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
key: 'someRadio',
label: 'Some radio',
key: 'aRadio',
label: 'A radio',
defaultValue: [{dummy: true}],
openForms: {
dataSrc: 'variable',
Expand All @@ -179,8 +179,8 @@ expectNotAssignable<RadioComponentSchema>({
expectNotAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
key: 'someRadio',
label: 'Some radio',
key: 'aRadio',
label: 'A radio',
openForms: {
dataSrc: 'manual',
translations: {},
Expand All @@ -191,8 +191,8 @@ expectNotAssignable<RadioComponentSchema>({
expectNotAssignable<RadioComponentSchema>({
id: 'yejak',
type: 'radio',
key: 'someRadio',
label: 'Some radio',
key: 'aRadio',
label: 'A radio',
openForms: {
dataSrc: 'variable',
translations: {},
Expand Down

0 comments on commit 239043d

Please sign in to comment.