forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
361 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,310 @@ | ||
import connectSaga from '../sagas/connectSaga'; | ||
import Observable from 'core-js/library/es7/observable'; | ||
|
||
import createSagaMiddleware from 'redux-saga'; | ||
import { applyMiddleware, createStore } from 'redux'; | ||
|
||
let connectionStatusObserver; | ||
let directLine; | ||
let store; | ||
|
||
beforeEach(() => { | ||
const sagaMiddleware = createSagaMiddleware(); | ||
|
||
store = createStore( | ||
({ actions = [] } = {}, action) => ({ | ||
actions: [ | ||
...actions, | ||
action | ||
] | ||
}), | ||
applyMiddleware(sagaMiddleware) | ||
); | ||
|
||
sagaMiddleware.run(connectSaga); | ||
|
||
directLine = { | ||
activity$: new Observable(() => {}), | ||
connectionStatus$: new Observable(observer => { | ||
connectionStatusObserver = observer; | ||
observer.next(0); | ||
}), | ||
end: () => {}, | ||
postActivity: () => new Observable(observer => { | ||
observer.next({ id: '' }); | ||
}) | ||
}; | ||
}); | ||
|
||
function sleep(ms = 1000) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} | ||
|
||
function assertActionTypes(actual, expectedTypes) { | ||
// console.log(store.getState().actions.map(({ type }) => type)); | ||
|
||
const actualTypes = []; | ||
|
||
actual.forEach(({ type }) => { | ||
if (expectedTypes[actualTypes.length] === type) { | ||
actualTypes.push(type); | ||
} | ||
}); | ||
|
||
expect(expectedTypes).toEqual(actualTypes); | ||
} | ||
|
||
test('Connect successfully', async () => { | ||
store.dispatch({ | ||
type: 'DIRECT_LINE/CONNECT', | ||
payload: { directLine } | ||
}); | ||
|
||
connectionStatusObserver.next(1); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING' | ||
]); | ||
|
||
connectionStatusObserver.next(2); | ||
|
||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING', | ||
'DIRECT_LINE/CONNECT_FULFILLING', | ||
'DIRECT_LINE/CONNECT_FULFILLED' | ||
]); | ||
}); | ||
|
||
test('Connect failed initially', async () => { | ||
store.dispatch({ | ||
type: 'DIRECT_LINE/CONNECT', | ||
payload: { directLine } | ||
}); | ||
|
||
connectionStatusObserver.next(1); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING' | ||
]); | ||
|
||
connectionStatusObserver.next(4); | ||
|
||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING', | ||
'DIRECT_LINE/CONNECT_REJECTED' | ||
]); | ||
}); | ||
|
||
test('Connection failed after established', async () => { | ||
store.dispatch({ | ||
type: 'DIRECT_LINE/CONNECT', | ||
payload: { directLine } | ||
}); | ||
|
||
connectionStatusObserver.next(1); | ||
connectionStatusObserver.next(2); | ||
connectionStatusObserver.next(4); | ||
|
||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING', | ||
'DIRECT_LINE/CONNECT_FULFILLING', | ||
'DIRECT_LINE/CONNECT_FULFILLED', | ||
'DIRECT_LINE/DISCONNECT_PENDING', | ||
'DIRECT_LINE/DISCONNECT_FULFILLED' | ||
]); | ||
}); | ||
|
||
test('Request to disconnect before connection established', async () => { | ||
store.dispatch({ | ||
type: 'DIRECT_LINE/CONNECT', | ||
payload: { directLine } | ||
}); | ||
|
||
connectionStatusObserver.next(1); | ||
|
||
store.dispatch({ type: 'DIRECT_LINE/DISCONNECT' }); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING', | ||
'DIRECT_LINE/DISCONNECT', | ||
'DIRECT_LINE/CONNECT_REJECTED', | ||
'DIRECT_LINE/DISCONNECT_PENDING', | ||
'DIRECT_LINE/DISCONNECT_FULFILLED' | ||
]); | ||
}); | ||
|
||
test('Request to disconnect after connection established', async () => { | ||
store.dispatch({ | ||
type: 'DIRECT_LINE/CONNECT', | ||
payload: { directLine } | ||
}); | ||
|
||
connectionStatusObserver.next(1); | ||
connectionStatusObserver.next(2); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
store.dispatch({ type: 'DIRECT_LINE/DISCONNECT' }); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING', | ||
'DIRECT_LINE/CONNECT_FULFILLING', | ||
'DIRECT_LINE/CONNECT_FULFILLED', | ||
'DIRECT_LINE/DISCONNECT', | ||
'DIRECT_LINE/DISCONNECT_PENDING', | ||
'DIRECT_LINE/DISCONNECT_FULFILLED' | ||
]); | ||
}); | ||
|
||
test('Reconnect after connection established', async () => { | ||
store.dispatch({ | ||
type: 'DIRECT_LINE/CONNECT', | ||
payload: { directLine } | ||
}); | ||
|
||
connectionStatusObserver.next(1); | ||
connectionStatusObserver.next(2); | ||
connectionStatusObserver.next(1); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING', | ||
'DIRECT_LINE/CONNECT_FULFILLING', | ||
'DIRECT_LINE/CONNECT_FULFILLED', | ||
'DIRECT_LINE/RECONNECT_PENDING' | ||
]); | ||
|
||
connectionStatusObserver.next(2); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING', | ||
'DIRECT_LINE/CONNECT_FULFILLING', | ||
'DIRECT_LINE/CONNECT_FULFILLED', | ||
'DIRECT_LINE/RECONNECT_PENDING', | ||
'DIRECT_LINE/RECONNECT_FULFILLING', | ||
'DIRECT_LINE/RECONNECT_FULFILLED' | ||
]); | ||
}); | ||
|
||
test('Reconnect failed', async () => { | ||
store.dispatch({ | ||
type: 'DIRECT_LINE/CONNECT', | ||
payload: { directLine } | ||
}); | ||
|
||
connectionStatusObserver.next(1); | ||
connectionStatusObserver.next(2); | ||
connectionStatusObserver.next(1); | ||
connectionStatusObserver.next(4); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING', | ||
'DIRECT_LINE/CONNECT_FULFILLING', | ||
'DIRECT_LINE/CONNECT_FULFILLED', | ||
'DIRECT_LINE/RECONNECT_PENDING', | ||
'DIRECT_LINE/RECONNECT_REJECTED', | ||
'DIRECT_LINE/DISCONNECT_PENDING', | ||
'DIRECT_LINE/DISCONNECT_FULFILLED' | ||
]); | ||
}); | ||
|
||
test('Request to disconnect when reconnecting', async () => { | ||
store.dispatch({ | ||
type: 'DIRECT_LINE/CONNECT', | ||
payload: { directLine } | ||
}); | ||
|
||
connectionStatusObserver.next(1); | ||
connectionStatusObserver.next(2); | ||
connectionStatusObserver.next(1); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
store.dispatch({ type: 'DIRECT_LINE/DISCONNECT' }); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING', | ||
'DIRECT_LINE/CONNECT_FULFILLING', | ||
'DIRECT_LINE/CONNECT_FULFILLED', | ||
'DIRECT_LINE/RECONNECT_PENDING', | ||
'DIRECT_LINE/DISCONNECT', | ||
'DIRECT_LINE/RECONNECT_REJECTED', | ||
'DIRECT_LINE/DISCONNECT_PENDING', | ||
'DIRECT_LINE/DISCONNECT_FULFILLED' | ||
]); | ||
}); | ||
|
||
test('Request to disconnect after reconnected', async () => { | ||
store.dispatch({ | ||
type: 'DIRECT_LINE/CONNECT', | ||
payload: { directLine } | ||
}); | ||
|
||
connectionStatusObserver.next(1); | ||
connectionStatusObserver.next(2); | ||
connectionStatusObserver.next(1); | ||
connectionStatusObserver.next(2); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
store.dispatch({ type: 'DIRECT_LINE/DISCONNECT' }); | ||
|
||
// TODO: [P4] Investigates why we need to sleep 0 here | ||
await sleep(0); | ||
|
||
assertActionTypes(store.getState().actions, [ | ||
'DIRECT_LINE/CONNECT', | ||
'DIRECT_LINE/CONNECT_PENDING', | ||
'DIRECT_LINE/CONNECT_FULFILLING', | ||
'DIRECT_LINE/CONNECT_FULFILLED', | ||
'DIRECT_LINE/RECONNECT_PENDING', | ||
'DIRECT_LINE/RECONNECT_FULFILLING', | ||
'DIRECT_LINE/RECONNECT_FULFILLED', | ||
'DIRECT_LINE/DISCONNECT', | ||
'DIRECT_LINE/DISCONNECT_PENDING', | ||
'DIRECT_LINE/DISCONNECT_FULFILLED' | ||
]); | ||
}); |
Oops, something went wrong.