UNPKG

1.38 kBJavaScriptView Raw
1// Copyright 2017-2018 @polkadot/api-provider authors & contributors
2// This software may be modified and distributed under the terms
3// of the ISC license. See the LICENSE file for details.
4
5import createState from './state';
6import subscribe from './subscribe';
7import unsubscribe from './unsubscribe';
8
9describe('unsubscribe', () => {
10 let state;
11 let id;
12
13 beforeEach(() => {
14 state = createState();
15
16 return subscribe(state, 'chain_newHead', 'chain_newHead', [() => void 0]).then((_id) => {
17 id = _id;
18 });
19 });
20
21 it('fails on unknown ids', () => {
22 return unsubscribe(state, 'chain_newHead', 'chain_newHead', 5).catch((error) => {
23 expect(error.message).toMatch(/Unable to find/);
24 });
25 });
26
27 it('unsubscribes successfully', () => {
28 return unsubscribe(state, 'chain_newHead', 'chain_newHead', id);
29 });
30
31 it('fails on double unsubscribe', () => {
32 return unsubscribe(state, 'chain_newHead', 'chain_newHead', id)
33 .then(() => unsubscribe(state, id))
34 .catch((error) => {
35 expect(error.message).toMatch(/Unable to find/);
36 });
37 });
38
39 it('clears the subscriptions', () => {
40 return unsubscribe(state, 'chain_newHead', 'chain_newHead', id).then(() => {
41 expect(state.subscriptionMap[id]).not.toBeDefined();
42 expect(state.subscriptions['chain_newHead'].callbacks[id]).not.toBeDefined();
43 });
44 });
45});