|
1 | 1 | import { observable, ObservableMap, values } from 'mobx'; |
| 2 | +import * as LND from 'types/generated/lnd_pb'; |
2 | 3 | import { grpc } from '@improbable-eng/grpc-web'; |
3 | 4 | import { waitFor } from '@testing-library/react'; |
4 | 5 | import Big from 'big.js'; |
5 | 6 | import { BalanceMode } from 'util/constants'; |
6 | | -import { lndListChannels } from 'util/tests/sampleData'; |
| 7 | +import { lndChannelEvent, lndListChannels } from 'util/tests/sampleData'; |
7 | 8 | import { createStore, Store } from 'store'; |
8 | 9 | import Channel from 'store/models/channel'; |
9 | 10 | import ChannelStore from 'store/stores/channelStore'; |
@@ -118,4 +119,60 @@ describe('ChannelStore', () => { |
118 | 119 |
|
119 | 120 | expect(+store.totalOutbound).toBe(outbound); |
120 | 121 | }); |
| 122 | + |
| 123 | + describe('onChannelEvent', () => { |
| 124 | + const { |
| 125 | + OPEN_CHANNEL, |
| 126 | + CLOSED_CHANNEL, |
| 127 | + ACTIVE_CHANNEL, |
| 128 | + INACTIVE_CHANNEL, |
| 129 | + } = LND.ChannelEventUpdate.UpdateType; |
| 130 | + |
| 131 | + beforeEach(async () => { |
| 132 | + await store.fetchChannels(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should handle inactive channel event', async () => { |
| 136 | + const event = { ...lndChannelEvent, type: INACTIVE_CHANNEL }; |
| 137 | + const len = lndListChannels.channelsList.length; |
| 138 | + expect(store.activeChannels).toHaveLength(len); |
| 139 | + store.onChannelEvent(event); |
| 140 | + expect(store.activeChannels).toHaveLength(len - 1); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should handle active channel event', async () => { |
| 144 | + await store.fetchChannels(); |
| 145 | + const chan = store.channels.get(lndListChannels.channelsList[0].chanId) as Channel; |
| 146 | + chan.active = false; |
| 147 | + const event = { ...lndChannelEvent, type: ACTIVE_CHANNEL }; |
| 148 | + const len = lndListChannels.channelsList.length; |
| 149 | + expect(store.activeChannels).toHaveLength(len - 1); |
| 150 | + store.onChannelEvent(event); |
| 151 | + expect(store.activeChannels).toHaveLength(len); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should handle open channel event', async () => { |
| 155 | + const event = { ...lndChannelEvent, type: OPEN_CHANNEL }; |
| 156 | + event.openChannel.chanId = '12345'; |
| 157 | + expect(store.channels.get('12345')).toBeUndefined(); |
| 158 | + store.onChannelEvent(event); |
| 159 | + expect(store.channels.get('12345')).toBeDefined(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should handle close channel event', async () => { |
| 163 | + const event = { ...lndChannelEvent, type: CLOSED_CHANNEL }; |
| 164 | + const chanId = event.closedChannel.chanId; |
| 165 | + expect(store.channels.get(chanId)).toBeDefined(); |
| 166 | + store.onChannelEvent(event); |
| 167 | + expect(store.channels.get(chanId)).toBeUndefined(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should do nothing for unknown channel event type', async () => { |
| 171 | + const event = { ...lndChannelEvent, type: 99 }; |
| 172 | + const len = lndListChannels.channelsList.length; |
| 173 | + expect(store.activeChannels).toHaveLength(len); |
| 174 | + store.onChannelEvent(event as any); |
| 175 | + expect(store.activeChannels).toHaveLength(len); |
| 176 | + }); |
| 177 | + }); |
121 | 178 | }); |
0 commit comments