From 18e799f23f65d6141cf95213aa5f54b6a0bc6465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Peres=20Fran=C3=A7a?= Date: Fri, 19 Jun 2020 12:17:31 -0300 Subject: [PATCH 1/3] fixing action to send requests --- src/actions/sendRequest.ts | 9 +++++++-- src/utils/url-builder.ts | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/actions/sendRequest.ts b/src/actions/sendRequest.ts index a84d1281..6fd07e3a 100644 --- a/src/actions/sendRequest.ts +++ b/src/actions/sendRequest.ts @@ -81,8 +81,13 @@ const sendRequest: ActionHandler = async ({ contextResponse.status = response.status contextResponse.statusText = response.statusText contextResponse.data = resultText - const resultData = resultText && JSON.parse(resultText) - contextResponse.data = resultData + + try { + const resultData = resultText && JSON.parse(resultText) + contextResponse.data = resultData + } catch { + contextResponse.data = resultText + } if (!response.ok) throw new Error(contextResponse.statusText) handleSuccess(contextResponse as ParsedResponse) diff --git a/src/utils/url-builder.ts b/src/utils/url-builder.ts index d27e1b98..29dedc86 100644 --- a/src/utils/url-builder.ts +++ b/src/utils/url-builder.ts @@ -24,7 +24,8 @@ function createURLBuilder(baseUrl: string): URLBuilder { // According to the convention the relative path should start with '/' const relativePathRegex = /^\/+(\b|$)/ const base = removeSuffix(baseUrl || defaultBaseUrl || '', '/') - return path.match(relativePathRegex) ? `${base}${path}` : path + const url = path.match(relativePathRegex) ? `${base}${path}` : path + return encodeURI(url) }, } } From 33ebbb22054eb3f51e708670ce58f7739c058ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Peres=20Fran=C3=A7a?= Date: Sun, 6 Sep 2020 09:14:03 -0300 Subject: [PATCH 2/3] conditional action. no tests. --- src/action/condition.ts | 25 +++++++++++++++++++++++++ src/action/index.ts | 2 ++ src/action/types.ts | 8 ++++++++ 3 files changed, 35 insertions(+) create mode 100644 src/action/condition.ts diff --git a/src/action/condition.ts b/src/action/condition.ts new file mode 100644 index 00000000..fa2a25e6 --- /dev/null +++ b/src/action/condition.ts @@ -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 = ({ action, executeAction }) => { + const { condition, onTrue, onFalse } = action + if (condition && onTrue) executeAction(onTrue) + if (!condition && onFalse) executeAction(onFalse) +} + +export default condition diff --git a/src/action/index.ts b/src/action/index.ts index 8df2fb22..cb41f375 100644 --- a/src/action/index.ts +++ b/src/action/index.ts @@ -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' @@ -30,6 +31,7 @@ const defaultActionHandlers: Record = { 'beagle:alert': alert, 'beagle:confirm': confirm, 'beagle:submitForm': submitForm, + 'beagle:condition': condition, ...NavigationActions, } diff --git a/src/action/types.ts b/src/action/types.ts index 341807eb..e3b6cfc5 100644 --- a/src/action/types.ts +++ b/src/action/types.ts @@ -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, @@ -74,6 +81,7 @@ export type BeagleDefaultAction = ( | ConfirmAction | BeagleNavigationAction | SubmitFormAction + | ConditionAction ) export type BeagleAction = BeagleDefaultAction | CustomAction From 1285787b1a66ce635b58d85af0fe45556b69267f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Peres=20Fran=C3=A7a?= Date: Tue, 8 Sep 2020 10:55:27 -0300 Subject: [PATCH 3/3] tests --- __tests__/action/condition.spec.ts | 67 ++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 __tests__/action/condition.spec.ts diff --git a/__tests__/action/condition.spec.ts b/__tests__/action/condition.spec.ts new file mode 100644 index 00000000..662c4282 --- /dev/null +++ b/__tests__/action/condition.spec.ts @@ -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) + }) +})