diff --git a/packages/apps/src/NodeInfo.tsx b/packages/apps/src/NodeInfo.tsx index eb2b32bd43..69503a430f 100644 --- a/packages/apps/src/NodeInfo.tsx +++ b/packages/apps/src/NodeInfo.tsx @@ -6,6 +6,7 @@ import { I18nProps } from '@polkadot/ui-app/types'; import React from 'react'; +import isTestChain from '@polkadot/ui-react-rx/util/isTestChain'; import classes from '@polkadot/ui-app/util/classes'; import keyring from '@polkadot/ui-keyring/index'; import Chain from '@polkadot/ui-react-rx/Chain'; @@ -17,7 +18,7 @@ import translate from './translate'; type Props = I18nProps & {}; function updateTestInfo (chain?: string) { - keyring.setTestMode(chain === 'dev'); + keyring.setTestMode(isTestChain(chain)); } function NodeInfo ({ className, style, t }: Props) { diff --git a/packages/ui-react-rx/src/Api/index.tsx b/packages/ui-react-rx/src/Api/index.tsx index 3d64ce7ec4..89ac439d10 100644 --- a/packages/ui-react-rx/src/Api/index.tsx +++ b/packages/ui-react-rx/src/Api/index.tsx @@ -8,6 +8,8 @@ import { EncodingVersions } from '@polkadot/params/types'; import { ApiProps } from '../types'; import React from 'react'; + +import shouldUseLatestChain from '@polkadot/ui-react-rx/util/shouldUseLatestChain'; import createWsProvider from '@polkadot/api-provider/ws'; import createApi from '@polkadot/api-rx'; import defaults from '@polkadot/api-rx/defaults'; @@ -26,9 +28,7 @@ type State = ApiProps & { }; function apiSupport (chain?: string): EncodingVersions { - return chain === undefined || chain === 'poc-1' - ? 'poc-1' - : 'latest'; + return shouldUseLatestChain(chain) ? 'latest' : 'poc-1'; } export default class Api extends React.PureComponent { diff --git a/packages/ui-react-rx/src/util/isTestChain.spec.js b/packages/ui-react-rx/src/util/isTestChain.spec.js new file mode 100644 index 0000000000..4f19e43154 --- /dev/null +++ b/packages/ui-react-rx/src/util/isTestChain.spec.js @@ -0,0 +1,24 @@ +// Copyright 2017-2018 @polkadot/ui-react-rx authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import isTestChain from './isTestChain'; + +describe('check chain spec to configure keyring test mode and dev accounts availability', () => { + it('enables test environment when chain specification matches text of dev or loc(al)', () => { + const validTestModeChainSpecsWithDev = ['Development disorder', 'my development', 'a devotion to']; + const validTestModeChainSpecsWithLoc = ['Local beer', 'one local beer', 'one good locust']; + + for (let s of validTestModeChainSpecsWithDev.concat(validTestModeChainSpecsWithLoc)) { + expect(isTestChain(s)).toEqual(true); + } + }); + + it('disables keyring test mode when chain specification is not a test mode or undefined or number type', () => { + const invalidTestModeChainSpecs = ['PoC-1 Testnet', 'Staging Testnet', 'future PoC-2 Testnet', 'a pocadot?', undefined, 0]; + + for (let s of invalidTestModeChainSpecs) { + expect(isTestChain(s)).toEqual(false); + } + }); +}); diff --git a/packages/ui-react-rx/src/util/isTestChain.ts b/packages/ui-react-rx/src/util/isTestChain.ts new file mode 100644 index 0000000000..2dd0f879ce --- /dev/null +++ b/packages/ui-react-rx/src/util/isTestChain.ts @@ -0,0 +1,17 @@ +// Copyright 2017-2018 @polkadot/ui-react-rx authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import isUndefined from '@polkadot/util/is/undefined'; + +const re = new RegExp('(dev|loc)', 'i'); + +export default function isTestChain (chain?: string): boolean { + if (isUndefined(chain)) { + return false; + } + + const match = re.test(chain.toString().toLowerCase()); + + return match ? true : false; +} diff --git a/packages/ui-react-rx/src/util/shouldUseLatestChain.spec.js b/packages/ui-react-rx/src/util/shouldUseLatestChain.spec.js new file mode 100644 index 0000000000..26f50103ff --- /dev/null +++ b/packages/ui-react-rx/src/util/shouldUseLatestChain.spec.js @@ -0,0 +1,25 @@ +// Copyright 2017-2018 @polkadot/ui-react-rx authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import shouldUseLatestChain from './shouldUseLatestChain'; + +describe('check chain spec to configure encoding', () => { + it('enables PoC-1 encoding when chain specification is PoC-1 or undefined', () => { + let validChainSpecWithPoC1 = ['try poc-1 now', 'testing PoC-1 Testnet']; + const validChainSpecWithUndefinedOrNumber = [undefined]; + + for (let s of validChainSpecWithPoC1.concat(validChainSpecWithUndefinedOrNumber)) { + expect(shouldUseLatestChain(s)).toEqual(false); + } + }); + + it('enables Latest encoding when chain specification is NOT PoC-1 or undefined', () => { + const invalidChainSpecWithoutPoC1OrUndefined = ['poc-2', 'PoC-2 Testnet', 'staging', + 'Staging Testnet', 'Local', 'Development', ' development', 'dev', 'Local', ' local ', 'loc', 0]; + + for (let s of invalidChainSpecWithoutPoC1OrUndefined) { + expect(shouldUseLatestChain(s)).toEqual(true); + } + }); +}); diff --git a/packages/ui-react-rx/src/util/shouldUseLatestChain.ts b/packages/ui-react-rx/src/util/shouldUseLatestChain.ts new file mode 100644 index 0000000000..376fa5fd01 --- /dev/null +++ b/packages/ui-react-rx/src/util/shouldUseLatestChain.ts @@ -0,0 +1,17 @@ +// Copyright 2017-2018 @polkadot/ui-react-rx authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import isUndefined from '@polkadot/util/is/undefined'; + +const re = new RegExp('(poc-1)', 'i'); + +export default function shouldUseLatestChain (chain?: string): boolean { + if (isUndefined(chain)) { + return false; + } + + const match = re.test(chain.toString().toLowerCase()); + + return match ? false : true; +}