diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-03-25 20:17:19 +0800 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-04-03 01:44:33 +0800 |
| commit | 59948d8c573daae1fadbecb09df5da83b337d3df (patch) | |
| tree | 427324f5ebdd664ffa4fb3f6a5ee2b148ccda040 /test | |
| parent | eeec205e2e51a05dde7ece5ed0e83d186abed5aa (diff) | |
| download | mullvadvpn-59948d8c573daae1fadbecb09df5da83b337d3df.tar.xz mullvadvpn-59948d8c573daae1fadbecb09df5da83b337d3df.zip | |
Mock backend
Diffstat (limited to 'test')
| -rw-r--r-- | test/actions.spec.js | 64 | ||||
| -rw-r--r-- | test/mocks/backend.js | 32 | ||||
| -rw-r--r-- | test/routing.spec.js | 22 |
3 files changed, 71 insertions, 47 deletions
diff --git a/test/actions.spec.js b/test/actions.spec.js index 84a53c935f..f91a99a1bf 100644 --- a/test/actions.spec.js +++ b/test/actions.spec.js @@ -11,11 +11,16 @@ describe('actions', function() { it('should login', (done) => { const expectedActions = [ - { type: 'USER_LOGIN_CHANGE', payload: { status: 'connecting', error: null, account: '222223456789', paidUntil: null } }, - { type: 'USER_LOGIN_CHANGE', payload: { paidUntil: '2013-01-01T00:00:00.000Z', status: 'ok', error: null } } + { type: 'USER_LOGIN_CHANGE', payload: { status: 'connecting', error: null, account: '1'} }, + { type: 'USER_LOGIN_CHANGE', payload: { paidUntil: '2013-01-01T00:00:00.000Z', status: 'ok', error: undefined } } ]; const store = mockStore(mockState()); - const backend = mockBackend(store); + const backend = mockBackend({ + users: { + 1: { + paidUntil: '2013-01-01T00:00:00.000Z', + }} + }); mapBackendEventsToReduxActions(backend, store); backend.once(Backend.EventType.login, () => { @@ -24,24 +29,16 @@ describe('actions', function() { done(); }); - store.dispatch(userActions.login(backend, '222223456789')); + store.dispatch(userActions.login(backend, '1')); }); it('should logout', (done) => { const expectedActions = [ - { type: 'USER_LOGIN_CHANGE', payload: { account: null, paidUntil: null, status: 'none', error: null } } + { type: 'USER_LOGIN_CHANGE', payload: { account: null, paidUntil: null, status: 'none', error: null } }, ]; - let state = Object.assign(mockState(), { - user: { - account: '3333234567890', - paidUntil: '2038-01-01T00:00:00.000Z', - status: LoginState.ok - } - }); - - const store = mockStore(state); - const backend = mockBackend(store); + const store = mockStore(mockState()); + const backend = mockBackend(); mapBackendEventsToReduxActions(backend, store); backend.once(Backend.EventType.logout, () => { @@ -50,7 +47,7 @@ describe('actions', function() { expect(storeActions).deep.equal(expectedActions); done(); }); - + store.dispatch(userActions.logout(backend)); }); @@ -60,31 +57,36 @@ describe('actions', function() { { type: 'CONNECTION_CHANGE', payload: { status: 'connected' } } ]; - let state = Object.assign(mockState(), { - user: { - account: '3333234567890', - paidUntil: '2038-01-01T00:00:00.000Z', - status: LoginState.ok - } - }); - - const store = mockStore(state); - const backend = mockBackend(store); + const store = mockStore(mockState()); + const backend = mockBackend({ + users: { + '1': { + paidUntil: '2038-01-01T00:00:00.000Z', + status: LoginState.ok + } + }}); mapBackendEventsToReduxActions(backend, store); backend.once(Backend.EventType.connect, () => { - const storeActions = filterMinorActions(store.getActions()); + + const storeActions = filterMinorActions(store.getActions()) + .filter(action => { + return action.type === 'CONNECTION_CHANGE' && action.payload.status !== 'disconnected'; + }); expect(storeActions).deep.equal(expectedActions); done(); }); - store.dispatch(connectActions.connect(backend, '1.2.3.4')); + backend.once(Backend.EventType.login, () => { + store.dispatch(connectActions.connect(backend, '1.2.3.4')); + }); + store.dispatch(userActions.login(backend, '1')); }); it('should disconnect from VPN server', (done) => { const expectedActions = [ - { type: 'CONNECTION_CHANGE', payload: { serverAddress: null, status: 'disconnected' } } + { type: 'CONNECTION_CHANGE', payload: { status: 'disconnected', serverAddress: null } } ]; let state = Object.assign(mockState(), { @@ -100,7 +102,7 @@ describe('actions', function() { }); const store = mockStore(state); - const backend = mockBackend(store); + const backend = mockBackend(); mapBackendEventsToReduxActions(backend, store); backend.once(Backend.EventType.disconnect, () => { @@ -132,7 +134,7 @@ describe('actions', function() { }); const store = mockStore(state); - const backend = mockBackend(store); + const backend = mockBackend(); mapBackendEventsToReduxActions(backend, store); backend.once(Backend.EventType.disconnect, () => { diff --git a/test/mocks/backend.js b/test/mocks/backend.js index bac5fac19e..3011c8e26b 100644 --- a/test/mocks/backend.js +++ b/test/mocks/backend.js @@ -1,6 +1,7 @@ import configureMockStore from 'redux-mock-store'; import thunk from 'redux-thunk'; import Backend from '../../app/lib/backend'; +import Ipc from '../../app/lib/ipc'; import { defaultServer } from '../../app/config'; import { LoginState, ConnectionState } from '../../app/enums'; @@ -29,13 +30,34 @@ export const mockState = () => { }; }; -export const mockBackend = (store) => { - const backend = new Backend(); +export const mockBackend = (backendData) => { + return new Backend(mockIpc(backendData)); +}; + +const mockIpc = (backendData) => { + const ipc = new Ipc(); + ipc.send = (action, data) => { + return new Promise((resolve, reject) => { - // patch backend - backend.syncWithReduxStore(store); + switch (action) { + case 'login': + return resolve(backendData.users[data.accountNumber]); + case 'logout': + case 'cancelConnection': + case 'connect': + case 'disconnect': + return resolve(); - return backend; + case 'getLocation': + return resolve({}); + case 'getConnectionInfo': + return resolve({}); + } + + reject('Unknown action: ' + action); + }); + }; + return ipc; }; export const filterMinorActions = (actions) => { diff --git a/test/routing.spec.js b/test/routing.spec.js index 38af64b58c..ad0fa16322 100644 --- a/test/routing.spec.js +++ b/test/routing.spec.js @@ -21,13 +21,15 @@ describe('routing', function() { }); const store = mockStore(state); - const backend = mockBackend(store); + const backend = mockBackend(); mapBackendEventsToRouter(backend, store); store.dispatch(userActions.logout(backend)); - - const storeActions = filterMinorActions(store.getActions()); - expect(storeActions).deep.equal(expectedActions); + + setTimeout(() => { + const storeActions = filterMinorActions(store.getActions()); + expect(storeActions).deep.equal(expectedActions); + }, 0); }); it('should redirect to connect screen on login', (done) => { @@ -35,14 +37,12 @@ describe('routing', function() { { type: '@@router/CALL_HISTORY_METHOD', payload: { method: 'replace', args: [ '/connect' ] } } ]; - let state = Object.assign(mockState(), { - user: { - account: '1111234567890', - status: LoginState.none + const store = mockStore(mockState()); + const backend = mockBackend({ + users: { + '1': { status: LoginState.none }, } }); - const store = mockStore(state); - const backend = mockBackend(store); mapBackendEventsToRouter(backend, store); store.subscribe(() => { @@ -50,7 +50,7 @@ describe('routing', function() { expect(storeActions).deep.equal(expectedActions); done(); }); - store.dispatch(userActions.login(backend, '1111234567890')); + store.dispatch(userActions.login(backend, '1')); }); }); |
