diff --git a/src/prefabs/factories/options/action.ts b/src/prefabs/factories/options/action.ts new file mode 100644 index 00000000..d0cd7ec4 --- /dev/null +++ b/src/prefabs/factories/options/action.ts @@ -0,0 +1,29 @@ +import { + PrefabComponentOption, + ValueDefault, + ValueRef, +} from '../../types/options'; + +type OptionProducer = (key: string) => PrefabComponentOption; + +// typescript issue #36981 +// Omit is currently desctructive to union/extended types see +// So we have to Omit each variant as a work around +type RedundantKeys = 'type' | 'key' | 'label'; +type Attributes = + | Omit + | Omit; + +const defaultAttributes = { + value: [], +}; + +export const action = + (label: string, attrs: Attributes): OptionProducer => + (key) => ({ + ...defaultAttributes, + ...attrs, + key, + type: 'ACTION', + label, + }); diff --git a/src/prefabs/factories/options/actionInputObjects.ts b/src/prefabs/factories/options/actionInputObjects.ts new file mode 100644 index 00000000..8cccb3ad --- /dev/null +++ b/src/prefabs/factories/options/actionInputObjects.ts @@ -0,0 +1,29 @@ +import { + PrefabComponentOption, + ValueDefault, + ValueRef, +} from '../../types/options'; + +type OptionProducer = (key: string) => PrefabComponentOption; + +// typescript issue #36981 +// Omit is currently desctructive to union/extended types see +// So we have to Omit each variant as a work around +type RedundantKeys = 'type' | 'key' | 'label'; +type Attributes = + | Omit + | Omit; + +const defaultAttributes = { + value: [], +}; + +export const actionInputObjects = + (label: string, attrs: Attributes): OptionProducer => + (key) => ({ + ...defaultAttributes, + ...attrs, + key, + type: 'ACTION_INPUT_OBJECTS', + label, + }); diff --git a/src/prefabs/factories/options/index.ts b/src/prefabs/factories/options/index.ts index d01137e2..868a59ae 100644 --- a/src/prefabs/factories/options/index.ts +++ b/src/prefabs/factories/options/index.ts @@ -1,3 +1,5 @@ +export { action } from './action'; +export { actionInputObjects } from './actionInputObjects'; export { color } from './color'; export { endpoint } from './endpoint'; export { font } from './font'; diff --git a/tests/prefabs/factories/action.test.ts b/tests/prefabs/factories/action.test.ts new file mode 100644 index 00000000..292bdfec --- /dev/null +++ b/tests/prefabs/factories/action.test.ts @@ -0,0 +1,51 @@ +import test from 'tape'; +import { action } from '../../../src/prefabs/factories/options/action'; +import { actionInputObjects } from '../../../src/prefabs/factories/options/actionInputObjects'; + +test('action builds variable option with options ', (t) => { + const result = action('Action', { value: '#stringValue' })('action'); + + const expected = { + value: '#stringValue', + key: 'action', + type: 'ACTION', + label: 'Action', + }; + + t.deepEqual(result, expected); + t.end(); +}); + +test('actionInputObjects builds options with configuration', (t) => { + const result = actionInputObjects('Objects to pass to action', { + value: [], + configuration: { + apiVersion: 'v1', + condition: { + type: 'SHOW', + option: 'linkType', + comparator: 'EQ', + value: 'action', + }, + }, + })('actionModels'); + + const expected = { + value: [], + label: 'Objects to pass to action', + key: 'actionModels', + type: 'ACTION_INPUT_OBJECTS', + configuration: { + apiVersion: 'v1', + condition: { + type: 'SHOW', + option: 'linkType', + comparator: 'EQ', + value: 'action', + }, + }, + }; + + t.deepEqual(result, expected); + t.end(); +});