-
Notifications
You must be signed in to change notification settings - Fork 13
/
setupTests.js
77 lines (68 loc) · 2.31 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
/**
* 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.
*/
process.env.NODE_ENV = 'test';
// Mocking localStorage for tests
import 'jest-localstorage-mock';
import helpers from './src/utils/helpers';
// Mocking WebSocket for tests
import { Server, WebSocket } from 'mock-socket';
global.WebSocket = WebSocket;
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'}));
}
});
});
// 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, {'success': true, 'has_more': false, 'history': []}];
});
mock.onGet('version').reply((config) => ([200, {
version: '1.0.0',
network: 'testnet',
min_tx_weight: 14,
min_tx_weight_coefficient: 1.6,
min_tx_weight_k: 100,
token_deposit_percentage: 0.01,
}]));
expect.extend({
toMatchBuffer(received, expected) {
let pass;
if ((received instanceof Buffer === false) || (expected instanceof Buffer === false)) {
pass = false;
} else {
pass = expected.equals(received);
}
if (pass) {
return {
message: () => `expected Buffer(${received.toString('hex')}) to not match Buffer(${expected.toString('hex')})`,
pass: true,
}
} else {
return {
message: () => `expected Buffer(${received.toString('hex')}) to match Buffer(${expected.toString('hex')})`,
pass: false,
}
}
}
});
// jest test environment does not include the TextEncoder and TextDecoder glopbals which are required by Level (to test persistent storage with leveldb)
const { TextEncoder, TextDecoder } = require('util');
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;