ghost-portal/packages/ui-app/src/InputNumber.spec.js
Jaco Greeff fef9f61c65
Use ApiPromise instance, replace ApiObservable (#540)
* Expose ApiPromise, cleanup API props

* WIP

* First component via ApiPromise

* More items via ApiPromise

* More robust availability handling

* Disable routes based on API endpoint checks

* Handle promises

* Low-hanging fruit for call conversion

* Remove extra params

* Adjust rx components with children props

* Start pulling in derived values

* Additional derived

* Bump

* More derived values

* Add derive.balances.fees

* Additional account lookups

* derive lookup for Id -> Index mappings

* Cleanups, accountIdToIndex

* Rework for new API

* Update deps, all-ok after API bumps

* Add voting and validating balances

* Cleanup callback

* Democracy wrap-up

* 2019

* withApiPromise -> withApiCall

* Calculate referendum number

* Fix balances, withObservableBase -> withObservable

* Cleanup with actual interfaces

* withApiCall -> withCall

* Bumps

* api-observable types replaced

* Half-effort to adjust extrinsic creation

* No api-observable refs anymore

* Cleanup storage query app

* Cleanup call

* Voting works...

* Add Vote component

* Update API
2019-01-07 17:54:01 +01:00

81 lines
2.9 KiB
JavaScript

// Copyright 2017-2019 @polkadot/ui-app 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 BN from 'bn.js';
import React from 'react';
import { mount } from '../../../test/enzyme';
import { BitLengthOption } from './constants';
import Input from './Input';
import { InputNumber, maxConservativeLength } from './InputNumber';
const mockT = (key, options) => (key);
describe('InputNumber', () => {
const DEFAULT_BITLENGTH = BitLengthOption.CHAIN_SPEC;
const max128BN = new BN(2).pow(new BN(128)).subn(1);
const aboveMax128BN = max128BN.addn(1);
const belowMax128BN = max128BN.subn(1);
let wrapper, divInputNumber, defaultValue;
beforeEach(() => {
defaultValue = { value: '0' };
wrapper = mount(
<InputNumber
defaultValue={defaultValue}
t={mockT}
/>, {}
);
divInputNumber = wrapper.find('.ui--InputNumber');
});
it('creates the element', () => {
expect(wrapper).toHaveLength(1);
});
it('should display InputNumber wrapper and Input component', () => {
expect(divInputNumber).toHaveLength(1);
expect(wrapper.find(Input)).toHaveLength(1);
});
describe('maxLength', () => {
it('returns max length for use on number input fields that is the length of the max BN value minus a conservativeness factor for 128 bit', () => {
const maxValueLength = wrapper.instance().maxValue(DEFAULT_BITLENGTH).toString().length;
expect(maxConservativeLength(maxValueLength)).toBe(38);
});
});
describe('maxValue', () => {
it('returns max BN value for given bitlength or uses 128 bit bitlength fallback', () => {
expect(wrapper.instance().maxValue(128)).toEqual(max128BN);
});
});
describe('isValidBitLength', () => {
it('returns whether value in BN is valid for given bitlength or uses 128 bit bitlength fallback', () => {
expect(wrapper.instance().isValidBitLength(aboveMax128BN, DEFAULT_BITLENGTH)).toBe(false);
expect(wrapper.instance().isValidBitLength(max128BN, DEFAULT_BITLENGTH)).toBe(true);
});
});
describe('isValidNumber', () => {
describe('checks extrinsic balance', () => {
it('throws an error if input value for comparison is not a string', () => {
const invalidInputValueType = 340282366920938463463374607431768211456;
expect(() => { wrapper.instance().isValidNumber(new BN(invalidInputValueType), DEFAULT_BITLENGTH); }).toThrow();
});
});
it('should not be valid when user enters positive value greater than or equal to the 128 bit max for latest chain', () => {
expect(wrapper.instance().isValidNumber(max128BN, DEFAULT_BITLENGTH)).toBe(false);
});
it('should be valid when user enters positive value less than the 128 bit max for latest chain', () => {
expect(wrapper.instance().isValidNumber(belowMax128BN, DEFAULT_BITLENGTH)).toBe(true);
});
});
});