Skip to content

Commit

Permalink
test: added unit test for drain_delegate in contract api
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed Dec 1, 2022
1 parent 446aceb commit e3479df
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/taquito/test/contract/rpc-contract-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,36 @@ describe('RpcContractProvider test', () => {
});
});

describe('drainDelegate', () => {
it('should produce a drain operation', async (done) => {
const result = await rpcContractProvider.drainDelegate({
consensus_key: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
delegate: 'tz1MY8g5UqVmQtpAp7qs1cUwEof1GjZCHgVv',
destination: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
});

expect(result.raw).toEqual({
opbytes: 'test',
opOb: {
branch: 'test',
contents: [
{
kind: 'drain_delegate',
consensus_key: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
delegate: 'tz1MY8g5UqVmQtpAp7qs1cUwEof1GjZCHgVv',
destination: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
},
],
protocol: 'test_proto',
signature: 'test_sig',
},
counter: 0,
});

done();
});
});

describe('ballot', () => {
it('should produce a ballot operation', async (done) => {
const result = await rpcContractProvider.ballot({
Expand Down
137 changes: 137 additions & 0 deletions packages/taquito/test/operations/drain-delegate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { DrainDelegateOperation } from '../../src/operations';
import {
OperationContentsAndResult,
OpKind,
OperationContentsAndResultDrainDelegate,
} from '@taquito/rpc';
import { ForgedBytes } from '../../src/operations/types';
import { defaultConfigConfirmation } from '../../src/context';

describe('DrainDelegate operation', () => {
let fakeContext: any;

const fakeForgedBytes = {} as ForgedBytes;

const successfulResult = [
{
kind: 'drain_delegate',
consensus_key: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
delegate: 'tz1MY8g5UqVmQtpAp7qs1cUwEof1GjZCHgVv',
destination: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
metadata: {
balance_updates: [
{
kind: 'contract',
contract: 'tz1MY8g5UqVmQtpAp7qs1cUwEof1GjZCHgVv',
change: '-15525772494',
origin: 'block',
},
{
kind: 'contract',
contract: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
change: '15525772494',
origin: 'block',
},
{
kind: 'contract',
contract: 'tz1MY8g5UqVmQtpAp7qs1cUwEof1GjZCHgVv',
change: '-156825984',
origin: 'block',
},
{
kind: 'contract',
contract: 'tz1hoyMUiJYYr4FRPMU8Z7WJzYkqgjygjaTy',
change: '156825984',
origin: 'block',
},
],
},
},
] as OperationContentsAndResult[];

beforeEach(() => {
fakeContext = {
rpc: {
getBlock: jest.fn(),
},
config: { ...defaultConfigConfirmation },
};

fakeContext.rpc.getBlock.mockResolvedValue({
operations: [[{ hash: 'oo7gLAnWgTe7XtoYjgnkNBXDDvUy2e1DVcVwtvALxajx6KRzkXb' }], [], [], []],
header: {
level: 1,
},
});
});

it('should return OperationMetadataBalanceUpdates of DrainDelegate operation', () => {
const op = new DrainDelegateOperation(
'oo7gLAnWgTe7XtoYjgnkNBXDDvUy2e1DVcVwtvALxajx6KRzkXb',
{
kind: OpKind.DRAIN_DELEGATE,
consensus_key: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
delegate: 'tz1MY8g5UqVmQtpAp7qs1cUwEof1GjZCHgVv',
destination: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
},
fakeForgedBytes,
successfulResult,
fakeContext
);

expect(op.operationResults).toEqual(
(successfulResult[0] as OperationContentsAndResultDrainDelegate).metadata.balance_updates
);
});

it('should return consensusKey of DrainDelegate operation', () => {
const op = new DrainDelegateOperation(
'oo7gLAnWgTe7XtoYjgnkNBXDDvUy2e1DVcVwtvALxajx6KRzkXb',
{
kind: OpKind.DRAIN_DELEGATE,
consensus_key: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
delegate: 'tz1MY8g5UqVmQtpAp7qs1cUwEof1GjZCHgVv',
destination: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
},
fakeForgedBytes,
successfulResult,
fakeContext
);

expect(op.consensusKey).toEqual('tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj');
});

it('should return delegate of DrainDelegate operation', () => {
const op = new DrainDelegateOperation(
'oo7gLAnWgTe7XtoYjgnkNBXDDvUy2e1DVcVwtvALxajx6KRzkXb',
{
kind: OpKind.DRAIN_DELEGATE,
consensus_key: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
delegate: 'tz1MY8g5UqVmQtpAp7qs1cUwEof1GjZCHgVv',
destination: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
},
fakeForgedBytes,
successfulResult,
fakeContext
);

expect(op.delegate).toEqual('tz1MY8g5UqVmQtpAp7qs1cUwEof1GjZCHgVv');
});

it('should return destination of DrainDelegate operation', () => {
const op = new DrainDelegateOperation(
'oo7gLAnWgTe7XtoYjgnkNBXDDvUy2e1DVcVwtvALxajx6KRzkXb',
{
kind: OpKind.DRAIN_DELEGATE,
consensus_key: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
delegate: 'tz1MY8g5UqVmQtpAp7qs1cUwEof1GjZCHgVv',
destination: 'tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj',
},
fakeForgedBytes,
successfulResult,
fakeContext
);

expect(op.destination).toEqual('tz1KvJCU5cNdz5RAS3diEtdRvS9wfhRC7Cwj');
});
});

0 comments on commit e3479df

Please sign in to comment.