From 23d0400500081e7d883f9f95d8fe4f9a788e843a Mon Sep 17 00:00:00 2001 From: shahzad Date: Fri, 20 Mar 2020 13:24:15 +0100 Subject: [PATCH 1/3] update fetch effect --- .../public/state/effects/fetch_effect.ts | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/x-pack/legacy/plugins/uptime/public/state/effects/fetch_effect.ts b/x-pack/legacy/plugins/uptime/public/state/effects/fetch_effect.ts index d1d7626b2eab3..fb3e920d0eaac 100644 --- a/x-pack/legacy/plugins/uptime/public/state/effects/fetch_effect.ts +++ b/x-pack/legacy/plugins/uptime/public/state/effects/fetch_effect.ts @@ -24,17 +24,23 @@ export function fetchEffectFactory( fail: (error: Error) => Action ) { return function*(action: Action) { - const { - payload: { ...params }, - } = action; - const response = yield call(fetch, params); - if (response instanceof Error) { - // eslint-disable-next-line no-console - console.error(response); + try { + const { + payload: { ...params }, + } = action; + const response = yield call(fetch, params); + if (response instanceof Error) { + // eslint-disable-next-line no-console + console.error(response); - yield put(fail(response)); - } else { - yield put(success(response)); + yield put(fail(response)); + } else { + yield put(success(response)); + } + } catch (error) { + // eslint-disable-next-line no-console + console.error(error); + yield put(fail(error)); } }; } From 648f25d6363129fbf79d9cd1823508be6518a818 Mon Sep 17 00:00:00 2001 From: shahzad Date: Fri, 20 Mar 2020 18:17:45 +0100 Subject: [PATCH 2/3] added test --- .../effects/__tests__/fecth_effect.test.ts | 78 +++++++++++++++++++ .../public/state/effects/fetch_effect.ts | 6 +- 2 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 x-pack/legacy/plugins/uptime/public/state/effects/__tests__/fecth_effect.test.ts diff --git a/x-pack/legacy/plugins/uptime/public/state/effects/__tests__/fecth_effect.test.ts b/x-pack/legacy/plugins/uptime/public/state/effects/__tests__/fecth_effect.test.ts new file mode 100644 index 0000000000000..734f00eff7397 --- /dev/null +++ b/x-pack/legacy/plugins/uptime/public/state/effects/__tests__/fecth_effect.test.ts @@ -0,0 +1,78 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { call, put } from 'redux-saga/effects'; +import { fetchEffectFactory } from '../fetch_effect'; +import { indexStatusAction } from '../../actions'; + +describe('fetch saga effect factory', () => { + const asyncAction = indexStatusAction; + const calledAction = asyncAction.get(); + let fetchEffect; + + it('works with success workflow', () => { + const indexStatusResult = { indexExists: true, docCount: 2712532 }; + const fetchStatus = async () => { + return { indexExists: true, docCount: 2712532 }; + }; + fetchEffect = fetchEffectFactory( + fetchStatus, + asyncAction.success, + asyncAction.fail + )(calledAction); + let next = fetchEffect.next(); + + expect(next.value).toEqual(call(fetchStatus, calledAction.payload)); + + const successResult = put(asyncAction.success(indexStatusResult)); + + next = fetchEffect.next(indexStatusResult); + + expect(next.value).toEqual(successResult); + }); + + it('works with error workflow', () => { + const indexStatusResultError = new Error('no heartbeat index found'); + const fetchStatus = async () => { + return indexStatusResultError; + }; + fetchEffect = fetchEffectFactory( + fetchStatus, + asyncAction.success, + asyncAction.fail + )(calledAction); + let next = fetchEffect.next(); + + expect(next.value).toEqual(call(fetchStatus, calledAction.payload)); + + const errorResult = put(asyncAction.fail(indexStatusResultError)); + + next = fetchEffect.next(indexStatusResultError); + + expect(next.value).toEqual(errorResult); + }); + + it('works with throw error workflow', () => { + const unExpectedError = new Error('no url found, so throw error'); + const fetchStatus = async () => { + return await fetch('/some/url'); + }; + fetchEffect = fetchEffectFactory( + fetchStatus, + asyncAction.success, + asyncAction.fail + )(calledAction); + let next = fetchEffect.next().value; + + expect(next).toEqual(call(fetchStatus, calledAction.payload)); + + const unexpectedErrorResult = put(asyncAction.fail(unExpectedError)); + + next = fetchEffect.next(unExpectedError); + + expect(next.value).toEqual(unexpectedErrorResult); + }); +}); diff --git a/x-pack/legacy/plugins/uptime/public/state/effects/fetch_effect.ts b/x-pack/legacy/plugins/uptime/public/state/effects/fetch_effect.ts index fb3e920d0eaac..943275d21e51e 100644 --- a/x-pack/legacy/plugins/uptime/public/state/effects/fetch_effect.ts +++ b/x-pack/legacy/plugins/uptime/public/state/effects/fetch_effect.ts @@ -25,10 +25,8 @@ export function fetchEffectFactory( ) { return function*(action: Action) { try { - const { - payload: { ...params }, - } = action; - const response = yield call(fetch, params); + const response = yield call(fetch, action.payload); + if (response instanceof Error) { // eslint-disable-next-line no-console console.error(response); From a28775861ea7afca31e8cd8766e2448a257e342b Mon Sep 17 00:00:00 2001 From: shahzad Date: Fri, 20 Mar 2020 23:41:36 +0100 Subject: [PATCH 3/3] update type --- .../public/state/effects/__tests__/fecth_effect.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/legacy/plugins/uptime/public/state/effects/__tests__/fecth_effect.test.ts b/x-pack/legacy/plugins/uptime/public/state/effects/__tests__/fecth_effect.test.ts index 734f00eff7397..6520e1492bddc 100644 --- a/x-pack/legacy/plugins/uptime/public/state/effects/__tests__/fecth_effect.test.ts +++ b/x-pack/legacy/plugins/uptime/public/state/effects/__tests__/fecth_effect.test.ts @@ -65,9 +65,9 @@ describe('fetch saga effect factory', () => { asyncAction.success, asyncAction.fail )(calledAction); - let next = fetchEffect.next().value; + let next = fetchEffect.next(); - expect(next).toEqual(call(fetchStatus, calledAction.payload)); + expect(next.value).toEqual(call(fetchStatus, calledAction.payload)); const unexpectedErrorResult = put(asyncAction.fail(unExpectedError));