"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _testingPairs = _interopRequireDefault(require("@polkadot/keyring/testingPairs"));

var _mock = _interopRequireDefault(require("@polkadot/rpc-provider/mock"));

var _types = require("@polkadot/types");

var _ = _interopRequireDefault(require("."));

// Copyright 2017-2019 @polkadot/rpc-core authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
describe('Cached Observables', () => {
  let rpc;
  const keyring = (0, _testingPairs.default)();
  beforeEach(() => {
    rpc = new _.default(new _mock.default());
  });
  it('creates a single observable for subscriptions (multiple calls)', () => {
    const observable1 = rpc.state.subscribeStorage([123]);
    const observable2 = rpc.state.subscribeStorage([123]);
    expect(observable2).toBe(observable1);
  });
  it('creates a single observable for subscriptions (multiple calls, no arguments)', () => {
    const observable1 = rpc.chain.subscribeNewHeads();
    const observable2 = rpc.chain.subscribeNewHeads();
    expect(observable2).toBe(observable1);
  });
  it('creates a single observable (multiple calls, different arguments that should be cached together)', () => {
    const observable1 = rpc.state.subscribeStorage([keyring.alice.address]);
    const observable2 = rpc.state.subscribeStorage([(0, _types.createType)('AccountId', keyring.alice.address)]);
    expect(observable2).toBe(observable1);
  });
  it('creates multiple observables for different values', () => {
    const observable1 = rpc.chain.getBlockHash(123);
    const observable2 = rpc.chain.getBlockHash(456);
    expect(observable2).not.toBe(observable1);
  });
  it('clears cache if there are no more subscribers', () => {
    const observable1 = rpc.chain.subscribeNewHeads();
    const observable2 = rpc.chain.subscribeNewHeads();
    expect(observable1).toBe(observable2);
    const sub1 = observable1.subscribe();
    const sub2 = observable2.subscribe();
    sub1.unsubscribe();
    sub2.unsubscribe(); // No more subscribers, now create a new observable

    const observable3 = rpc.chain.subscribeNewHeads();
    expect(observable3).not.toBe(observable1);
  });
  it('creates different observables for different methods but same arguments', () => {
    // params do not match here
    const observable1 = rpc.chain.subscribeNewHeads([123]);
    const observable2 = rpc.state.subscribeStorage([123]);
    expect(observable2).not.toBe(observable1);
  });
  it('creates multiple observables for one-shots', () => {
    const observable1 = rpc.chain.getBlockHash(123);
    const observable2 = rpc.chain.getBlockHash(123);
    expect(observable2).not.toBe(observable1);
  });
});