Skip to content

Commit

Permalink
fix: Fixes polkadot-js#130 latest rust reports chains differently (po…
Browse files Browse the repository at this point in the history
…lkadot-js#133)

* fix: Detect chain specs that enable test environment and dev accounts Issue polkadot-js#130

* test: refactor moving chain checker into meaningful function name

* fix: Detect check specs requiring backward compatibility encoding for Issue polkadot-js#130

* remove comment

* remove comment and restore source maps

* fix: Fix the license information in test files

* refactor: Move cases into array with meaningful array. Cater for numbers

* refactor: Reduce how condensed the code is

* refactor: Use Polkadot isUndefined helper

* refactor: Move RegEx instantiation outside function so not called every time

* refactor: Move new utility functions and associated tests into ui-react-rx/src/util

* fix: Update to cater for renaming of poc-2 to staging

See commit updates since `poc-2` has been renamed to `staging` paritytech/polkadot@3426552
  • Loading branch information
ltfschoen authored and jacogr committed Jul 11, 2018
1 parent b96c450 commit 62b1158
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/apps/src/NodeInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions packages/ui-react-rx/src/Api/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<Props, State> {
Expand Down
24 changes: 24 additions & 0 deletions packages/ui-react-rx/src/util/isTestChain.spec.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
17 changes: 17 additions & 0 deletions packages/ui-react-rx/src/util/isTestChain.ts
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 25 additions & 0 deletions packages/ui-react-rx/src/util/shouldUseLatestChain.spec.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
17 changes: 17 additions & 0 deletions packages/ui-react-rx/src/util/shouldUseLatestChain.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 62b1158

Please sign in to comment.