summaryrefslogtreecommitdiffhomepage
path: root/test/mocks
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@codeispoetry.ru>2017-03-09 14:33:05 +0000
committerAndrej Mihajlov <and@codeispoetry.ru>2017-03-09 14:33:05 +0000
commit2e4756eaf97fd70c4917d01a1284887bdec28956 (patch)
tree035c41b684ea54d0901a2d04b3d06833d39a10b6 /test/mocks
parent4005868e4c28e526f26858f5721da7d50809d05e (diff)
downloadmullvadvpn-2e4756eaf97fd70c4917d01a1284887bdec28956.tar.xz
mullvadvpn-2e4756eaf97fd70c4917d01a1284887bdec28956.zip
- Add basic tests for AccountInput
- Add JSDOM
Diffstat (limited to 'test/mocks')
-rw-r--r--test/mocks/backend.js53
-rw-r--r--test/mocks/dom.js34
2 files changed, 87 insertions, 0 deletions
diff --git a/test/mocks/backend.js b/test/mocks/backend.js
new file mode 100644
index 0000000000..5cbf4630ba
--- /dev/null
+++ b/test/mocks/backend.js
@@ -0,0 +1,53 @@
+import configureMockStore from 'redux-mock-store';
+import thunk from 'redux-thunk';
+import Backend from '../app/lib/backend';
+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 = (store) => {
+ const backend = new Backend();
+
+ // patch backend
+ backend.syncWithReduxStore(store);
+
+ return backend;
+};
+
+export const filterIpUpdateActions = (actions) => {
+ return actions.filter((action) => {
+ if(action.type === 'CONNECTION_CHANGE' && action.payload.clientIp) {
+ return false;
+ }
+
+ if(action.type === 'USER_LOGIN_CHANGE' && action.payload.city) {
+ return false;
+ }
+
+ return true;
+ });
+};
diff --git a/test/mocks/dom.js b/test/mocks/dom.js
new file mode 100644
index 0000000000..9b72f25be2
--- /dev/null
+++ b/test/mocks/dom.js
@@ -0,0 +1,34 @@
+import jsdom from 'jsdom';
+
+global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
+global.window = document.defaultView;
+global.navigator = window.navigator;
+
+const keyMap = {
+ _1: { key: '1', which: 49, keyCode: 49 },
+ _2: { key: '2', which: 50, keyCode: 50 },
+ _3: { key: '3', which: 51, keyCode: 51 },
+ _4: { key: '4', which: 52, keyCode: 52 },
+ _5: { key: '5', which: 53, keyCode: 53 },
+ _6: { key: '6', which: 54, keyCode: 54 },
+ _7: { key: '7', which: 55, keyCode: 55 },
+ _8: { key: '8', which: 56, keyCode: 56 },
+ _9: { key: '9', which: 57, keyCode: 57 },
+ _0: { key: '0', which: 48, keyCode: 48 },
+ Tab: { which: 9, keyCode: 9 },
+ Enter: { which: 13, keyCode: 13 },
+ Backspace: { which: 8, keyCode: 8 }
+};
+
+export const KeyType = (() => {
+ let dict = {};
+ for(const i of Object.keys(keyMap)) {
+ dict[i] = i;
+ }
+
+ return dict;
+})();
+
+export function createKeyEvent(key) {
+ return Object.assign({ key }, keyMap[key]);
+} \ No newline at end of file