Skip to content

Commit

Permalink
test: refactor OptionCoder tests (#1006)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate authored May 17, 2023
1 parent 75002fe commit 41faf45
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .changeset/clean-squids-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
54 changes: 54 additions & 0 deletions packages/abi-coder/src/coders/option.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { BN } from '@fuel-ts/math';

import { U8_MAX } from '../../test/utils/constants';

import OptionCoder from './option';
import U64Coder from './u64';

describe('OptionCoder', () => {
it('should encode a some u64 option ', () => {
const coder = new OptionCoder('test option', { Some: new U64Coder() });
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255]);
// @ts-expect-error
const actual = coder.encode(U8_MAX);

expect(actual).toStrictEqual(expected);
});

it('should decode a some u64 option', () => {
const coder = new OptionCoder('test option', { Some: new U64Coder() });
const expectedValue = U8_MAX;
const expectedLength = 16;
const [actualValue, actualLength] = coder.decode(
new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255]),
0
);

expect(actualValue).toBeInstanceOf(BN);
expect((actualValue as unknown as BN).toNumber()).toBe(expectedValue);
expect(actualLength).toBe(expectedLength);
});

it('should encode a none u64 option', () => {
const coder = new OptionCoder('test option', { None: new U64Coder() });
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
// @ts-expect-error
const actual = coder.encode(undefined);

expect(actual).toStrictEqual(expected);
});

it('should decode a none u64 option', () => {
const coder = new OptionCoder('test option', { None: new U64Coder() });
const expectedValue = undefined;
const expectedLength = 16;
const [actualValue, actualLength] = coder.decode(
new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
0
);

expect(actualValue).toBe(expectedValue);
expect(actualLength).toBe(expectedLength);
});
});

0 comments on commit 41faf45

Please sign in to comment.