|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { call, put } from 'redux-saga/effects'; |
| 8 | +import { fetchEffectFactory } from '../fetch_effect'; |
| 9 | +import { indexStatusAction } from '../../actions'; |
| 10 | + |
| 11 | +describe('fetch saga effect factory', () => { |
| 12 | + const asyncAction = indexStatusAction; |
| 13 | + const calledAction = asyncAction.get(); |
| 14 | + let fetchEffect; |
| 15 | + |
| 16 | + it('works with success workflow', () => { |
| 17 | + const indexStatusResult = { indexExists: true, docCount: 2712532 }; |
| 18 | + const fetchStatus = async () => { |
| 19 | + return { indexExists: true, docCount: 2712532 }; |
| 20 | + }; |
| 21 | + fetchEffect = fetchEffectFactory( |
| 22 | + fetchStatus, |
| 23 | + asyncAction.success, |
| 24 | + asyncAction.fail |
| 25 | + )(calledAction); |
| 26 | + let next = fetchEffect.next(); |
| 27 | + |
| 28 | + expect(next.value).toEqual(call(fetchStatus, calledAction.payload)); |
| 29 | + |
| 30 | + const successResult = put(asyncAction.success(indexStatusResult)); |
| 31 | + |
| 32 | + next = fetchEffect.next(indexStatusResult); |
| 33 | + |
| 34 | + expect(next.value).toEqual(successResult); |
| 35 | + }); |
| 36 | + |
| 37 | + it('works with error workflow', () => { |
| 38 | + const indexStatusResultError = new Error('no heartbeat index found'); |
| 39 | + const fetchStatus = async () => { |
| 40 | + return indexStatusResultError; |
| 41 | + }; |
| 42 | + fetchEffect = fetchEffectFactory( |
| 43 | + fetchStatus, |
| 44 | + asyncAction.success, |
| 45 | + asyncAction.fail |
| 46 | + )(calledAction); |
| 47 | + let next = fetchEffect.next(); |
| 48 | + |
| 49 | + expect(next.value).toEqual(call(fetchStatus, calledAction.payload)); |
| 50 | + |
| 51 | + const errorResult = put(asyncAction.fail(indexStatusResultError)); |
| 52 | + |
| 53 | + next = fetchEffect.next(indexStatusResultError); |
| 54 | + |
| 55 | + expect(next.value).toEqual(errorResult); |
| 56 | + }); |
| 57 | + |
| 58 | + it('works with throw error workflow', () => { |
| 59 | + const unExpectedError = new Error('no url found, so throw error'); |
| 60 | + const fetchStatus = async () => { |
| 61 | + return await fetch('/some/url'); |
| 62 | + }; |
| 63 | + fetchEffect = fetchEffectFactory( |
| 64 | + fetchStatus, |
| 65 | + asyncAction.success, |
| 66 | + asyncAction.fail |
| 67 | + )(calledAction); |
| 68 | + let next = fetchEffect.next(); |
| 69 | + |
| 70 | + expect(next.value).toEqual(call(fetchStatus, calledAction.payload)); |
| 71 | + |
| 72 | + const unexpectedErrorResult = put(asyncAction.fail(unExpectedError)); |
| 73 | + |
| 74 | + next = fetchEffect.next(unExpectedError); |
| 75 | + |
| 76 | + expect(next.value).toEqual(unexpectedErrorResult); |
| 77 | + }); |
| 78 | +}); |
0 commit comments