Files
api/packages/rpc-rx/src/cached.spec.ts
T
Jaco Greeff 34888bd4fb Remove /index imports (swap to bare) (#773)
* Remove /index imports (swap to bare)

* Bump common
2019-03-14 09:02:55 +01:00

63 lines
1.5 KiB
TypeScript

// Copyright 2017-2019 @polkadot/rpc-rx authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Observable } from 'rxjs';
import { RpcInterface$Section } from '@polkadot/rpc-core/types';
jest.mock('@polkadot/rpc-provider/ws', () => class {
isConnected = () => true;
on = () => true;
send = () => true;
});
import RpcRx from '.';
describe('createCachedObservable', () => {
let api: RpcRx;
let creator: (...params: Array<any>) => Observable<any>;
let section: RpcInterface$Section;
beforeEach(() => {
api = new RpcRx();
});
beforeEach(() => {
const subMethod: any = jest.fn(() => Promise.resolve(12345));
subMethod.unsubscribe = jest.fn(() => Promise.resolve(true));
section = {
subMethod
};
// @ts-ignore
creator = api.createObservable('subMethod', section);
});
it('creates a single observable', () => {
creator(123).subscribe();
expect(
section.subMethod
).toHaveBeenCalledWith(123, expect.anything());
});
it('creates a single observable (multiple calls)', () => {
const observable1 = creator(123);
const observable2 = creator(123);
expect(
observable2
).toBe(observable1);
});
it('creates multiple observables for different values', () => {
const observable1 = creator(123);
const observable2 = creator(456);
expect(
observable2
).not.toEqual(observable1);
});
});