Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
compulim committed Apr 10, 2019
1 parent 974b496 commit 305a899
Show file tree
Hide file tree
Showing 2 changed files with 361 additions and 52 deletions.
310 changes: 310 additions & 0 deletions packages/core/src/__tests__/connectSaga.spec.js
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'
]);
});
Loading

0 comments on commit 305a899

Please sign in to comment.