Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor BooleanCoder tests #944

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/moody-taxis-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
77 changes: 77 additions & 0 deletions packages/abi-coder/src/coders/boolean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as mathMod from '@fuel-ts/math';

import BooleanCoder from './boolean';

jest.mock('@fuel-ts/math', () => ({
__esModule: true,
...jest.requireActual('@fuel-ts/math'),
}));

describe('BooleanCoder', () => {
const TRUE_DECODED = true;
const TRUE_ENCODED = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1]);
const FALSE_DECODED = false;
const FALSE_ENCODED = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]);

const coder = new BooleanCoder();

it('should encode a true boolean', () => {
const expected = TRUE_ENCODED;
const actual = coder.encode(TRUE_DECODED);

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

it('should encode a false boolean', () => {
const expected = FALSE_ENCODED;
const actual = coder.encode(FALSE_DECODED);

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

it('should decode a true boolean', () => {
const expectedValue = TRUE_DECODED;
const expectedLength = TRUE_ENCODED.length;
const [actualValue, actualLength] = coder.decode(TRUE_ENCODED, 0);

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

it('should decode a false boolean', () => {
const expectedValue = FALSE_DECODED;
const expectedLength = FALSE_ENCODED.length;
const [actualValue, actualLength] = coder.decode(FALSE_ENCODED, 0);

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

it('should throw an error when encoding an invalid boolean value', () => {
const toBytesSpy = jest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to assign the spy to the toBytesSpy variable here.

.spyOn(mathMod, 'toBytes')
.mockReturnValueOnce(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1]));

expect(() => {
coder.encode(TRUE_DECODED);
}).toThrow('Invalid bool');
});

it('should throw an error when input to encode cannot be converted to bytes', () => {
const toBytesSpy = jest.spyOn(mathMod, 'toBytes').mockImplementationOnce(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to assign the spy to the toBytesSpy variable here.

throw new Error();
});

expect(() => {
coder.encode(TRUE_DECODED);
}).toThrow('Invalid bool');
});

it('should throw an error when decoding an invalid boolean value', () => {
const invalidInput = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 2]);

expect(() => {
coder.decode(invalidInput, 0);
}).toThrow('Invalid boolean value');
});
});