forked from LN-Zap/zap-desktop
/
neutrino.spec.js
174 lines (159 loc) · 6.08 KB
/
neutrino.spec.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import Neutrino from 'lib/lnd/neutrino'
jest.mock('electron', () => {
const { normalize } = require('path')
return {
app: {
getPath: name => normalize(`/tmp/zap-test/${name}`),
getAppPath: () => normalize('/tmp/zap-test')
}
}
})
describe('Neutrino', function() {
describe('Constructor', () => {
beforeAll(() => (this.neutrino = new Neutrino()))
describe('initial values', () => {
it('should set the "process" property to null', () => {
expect(this.neutrino.process).toEqual(null)
})
it('should set the "walletUnlockerGrpcActive" property to false', () => {
expect(this.neutrino.walletUnlockerGrpcActive).toEqual(false)
})
it('should set the "lightningGrpcActive" property to false', () => {
expect(this.neutrino.lightningGrpcActive).toEqual(false)
})
it('should set the "chainSyncStatus" property to "chain-sync-pending"', () => {
expect(this.neutrino.chainSyncStatus).toEqual('chain-sync-pending')
})
it('should set the "currentBlockHeight" property to 0', () => {
expect(this.neutrino.currentBlockHeight).toEqual(0)
})
it('should set the "lndBlockHeight" property to 0', () => {
expect(this.neutrino.lndBlockHeight).toEqual(0)
})
it('should set the "lndCfilterHeight" property to 0', () => {
expect(this.neutrino.lndCfilterHeight).toEqual(0)
})
it('should set the "lastError" property to be null', () => {
expect(this.neutrino.lastError).toEqual(null)
})
})
})
describe('.setState', () => {
describe('called with new state', () => {
beforeEach(() => {
this.neutrino = new Neutrino()
this.callback = jest.fn()
this.newVal = 'chain-sync-finished'
this.neutrino.on('chain-sync-finished', this.callback)
this.neutrino.setState(this.newVal)
})
it('should set the state', () => {
expect(this.neutrino.chainSyncStatus).toEqual(this.newVal)
})
it('should emit an event with the new state name', () => {
expect(this.callback).toHaveBeenCalledTimes(1)
})
})
describe('called with current state', () => {
beforeEach(() => {
this.neutrino = new Neutrino()
this.callback = jest.fn()
this.newVal = 'chain-sync-pending'
this.neutrino.on('chain-sync-pending', this.callback)
this.neutrino.setState(this.newVal)
})
it('should not change the state', () => {
expect(this.neutrino.chainSyncStatus).toEqual(this.newVal)
})
it('should not emit an event with the new state name', () => {
expect(this.callback).not.toHaveBeenCalled()
})
})
})
describe('.setCurrentBlockHeight', () => {
describe('called with higher height', () => {
beforeEach(() => {
this.neutrino = new Neutrino()
this.callback = jest.fn()
this.newVal = 100
this.neutrino.on('got-current-block-height', this.callback)
this.neutrino.setCurrentBlockHeight(this.newVal)
})
it('should change the current block height', () => {
expect(this.neutrino.currentBlockHeight).toEqual(this.newVal)
})
it('should emit an event with the new current block height', () => {
expect(this.callback).toHaveBeenCalledTimes(1)
expect(this.callback).toHaveBeenCalledWith(this.newVal)
})
})
describe('called with lower height', () => {
beforeEach(() => {
this.neutrino = new Neutrino()
this.callback = jest.fn()
this.newVal = -1
this.neutrino.on('got-current-block-height', this.callback)
this.neutrino.setCurrentBlockHeight(this.newVal)
})
it('should not change the current block height', () => {
expect(this.neutrino.currentBlockHeight).toEqual(0)
})
it('should not emit an event with the new current block height', () => {
expect(this.callback).not.toHaveBeenCalled()
})
})
})
describe('.setLndBlockHeight', () => {
describe('called with higher height', () => {
beforeEach(() => {
this.neutrino = new Neutrino()
this.callback = jest.fn()
this.newVal = 100
this.neutrino.on('got-lnd-block-height', this.callback)
this.neutrino.setCurrentBlockHeight = jest.fn()
this.neutrino.setLndBlockHeight(this.newVal)
})
it('should change the lnd block height', () => {
expect(this.neutrino.lndBlockHeight).toEqual(this.newVal)
})
it('should emit an event with the new lnd block height', () => {
expect(this.callback).toHaveBeenCalledTimes(1)
expect(this.callback).toHaveBeenCalledWith(this.newVal)
})
it('should call this.setCurrentBlockHeight', () => {
expect(this.neutrino.setCurrentBlockHeight).toHaveBeenCalledTimes(1)
expect(this.neutrino.setCurrentBlockHeight).toHaveBeenCalledWith(this.newVal)
})
})
describe('called with lower height', () => {
beforeEach(() => {
this.neutrino = new Neutrino()
this.callback = jest.fn()
this.newVal = -1
this.neutrino.on('got-lnd-block-height', this.callback)
this.neutrino.setLndBlockHeight(this.newVal)
this.neutrino.setCurrentBlockHeight = jest.fn()
})
it('should not change the lnd block height', () => {
expect(this.neutrino.lndBlockHeight).toEqual(0)
})
it('should not emit an event with the new lnd block height', () => {
expect(this.callback).not.toHaveBeenCalled()
})
it('should not call this.setCurrentBlockHeight', () => {
expect(this.neutrino.setCurrentBlockHeight).not.toHaveBeenCalled()
})
})
})
describe('.is', () => {
describe('called with current state', () => {
beforeEach(() => (this.neutrino = new Neutrino()))
it('should returnn true if the current state matches', () => {
expect(this.neutrino.is('chain-sync-pending')).toEqual(true)
})
it('should return false if the current state does not matche', () => {
expect(this.neutrino.is('some-other-state')).toEqual(false)
})
})
})
})