UNPKG

1.35 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';
7
8describe('subscribe', () => {
9 let state;
10
11 beforeEach(() => {
12 state = createState();
13 });
14
15 it('fails on unknown methods', () => {
16 return subscribe(state, 'test', 'test_notFound').catch((error) => {
17 expect(error.message).toMatch(/Invalid method 'test_notFound'/);
18 });
19 });
20
21 it('returns a subscription id', () => {
22 return subscribe(state, 'chain_newHead', 'chain_newHead', [() => void 0]).then((id) => {
23 expect(id).toEqual(state.subscriptionId);
24 });
25 });
26
27 it('stores the mapping values', () => {
28 const cb = () => void 0;
29
30 return subscribe(state, 'chain_newHead', 'chain_newHead', [cb]).then((id) => {
31 expect(state.subscriptionMap[id]).toEqual('chain_newHead');
32 expect(state.subscriptions['chain_newHead'].callbacks[id]).toEqual(cb);
33 });
34 });
35
36 it('calls back with the last known value', (done) => {
37 state.subscriptions['chain_newHead'].lastValue = 'testValue';
38
39 subscribe(state, 'chain_newHead', 'chain_newHead', [(_, value) => {
40 expect(value).toEqual('testValue');
41 done();
42 }]);
43 });
44});