|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. |
| 4 | +// |
| 5 | +// Microsoft Bot Framework: http://botframework.com |
| 6 | +// |
| 7 | +// Bot Framework Emulator Github: |
| 8 | +// https://github.com/Microsoft/BotFramwork-Emulator |
| 9 | +// |
| 10 | +// Copyright (c) Microsoft Corporation |
| 11 | +// All rights reserved. |
| 12 | +// |
| 13 | +// MIT License: |
| 14 | +// Permission is hereby granted, free of charge, to any person obtaining |
| 15 | +// a copy of this software and associated documentation files (the |
| 16 | +// "Software"), to deal in the Software without restriction, including |
| 17 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 18 | +// distribute, sublicense, and/or sell copies of the Software, and to |
| 19 | +// permit persons to whom the Software is furnished to do so, subject to |
| 20 | +// the following conditions: |
| 21 | +// |
| 22 | +// The above copyright notice and this permission notice shall be |
| 23 | +// included in all copies or substantial portions of the Software. |
| 24 | +// |
| 25 | +// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, |
| 26 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 28 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 29 | +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 30 | +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 31 | +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 32 | +// |
| 33 | + |
| 34 | +import { |
| 35 | + TunnelStatus, |
| 36 | + NgrokTunnelAction, |
| 37 | + TunnelInfo, |
| 38 | + NgrokTunnelPayloadTypes, |
| 39 | + NgrokTunnelActions, |
| 40 | + TunnelError, |
| 41 | + TunnelStatusAndTs, |
| 42 | +} from '../actions/ngrokTunnelActions'; |
| 43 | + |
| 44 | +import { ngrokTunnel, NgrokTunnelState } from './ngrokTunnel'; |
| 45 | + |
| 46 | +describe('Ngrok Tunnel reducer', () => { |
| 47 | + const DEFAULT_STATE: NgrokTunnelState = { |
| 48 | + inspectUrl: 'http://127.0.0.1:4040', |
| 49 | + publicUrl: '', |
| 50 | + logPath: '', |
| 51 | + postmanCollectionPath: '', |
| 52 | + errors: {} as TunnelError, |
| 53 | + tunnelStatus: TunnelStatus.Inactive, |
| 54 | + lastTunnelStatusCheckTS: '', |
| 55 | + }; |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + const emptyAction: NgrokTunnelAction<NgrokTunnelPayloadTypes> = { |
| 59 | + type: null, |
| 60 | + payload: null, |
| 61 | + }; |
| 62 | + ngrokTunnel({ ...DEFAULT_STATE }, emptyAction); |
| 63 | + }); |
| 64 | + |
| 65 | + it('Tunnel info should be set from payload', () => { |
| 66 | + const payload: TunnelInfo = { |
| 67 | + publicUrl: 'https://abc.io/', |
| 68 | + inspectUrl: 'http://127.0.0.1:4003', |
| 69 | + logPath: 'logger.txt', |
| 70 | + postmanCollectionPath: 'postman.json', |
| 71 | + }; |
| 72 | + const setDetailsAction: NgrokTunnelAction<NgrokTunnelPayloadTypes> = { |
| 73 | + type: NgrokTunnelActions.setDetails, |
| 74 | + payload, |
| 75 | + }; |
| 76 | + const startingState = { ...DEFAULT_STATE }; |
| 77 | + const endingState = ngrokTunnel(startingState, setDetailsAction); |
| 78 | + |
| 79 | + expect(endingState.publicUrl).toBe(payload.publicUrl); |
| 80 | + expect(endingState.logPath).toBe(payload.logPath); |
| 81 | + expect(endingState.postmanCollectionPath).toBe(payload.postmanCollectionPath); |
| 82 | + expect(endingState.inspectUrl).toBe(payload.inspectUrl); |
| 83 | + }); |
| 84 | + |
| 85 | + it('Tunnel errors should be set from payload', () => { |
| 86 | + const payload: TunnelError = { |
| 87 | + statusCode: 422, |
| 88 | + errorMessage: '<h1>Too many connections<h1>', |
| 89 | + }; |
| 90 | + const updateErrorAction: NgrokTunnelAction<NgrokTunnelPayloadTypes> = { |
| 91 | + type: NgrokTunnelActions.updateOnError, |
| 92 | + payload, |
| 93 | + }; |
| 94 | + const startingState = { ...DEFAULT_STATE }; |
| 95 | + const endingState = ngrokTunnel(startingState, updateErrorAction); |
| 96 | + |
| 97 | + expect(endingState.publicUrl).toBe(DEFAULT_STATE.publicUrl); |
| 98 | + expect(endingState.errors.statusCode).toBe(payload.statusCode); |
| 99 | + expect(endingState.errors.errorMessage).toBe(payload.errorMessage); |
| 100 | + }); |
| 101 | + |
| 102 | + it('Tunnel status should be set from payload', () => { |
| 103 | + const payload: TunnelStatusAndTs = { |
| 104 | + status: TunnelStatus.Active, |
| 105 | + ts: '12/27/2019, 1:30:00 PM', |
| 106 | + }; |
| 107 | + const nextPayload: TunnelStatusAndTs = { |
| 108 | + status: TunnelStatus.Error, |
| 109 | + ts: '12/27/2019, 1:33:00 PM', |
| 110 | + }; |
| 111 | + const actions: NgrokTunnelAction<NgrokTunnelPayloadTypes>[] = [ |
| 112 | + { |
| 113 | + type: NgrokTunnelActions.setStatus, |
| 114 | + payload, |
| 115 | + }, |
| 116 | + { |
| 117 | + type: NgrokTunnelActions.setStatus, |
| 118 | + payload: nextPayload, |
| 119 | + }, |
| 120 | + ]; |
| 121 | + const startingState = { ...DEFAULT_STATE }; |
| 122 | + const transientState = ngrokTunnel(startingState, actions[0]); |
| 123 | + expect(transientState.tunnelStatus).toBe(payload.status); |
| 124 | + |
| 125 | + const finalState = ngrokTunnel(startingState, actions[1]); |
| 126 | + expect(finalState.tunnelStatus).toBe(nextPayload.status); |
| 127 | + }); |
| 128 | +}); |
0 commit comments