diff options
| -rw-r--r-- | app/lib/backend.js | 15 | ||||
| -rw-r--r-- | app/redux/connection/actions.js | 45 | ||||
| -rw-r--r-- | app/redux/connection/reducers.js | 9 | ||||
| -rw-r--r-- | test/connect.spec.js | 12 | ||||
| -rw-r--r-- | test/connection-info.spec.js | 31 |
5 files changed, 98 insertions, 14 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js index bc6eeeb8ff..02ab2717f0 100644 --- a/app/lib/backend.js +++ b/app/lib/backend.js @@ -112,7 +112,7 @@ export class Backend { country: location.country, city: location.city }; - this._store.dispatch(connectionActions.connectionChange(newLocation)); + this._store.dispatch(connectionActions.newLocation(newLocation)); }) .catch(e => { log.info('Failed getting new location', e); @@ -227,23 +227,18 @@ export class Backend { }); } - connect(addr: string) { + connect(addr: string): Promise<void> { - this._store.dispatch(connectionActions.connectionChange({ - status: 'connecting', - serverAddress: addr, - })); + this._store.dispatch(connectionActions.connectingTo(addr)); - this._ipc.setCountry(addr) + return this._ipc.setCountry(addr) .then( () => { return this._ipc.connect(); }) .catch(e => { log.info('Failed connecting to', addr, e); - this._store.dispatch(connectionActions.connectionChange({ - status: 'disconnected', - })); + this._store.dispatch(connectionActions.disconnected()); }); } diff --git a/app/redux/connection/actions.js b/app/redux/connection/actions.js index 142feb9d79..e334cfd63b 100644 --- a/app/redux/connection/actions.js +++ b/app/redux/connection/actions.js @@ -5,6 +5,7 @@ import { clipboard } from 'electron'; import type { Backend } from '../../lib/backend'; import type { ConnectionReduxState } from './reducers.js'; import type { ReduxGetState, ReduxDispatch } from '../store'; +import type { Coordinate2d } from '../../types'; const connect = (backend: Backend, addr: string) => () => backend.connect(addr); @@ -19,16 +20,49 @@ const copyIPAddress = () => { }; +type ConnectingAction = { + type: 'CONNECTING', + serverAddress: string, +}; +type DisconnectedAction = { + type: 'DISCONNECTED', +}; type ConnectionChangeAction = { type: 'CONNECTION_CHANGE', newData: $Shape<ConnectionReduxState>, }; + type NewPublicIpAction = { type: 'NEW_PUBLIC_IP', ip: string, }; -export type ConnectionAction = ConnectionChangeAction | NewPublicIpAction; + +type Location = { + location: Coordinate2d, + country: string, + city: string, +}; + +type NewLocationAction = { + type: 'NEW_LOCATION', + newLocation: Location, +}; + +export type ConnectionAction = ConnectionChangeAction | NewPublicIpAction | NewLocationAction | ConnectingAction | DisconnectedAction; + +function connectingTo(serverAddress: string): ConnectingAction { + return { + type: 'CONNECTING', + serverAddress: serverAddress, + }; +} + +function disconnected(): DisconnectedAction { + return { + type: 'DISCONNECTED', + }; +} function connectionChange(newData: $Shape<ConnectionReduxState>): ConnectionChangeAction { return { @@ -44,6 +78,13 @@ function newPublicIp(ip: string): NewPublicIpAction { }; } +function newLocation(newLoc: Location): NewLocationAction { + return { + type: 'NEW_LOCATION', + newLocation: newLoc, + }; +} + +export default { connect, disconnect, copyIPAddress, connectionChange, newPublicIp, newLocation, connectingTo, disconnected }; -export default { connect, disconnect, copyIPAddress, connectionChange, newPublicIp }; diff --git a/app/redux/connection/reducers.js b/app/redux/connection/reducers.js index 2426f7eddd..d454e1bc4a 100644 --- a/app/redux/connection/reducers.js +++ b/app/redux/connection/reducers.js @@ -32,6 +32,15 @@ export default function(state: ConnectionReduxState = initialState, action: Redu return { ...state, ...action.newData }; case 'NEW_PUBLIC_IP': return { ...state, ...{ clientIp: action.ip }}; + case 'NEW_LOCATION': + return { ...state, ...action.newLocation }; + case 'CONNECTING': + return { ...state, ...{ + status: 'connecting', + serverAddress: action.serverAddress, + }}; + case 'DISCONNECTED': + return { ...state, ...{ status: 'disconnected' }}; default: return state; } diff --git a/test/connect.spec.js b/test/connect.spec.js index 6f7069f2c0..e2e531e179 100644 --- a/test/connect.spec.js +++ b/test/connect.spec.js @@ -45,6 +45,18 @@ describe('connect', () => { }, done); }); + it('should update the state with the server address', () => { + const { store, backend } = setupBackendAndStore(); + const arbitraryString = 'www.example.com'; + + return backend.connect(arbitraryString) + .then( () => { + const state = store.getState().connection; + expect(state.status).to.equal('connecting'); + expect(state.serverAddress).to.equal(arbitraryString); + }); + }); + it('should correctly deduce \'connected\' from backend states', () => { const { store, mockIpc } = setupBackendAndStore(); diff --git a/test/connection-info.spec.js b/test/connection-info.spec.js index 16600d4389..76a97a35f7 100644 --- a/test/connection-info.spec.js +++ b/test/connection-info.spec.js @@ -8,12 +8,39 @@ import connectionActions from '../app/redux/connection/actions'; describe('The connection state', () => { it('should contain the latest IP', () => { - const memoryHistory = createMemoryHistory(); - const store = configureStore(null, memoryHistory); + const store = createStore(); store.dispatch(connectionActions.newPublicIp('1.2.3.4')); store.dispatch(connectionActions.newPublicIp('5.6.7.8')); expect(store.getState().connection.clientIp).to.equal('5.6.7.8'); }); + + it('should contain the latest location', () => { + const store = createStore(); + + const firstLoc = { + location: [1, 2], + city: 'a', + country: 'b', + }; + const secondLoc = { + location: [3, 4], + city: 'c', + country: 'd', + }; + + store.dispatch(connectionActions.newLocation(firstLoc)); + store.dispatch(connectionActions.newLocation(secondLoc)); + + const { location, city, country } = store.getState().connection; + expect(location).to.equal(secondLoc.location); + expect(city).to.equal(secondLoc.city); + expect(country).to.equal(secondLoc.country); + }); }); + +function createStore() { + const memoryHistory = createMemoryHistory(); + return configureStore(null, memoryHistory); +} |
