forked from HathorNetwork/hathor-wallet-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetupTests.js
107 lines (89 loc) · 2.67 KB
/
setupTests.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Copyright (c) Hathor Labs and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// Creating memory storage to be used in the place of localStorage
class MemoryOnlyStore {
constructor() {
this.hathorMemoryStorage = {};
}
getItem(key) {
const ret = this.hathorMemoryStorage[key];
if (ret === undefined) {
return null
}
return ret;
}
setItem(key, value) {
this.hathorMemoryStorage[key] = value;
}
removeItem(key) {
delete this.hathorMemoryStorage[key];
}
clear() {
this.hathorMemoryStorage = {};
}
}
// Mocking localStorage for tests
import 'jest-localstorage-mock';
const storage = require('./src/storage').default;
storage.setStore(new MemoryOnlyStore());
// Mocking WebSocket for tests
import { Server, WebSocket } from 'mock-socket';
global.WebSocket = WebSocket;
import helpers from './src/helpers';
storage.setItem('wallet:server', 'http://localhost:8080/');
let wsURL = helpers.getWSServerURL();
// Creating a ws mock server
const mockServer = new Server(wsURL);
mockServer.on('connection', socket => {
socket.on('message', data => {
let jsonData = JSON.parse(data);
if (jsonData.type === 'subscribe_address') {
// Only for testing purposes
socket.send(JSON.stringify({'type': 'subscribe_success', 'address': jsonData.address}));
} else if (jsonData.type === 'ping') {
socket.send(JSON.stringify({'type': 'pong'}));
}
});
});
// When using asyncronous test jest expect does not raise fail
// so we need to call done.fail() ourselves when some test is wrong
global.check = (realValue, expectedValue, doneCb) => {
if (expectedValue !== realValue) {
doneCb.fail(`${expectedValue} != ${realValue}`);
}
}
global.checkNot = (realValue, notExpectedValue, doneCb) => {
if (notExpectedValue === realValue) {
doneCb.fail(`${notExpectedValue} != ${realValue}`);
}
}
global.isObjectEmpty = (obj) => {
return Object.entries(obj).length === 0 && obj.constructor === Object
}
// Mocking axios
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
global.mock = new MockAdapter(axios);
// Default mock for /thin_wallet/address_history
mock.onGet('thin_wallet/address_history').reply((config) => {
return [200, {'history': []}];
});
mock.onGet('version').reply((config) => {
const data = {
version: '1.0.0',
network: 'mainnet',
min_tx_weight: 14,
min_tx_weight_coefficient: 1.6,
min_tx_weight_k: 100,
token_deposit_percentage: 0.01,
}
return [200, data];
});
import WS from './src/WebSocketHandler';
WS.WebSocket = WebSocket;
WS.setup();
global.window = {};