forked from web3/web3.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'issues/2378' into issue/2356
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/web3-core-method/src/methods/network/ChainIdMethod.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
/** | ||
* @file ListeningMethod.js | ||
* @author Samuel Furter <[email protected]> | ||
* @date 2018 | ||
*/ | ||
|
||
import AbstractCallMethod from '../../../lib/methods/AbstractCallMethod'; | ||
|
||
export default class ChainIdMethod extends AbstractCallMethod { | ||
/** | ||
* @param {Utils} utils | ||
* @param {Object} formatters | ||
* | ||
* @constructor | ||
*/ | ||
constructor(utils, formatters) { | ||
super('eth_chainId', 0, utils, formatters); | ||
} | ||
|
||
/** | ||
* This method will be executed after the RPC request. | ||
* | ||
* @method afterExecution | ||
* | ||
* @param {Object} response | ||
* | ||
* @returns {Number} | ||
*/ | ||
afterExecution(response) { | ||
return this.utils.hexToNumber(response); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/web3-core-method/tests/src/methods/network/ChainIdMethodTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as Utils from 'web3-utils'; | ||
import AbstractCallMethod from '../../../../lib/methods/AbstractCallMethod'; | ||
import ChainIdMethod from '../../../../src/methods/network/ChainIdMethod'; | ||
|
||
// Mocks | ||
jest.mock('Utils'); | ||
|
||
/** | ||
* PeerCountMethod test | ||
*/ | ||
describe('PeerCountMethodTest', () => { | ||
let method; | ||
|
||
beforeEach(() => { | ||
method = new ChainIdMethod(Utils, null); | ||
}); | ||
|
||
it('constructor check', () => { | ||
expect(method).toBeInstanceOf(AbstractCallMethod); | ||
|
||
expect(method.rpcMethod).toEqual('eth_chainId'); | ||
|
||
expect(method.parametersAmount).toEqual(0); | ||
|
||
expect(method.utils).toEqual(Utils); | ||
|
||
expect(method.formatters).toEqual(null); | ||
}); | ||
|
||
it('afterExecution should map the response', () => { | ||
Utils.hexToNumber.mockReturnValueOnce(61); | ||
|
||
expect(method.afterExecution('0x0')).toEqual(61); | ||
|
||
expect(Utils.hexToNumber).toHaveBeenCalledWith('0x0'); | ||
}); | ||
}); |