Skip to content

Commit 53f29da

Browse files
committed
test: add unit tests for getMultipleStates method
1 parent b1c74d2 commit 53f29da

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

libraries/fabric-shim/lib/handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ class ChaincodeMessageHandler {
439439

440440
if (responseMsg.getType() === peer.ChaincodeMessage.Type.RESPONSE) {
441441
const result = peer.GetStateMultipleResult.deserializeBinary(responseMsg.getPayload());
442-
return result.getValuesList();
442+
return result.getValuesList();
443443
}
444444

445445
if (responseMsg.getType() === peer.ChaincodeMessage.Type.ERROR) {

libraries/fabric-shim/test/unit/stub.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,56 @@ describe('Stub', () => {
612612
});
613613
});
614614

615+
describe.only('getMultipleStates', () => {
616+
it('should return handler.handleGetMultipleStates results', async () => {
617+
const handleGetMultipleStatesStub = sinon.stub().resolves(['value1', 'value2', 'value3']);
618+
619+
const stub = new Stub({
620+
handleGetMultipleStates: handleGetMultipleStatesStub
621+
}, 'dummyChannelId', 'dummyTxid', chaincodeInput);
622+
623+
const result = await stub.getMultipleStates('key1', 'key2', 'key3');
624+
expect(result).to.deep.equal(['value1', 'value2', 'value3']);
625+
626+
expect(handleGetMultipleStatesStub.calledOnce).to.be.true;
627+
expect(handleGetMultipleStatesStub.firstCall.args).to.deep.equal([
628+
'',
629+
['key1', 'key2', 'key3'],
630+
'dummyChannelId',
631+
'dummyTxid'
632+
]);
633+
});
634+
635+
it('should return empty array if no keys passed', async () => {
636+
const handleGetMultipleStatesStub = sinon.stub().resolves([]);
637+
638+
const stub = new Stub({
639+
handleGetMultipleStates: handleGetMultipleStatesStub
640+
}, 'dummyChannelId', 'dummyTxid', chaincodeInput);
641+
642+
const result = await stub.getMultipleStates();
643+
expect(result).to.deep.equal([]);
644+
645+
expect(handleGetMultipleStatesStub.calledOnce).to.be.true;
646+
expect(handleGetMultipleStatesStub.firstCall.args).to.deep.equal([
647+
'',
648+
[],
649+
'dummyChannelId',
650+
'dummyTxid'
651+
]);
652+
});
653+
654+
it('should throw error if handler rejects', async () => {
655+
const handleGetMultipleStatesStub = sinon.stub().rejects(new Error('Something gone wrong'));
656+
657+
const stub = new Stub({
658+
handleGetMultipleStates: handleGetMultipleStatesStub
659+
}, 'dummyChannelId', 'dummyTxid', chaincodeInput);
660+
661+
await expect(stub.getMultipleStates('key1')).to.be.rejectedWith('Something gone wrong');
662+
});
663+
});
664+
615665
describe('putState', () => {
616666
it ('should return handler.handlePutState', async () => {
617667
const handlePutStateStub = sinon.stub().resolves('some state');

0 commit comments

Comments
 (0)