summaryrefslogtreecommitdiffhomepage
path: root/test/mocks/backend.js
blob: 3011c8e26b2df4671392134122546ac37b4025d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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';

// fetch is absent in node environment
// this will automatically import it into global scope
import fetch from 'isomorphic-fetch'; // eslint-disable-line no-unused-vars

const middlewares = [ thunk ];
export const mockStore = configureMockStore(middlewares);
export const mockState = () => {
  return {
    user: {
      account: null,
      status: LoginState.none,
      error: null
    },
    connect: {
      status: ConnectionState.disconnected,
      serverAddress: null,
      clientIp: null
    },
    settings: {
      autoSecure: false,
      preferredServer: defaultServer
    }
  };
};

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) => {

      switch (action) {
      case 'login':
        return resolve(backendData.users[data.accountNumber]);
      case 'logout':
      case 'cancelConnection':
      case 'connect':
      case 'disconnect':
        return resolve();

      case 'getLocation':
        return resolve({});
      case 'getConnectionInfo':
        return resolve({});
      }

      reject('Unknown action: ' + action);
    });
  };
  return ipc;
};

export const filterMinorActions = (actions) => {
  return actions.filter((action) => {
    if(action.type === 'CONNECTION_CHANGE' && action.payload.clientIp) {
      return false;
    }

    if(action.type === 'CONNECTION_CHANGE' && action.payload.isOnline) {
      return false;
    }
    
    if(action.type === 'USER_LOGIN_CHANGE' && action.payload.city) {
      return false;
    }

    return true;
  });
};