-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from bettyblocks/feat/add-action-helper-functi…
…ons-TMPLT-1493 feat: add action and actioninputobject helper functions
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |