Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Feature/conditional action #244

Merged
merged 27 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
18e799f
fixing action to send requests
Tiagoperes Jun 19, 2020
bc51ccb
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jun 19, 2020
03d837d
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jun 22, 2020
7fd8685
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jun 23, 2020
b09ec79
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jun 23, 2020
a3fc8b1
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jun 24, 2020
3e290da
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jun 25, 2020
140486d
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jun 26, 2020
272072a
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jul 1, 2020
b478a43
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jul 12, 2020
6b74e49
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jul 21, 2020
8b05520
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jul 23, 2020
2b13987
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Jul 29, 2020
e1ad95c
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Aug 11, 2020
cb1a53a
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Aug 13, 2020
dc88ad8
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Aug 17, 2020
a29c04f
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Aug 20, 2020
8ad4233
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Aug 25, 2020
07b5a8e
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Aug 28, 2020
30725e9
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Sep 2, 2020
7ccad2f
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Sep 3, 2020
1dd8705
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Sep 4, 2020
cf83b15
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Sep 4, 2020
4f4cc00
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Sep 4, 2020
064dfd6
Merge branch 'master' of github.com:ZupIT/beagle-web-core
Tiagoperes Sep 4, 2020
33ebbb2
conditional action. no tests.
Tiagoperes Sep 6, 2020
1285787
tests
Tiagoperes Sep 8, 2020
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
67 changes: 67 additions & 0 deletions __tests__/action/condition.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import actions from 'action'
import condition from 'action/condition'
import { ConditionAction } from 'action/types'
import { BeagleView } from 'beagle-view/types'

describe('Action: condition', () => {
const element = { _beagleComponent_: 'beagle:button', id: 'btn' }
const onTrue = [{ _beagleAction_: 'custom:onTrue' }]
const onFalse = [{ _beagleAction_: 'custom:onFalse' }]
const trueAction: ConditionAction = {
_beagleAction_: 'beagle:condition',
condition: true,
onFalse,
onTrue,
}
const beagleView = {} as BeagleView
const executeAction = jest.fn()

beforeEach(() => {
executeAction.mockClear()
})

it('should exist', () => {
expect(actions['beagle:condition']).toBe(condition)
})

it('should run onTrue, but not onFalse', () => {
condition({ action: trueAction, element, beagleView, executeAction })
expect(executeAction).toHaveBeenCalledTimes(1)
expect(executeAction).toHaveBeenCalledWith(onTrue)
})

it('should run onFalse, but not onTrue', () => {
const action = { ...trueAction, condition: false }
condition({ action, element, beagleView, executeAction })
expect(executeAction).toHaveBeenCalledTimes(1)
expect(executeAction).toHaveBeenCalledWith(onFalse)
})

it('should do nothing when resolved to true and no onTrue exists', () => {
const action = { ...trueAction, onTrue: undefined }
condition({ action, element, beagleView, executeAction })
expect(executeAction).toHaveBeenCalledTimes(0)
})

it('should do nothing when resolved to false and no onFalse exists', () => {
const action = { ...trueAction, condition: false, onFalse: undefined }
condition({ action, element, beagleView, executeAction })
expect(executeAction).toHaveBeenCalledTimes(0)
})
})
25 changes: 25 additions & 0 deletions src/action/condition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ActionHandler, ConditionAction } from './types'

const condition: ActionHandler<ConditionAction> = ({ action, executeAction }) => {
const { condition, onTrue, onFalse } = action
if (condition && onTrue) executeAction(onTrue)
if (!condition && onFalse) executeAction(onFalse)
}

export default condition
2 changes: 2 additions & 0 deletions src/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import sendRequest from './send-request'
import alert from './alert'
import confirm from './confirm'
import submitForm from './submit-form'
import condition from './condition'
import NavigationActions from './navigation'
import { ActionHandler } from './types'

Expand All @@ -30,6 +31,7 @@ const defaultActionHandlers: Record<string, ActionHandler> = {
'beagle:alert': alert,
'beagle:confirm': confirm,
'beagle:submitForm': submitForm,
'beagle:condition': condition,
...NavigationActions,
}

Expand Down
8 changes: 8 additions & 0 deletions src/action/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export interface SubmitFormAction {
_beagleAction_: 'beagle:submitForm',
}

export interface ConditionAction {
_beagleAction_: 'beagle:condition',
condition: boolean,
onTrue?: BeagleAction | BeagleAction[],
onFalse?: BeagleAction | BeagleAction[],
}

export interface CustomAction {
_beagleAction_: string,
[key: string]: any,
Expand All @@ -74,6 +81,7 @@ export type BeagleDefaultAction = (
| ConfirmAction
| BeagleNavigationAction
| SubmitFormAction
| ConditionAction
)

export type BeagleAction = BeagleDefaultAction | CustomAction
Expand Down