Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed May 24, 2024
1 parent 46cc00b commit a6f1494
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/abi-coder/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {
}

decodeLog(data: BytesLike, logId: string): any {
const loggedType = this.jsonAbi.loggedTypes.find((type) => type.logId.toString() === logId);
const loggedType = this.jsonAbi.loggedTypes.find((type) => type.logId === logId);
if (!loggedType) {
throw new FuelError(
ErrorCode.LOG_TYPE_NOT_FOUND,
Expand Down
45 changes: 31 additions & 14 deletions packages/abi-coder/test/Interface.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
import type { BN } from '@fuel-ts/math';
import { concat } from '@fuel-ts/utils';
import { concat, hexlify } from '@fuel-ts/utils';

import { Interface } from '../src';
/** @knipignore */
Expand Down Expand Up @@ -769,39 +771,54 @@ describe('Abi interface', () => {

describe('decodeLog', () => {
it('should return decoded log by id', () => {
const data = exhaustiveExamplesInterface.decodeLog('0x01000000000000000000000000000020', '0');
const data = exhaustiveExamplesInterface.decodeLog(
hexlify(Uint8Array.from([1, 0, 0, 0, 32])),
'8500535089865083573'
);
expect(data).toEqual({
a: true,
b: 32,
});
});

it('should throw an error when log does not exist', () => {
expect(() =>
exhaustiveExamplesInterface.decodeLog('0x01000000000000000000000000000020', '1')
).toThrowError(`Log type with logId '1' doesn't exist in the ABI.`);
it('should throw an error when log does not exist', async () => {
await expectToThrowFuelError(
() => {
exhaustiveExamplesInterface.decodeLog(
hexlify(Uint8Array.from([1, 0, 0, 0, 32])),
'8500535089865083573'
);
},
new FuelError(
ErrorCode.LOG_TYPE_NOT_FOUND,
`Log type with logId '8500535089865083573' doesn't exist in the ABI.`
)
);
});
});

describe('decodeFunctionResult', () => {
it('should return decoded function result', () => {
const data = exhaustiveExamplesInterface.decodeFunctionResult(
'struct_simple',
'0x01000000000000000000000000000020'
hexlify(Uint8Array.from([1, 0, 0, 0, 32]))
);
expect(data).toEqual({
a: true,
b: 32,
});
});

it('should throw an error when function does not exist', () => {
expect(() => {
exhaustiveExamplesInterface.decodeFunctionResult(
'doesnt_exist',
'0x01000000000000000000000000000020'
);
}).toThrowError(/^Function doesnt_exist not found\.$/);
it('should throw an error when function does not exist', async () => {
await expectToThrowFuelError(
() => {
exhaustiveExamplesInterface.decodeFunctionResult(
'doesnt_exist',
hexlify(Uint8Array.from([1, 0, 0, 0, 32]))
);
},
new FuelError(ErrorCode.FUNCTION_NOT_FOUND, `Function doesnt_exist not found.`)
);
});
});
});

0 comments on commit a6f1494

Please sign in to comment.