Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions src/server/plugins/engine/components/CheckboxesField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@ describe.each([
deny: ['5', '6', '7', '8']
}
},
{
component: {
title: 'String list title',
shortDescription: 'String list',
name: 'myComponent',
type: ComponentType.CheckboxesField,
list: 'listString',
options: {}
} satisfies CheckboxesFieldComponent,

options: {
label: 'string list',
list: listString,
examples: listStringExamples,
allow: ['1', '2', '3', '4'],
deny: ['5', '6', '7', '8']
}
},
{
component: {
title: 'Number list title',
Expand Down Expand Up @@ -407,5 +389,43 @@ describe.each([
expect(errors.advancedSettingsErrors).toBeEmpty()
})
})

describe('getDisplayStringFromFormValue', () => {
it('returns empty string when value is undefined', () => {
const checkboxField = field as CheckboxesField
const result = checkboxField.getDisplayStringFromFormValue(undefined)
expect(result).toBe('')
})

it('returns empty string when value is empty array', () => {
const checkboxField = field as CheckboxesField
const result = checkboxField.getDisplayStringFromFormValue([])
expect(result).toBe('')
})

it.each([...options.examples])(
'returns text for single selected value',
(item) => {
const checkboxField = field as CheckboxesField
const result = checkboxField.getDisplayStringFromFormValue([
item.value
])
expect(result).toBe(item.text)
}
)

it('returns comma-separated text for multiple selected values', () => {
const checkboxField = field as CheckboxesField
const item1 = options.examples[0]
const item2 = options.examples[2]

const result = checkboxField.getDisplayStringFromFormValue([
item1.value,
item2.value
])

expect(result).toBe(`${item1.text}, ${item2.text}`)
})
})
})
})
8 changes: 7 additions & 1 deletion src/server/plugins/engine/components/CheckboxesField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ export class CheckboxesField extends SelectionControlField {
return this.isValue(value) ? value : undefined
}

getDisplayStringFromFormValue(selected: (string | number | boolean)[]) {
getDisplayStringFromFormValue(
selected: (string | number | boolean)[] | undefined
) {
const { items } = this

if (!selected) {
return ''
}

// Map selected values to text
return items
.filter((item) => selected.includes(item.value))
Expand Down
Loading