"use strict";

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

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

var _util = require("@polkadot/util");

var _createFunction = _interopRequireDefault(require("./createFunction"));

// Copyright 2017-2019 @polkadot/api-metadata 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('createFunction', () => {
  it('should create timestamp.now correctly', () => {
    expect((0, _createFunction.default)({
      prefix: 'Timestamp',
      section: 'timestamp',
      method: 'Now',
      meta: {
        type: {}
      }
    })()).toEqual(Uint8Array.from([64, 14, 73, 68, 207, 217, 141, 111, 76, 195, 116, 209, 111, 90, 78, 63, 156]) // Length-prefixed
    );
  });
  it('allows overrides on key (keeping name)', () => {
    expect((0, _createFunction.default)({
      prefix: 'Substrate',
      section: 'substrate',
      method: 'authorityCount',
      meta: {
        type: {}
      }
    }, {
      key: ':auth:len',
      skipHashing: true
    }).method).toEqual('authorityCount');
  });
  it('allows overrides on key (unhashed)', () => {
    const key = ':auth:len';
    expect((0, _createFunction.default)({
      prefix: 'Substrate',
      section: 'substrate',
      method: 'authorityCount',
      meta: {
        type: {}
      }
    }, {
      key,
      skipHashing: true
    })()).toEqual((0, _util.u8aConcat)(Uint8Array.from([key.length << 2]), (0, _util.stringToU8a)(':auth:len')));
  });
  describe('the created double map function', () => {
    let storageFn;
    beforeAll(() => {
      storageFn = (0, _createFunction.default)({
        prefix: 'GenericAsset',
        section: 'genericAsset',
        method: 'FreeBalance',
        meta: {
          name: 'metaName',
          type: {
            isDoubleMap: true,
            asDoubleMap: {
              hasher: new _types.StorageHasher('Blake2_256'),
              key1: new _types.Text('AccountId'),
              key2: new _types.Text('AccountId'),
              value: new _types.Text('Balance'),
              key2Hasher: new _types.Text('twox_128')
            }
          }
        }
      });
    });
    it('should return correct key', () => {
      const result = storageFn(['5DXUeE5N5LtkW97F2PzqYPyqNkxqSWESdGSPTX6AvkUAhwKP', '5DXUeE5N5LtkW97F2PzqYPyqNkxqSWESdGSPTX6AvkUAhwKP']);
      expect((0, _util.u8aToHex)(result)).toEqual('0xc000fa40e72d7173e69ee54b980345ea01cb81e64258502e0247af4303dee91ec0aec2ecd3a60ab080cff7b52a8f6d543b');
    });
    it('needs two arguments', () => {
      expect(() => storageFn(['5DXUeE5N5LtkW97F2PzqYPyqNkxqSWESdGSPTX6AvkUAhwKP'])).toThrow(/requires two arguments/);
    });
  });
  it('allows creates double map function with a Null type key', () => {
    const storageFn = (0, _createFunction.default)({
      prefix: 'System',
      section: 'system',
      method: 'EventTopics',
      meta: {
        type: {
          isDoubleMap: true,
          asDoubleMap: {
            hasher: new _types.StorageHasher('Blake2_256'),
            key1: new _types.Text('Null'),
            key2: new _types.Text('Hash'),
            value: new _types.Text('Vec<(BlockNumber,EventIndex)>'),
            key2Hasher: new _types.Text('blake2_256')
          }
        }
      }
    }); // the value of the Null type key does not effect the result

    expect((0, _util.u8aToHex)(storageFn(['any', [1, 2, 3]]))).toEqual((0, _util.u8aToHex)(storageFn([[1, 2, 3], [1, 2, 3]]))); // the value of the not Null type key does effect the result

    expect((0, _util.u8aToHex)(storageFn(['any', [1, 2, 3]]))).not.toEqual((0, _util.u8aToHex)(storageFn(['any', [1, 2, 3, 4]])));
  });
});