Skip to content

Commit

Permalink
Merge pull request #28 from bettyblocks/feat/add-action-helper-functi…
Browse files Browse the repository at this point in the history
…ons-TMPLT-1493

feat: add action and actioninputobject helper functions
  • Loading branch information
benjamin-rocks authored Jun 23, 2022
2 parents 911da15 + 15a4e34 commit b3ad688
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/prefabs/factories/options/action.ts
Original file line number Diff line number Diff line change
@@ -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<ValueDefault, RedundantKeys>
| Omit<ValueRef, RedundantKeys>;

const defaultAttributes = {
value: [],
};

export const action =
(label: string, attrs: Attributes): OptionProducer =>
(key) => ({
...defaultAttributes,
...attrs,
key,
type: 'ACTION',
label,
});
29 changes: 29 additions & 0 deletions src/prefabs/factories/options/actionInputObjects.ts
Original file line number Diff line number Diff line change
@@ -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<ValueDefault, RedundantKeys>
| Omit<ValueRef, RedundantKeys>;

const defaultAttributes = {
value: [],
};

export const actionInputObjects =
(label: string, attrs: Attributes): OptionProducer =>
(key) => ({
...defaultAttributes,
...attrs,
key,
type: 'ACTION_INPUT_OBJECTS',
label,
});
2 changes: 2 additions & 0 deletions src/prefabs/factories/options/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { action } from './action';
export { actionInputObjects } from './actionInputObjects';
export { color } from './color';
export { endpoint } from './endpoint';
export { font } from './font';
Expand Down
51 changes: 51 additions & 0 deletions tests/prefabs/factories/action.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit b3ad688

Please sign in to comment.