From d984fc4f03720eaa1d5f2fab9d886601726f6577 Mon Sep 17 00:00:00 2001 From: lotteam006 Date: Tue, 10 Sep 2024 16:08:02 +0800 Subject: [PATCH] add utils test case --- test/utils.test.ts | 95 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 test/utils.test.ts diff --git a/test/utils.test.ts b/test/utils.test.ts new file mode 100644 index 0000000..17998c1 --- /dev/null +++ b/test/utils.test.ts @@ -0,0 +1,95 @@ +import { validateAndParseAddress, parseBigintIsh, isEqualAddress, sortsBefore } from '../src/utils' +import JSBI from "jsbi"; +import { BigintIsh } from "../src/constants"; + +describe('validateAndParseAddress', () => { + + it('should return checksummed address if valid', () => { + const address = '0xAbCdEf0123456789aBcdef0123456789ABCDEF01' + const checksummedAddress = '0x000000000000000000000000abcdef0123456789abcdef0123456789abcdef01' + + const result = validateAndParseAddress(address) + expect(result).toBe(checksummedAddress) + }) + + it('should throw error if address is not valid', () => { + const address = 'invalid_address' + expect(() => validateAndParseAddress(address)).toThrowError(`${address} is not a valid address.`) + }) +}) + + +describe('parseBigintIsh', () => { + + it('should return the same JSBI instance if input is already JSBI', () => { + const jsbiValue = JSBI.BigInt('123') + const result = parseBigintIsh(jsbiValue) + expect(result).toBe(jsbiValue) + }) + + it('should convert bigint to JSBI', () => { + const bigintValue: BigintIsh = BigInt(123) + const result = parseBigintIsh(bigintValue) + expect(result).toEqual(JSBI.BigInt('123')) + }) + + it('should convert string to JSBI', () => { + const stringValue: BigintIsh = '123' + const result = parseBigintIsh(stringValue) + expect(result).toEqual(JSBI.BigInt('123')) + }) + + it('should handle large bigint values', () => { + const bigintValue: BigintIsh = BigInt('12345678901234567890') + const result = parseBigintIsh(bigintValue) + expect(result).toEqual(JSBI.BigInt('12345678901234567890')) + }) + + it('should handle large string values', () => { + const stringValue: BigintIsh = '12345678901234567890' + const result = parseBigintIsh(stringValue) + expect(result).toEqual(JSBI.BigInt('12345678901234567890')) + }) +}) + + +describe('isEqualAddress', () => { + it('should return true if two addresses are equal', () => { + const addressA = '0x123' + const addressB = '0x123' + + const result = isEqualAddress(addressA, addressB) + + expect(result).toBe(true) + }) + + it('should return false if two addresses are not equal', () => { + const addressA = '0x123' + const addressB = '0x456' + + const result = isEqualAddress(addressA, addressB) + + expect(result).toBe(false) + }) +}) + + +describe('sortsBefore', () => { + it('should return true if addressA is less than addressB', () => { + const addressA = '0x123' + const addressB = '0x456' + + const result = sortsBefore(addressA, addressB) + + expect(result).toBe(true) + }) + + it('should return false if addressA is greater than or equal to addressB', () => { + const addressA = '0x456' + const addressB = '0x123' + + const result = sortsBefore(addressA, addressB) + + expect(result).toBe(false) + }) +}) \ No newline at end of file