From a02c47b09fc38a9c7b5c3df9895fa98bb5a225d5 Mon Sep 17 00:00:00 2001 From: sugh01 <19183308+sugh01@users.noreply.github.com> Date: Fri, 19 Jan 2024 00:39:21 +0100 Subject: [PATCH] added few encodeAbi tests --- .../src/utils/encoders/encodeAbi.test.ts | 28 +++++++++++++++++++ .../src/utils/encoders/encodeSetAbi.test.ts | 2 +- .../src/utils/generateRecordCallArray.test.ts | 27 ++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/ensjs/src/utils/encoders/encodeAbi.test.ts b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts index dd0a3c3f..b4e6a246 100644 --- a/packages/ensjs/src/utils/encoders/encodeAbi.test.ts +++ b/packages/ensjs/src/utils/encoders/encodeAbi.test.ts @@ -12,6 +12,13 @@ describe('encodeAbi', () => { expect(result.encodedData).toEqual('0x7b22666f6f223a22626172227d') }) + // Null JSON data + it('encodes null JSON data', async () => { + const result = await encodeAbi({ encodeAs: 'json', data: null }) + expect(result.contentType).toEqual(1) + expect(result.encodedData).toEqual('0x') + }) + it('encodes data as zlib', async () => { const data = { foo: 'bar' } const result = await encodeAbi({ encodeAs: 'zlib', data }) @@ -21,6 +28,13 @@ describe('encodeAbi', () => { ) }) + // Null zlib data + it('encodes null zlib data', async () => { + const result = await encodeAbi({ encodeAs: 'zlib', data: null }) + expect(result.contentType).toEqual(2) + expect(result.encodedData).toEqual('0x') + }) + it('encodes data as cbor', async () => { const data = { foo: 'bar' } const result = await encodeAbi({ encodeAs: 'cbor', data }) @@ -28,12 +42,26 @@ describe('encodeAbi', () => { expect(result.encodedData).toEqual('0xa163666f6f63626172') }) + // Null CBOR data + it('encodes null CBOR data', async () => { + const result = await encodeAbi({ encodeAs: 'cbor', data: null }) + expect(result.contentType).toEqual(4) + expect(result.encodedData).toEqual('0x') + }) + it('encodes data as uri', async () => { const data = 'foo=bar' const result = await encodeAbi({ encodeAs: 'uri', data }) expect(result.contentType).toEqual(8) expect(result.encodedData).toEqual('0x666f6f3d626172') }) + + // Null URI data + it('encodes null URI data', async () => { + const result = await encodeAbi({ encodeAs: 'uri', data: null }) + expect(result.contentType).toEqual(8) + expect(result.encodedData).toEqual('0x') + }) }) describe('encodeAsToContentType', () => { diff --git a/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts index 65ddd4ec..046d95f8 100644 --- a/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts +++ b/packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts @@ -13,7 +13,7 @@ describe('encodeSetAbi', () => { encodedData, } - it('encodes the setAbi function data correctly', () => { + it('encodes the setAbi function data correctly with null encodedData', async () => { const expected = '0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000' const result = encodeSetAbi(parameters) diff --git a/packages/ensjs/src/utils/generateRecordCallArray.test.ts b/packages/ensjs/src/utils/generateRecordCallArray.test.ts index bfc5b31e..e50f21ef 100644 --- a/packages/ensjs/src/utils/generateRecordCallArray.test.ts +++ b/packages/ensjs/src/utils/generateRecordCallArray.test.ts @@ -1,4 +1,5 @@ import { generateRecordCallArray } from './generateRecordCallArray.js' +import { encodeAbi } from './index.js' import { namehash } from './normalise.js' it('generates a record call array', () => { @@ -78,3 +79,29 @@ it('adds coin calls when coins array is defined and not empty', () => { ] `) }) +it('adds abi call when data is null', async () => { + const result = await encodeAbi({ encodeAs: 'uri', data: null }) + expect( + generateRecordCallArray({ + namehash: namehash('test.eth'), + abi: result, + }), + ).toMatchInlineSnapshot(` + [ + "0x623195b0eb4f647bea6caa36333c816d7b46fdcb05f9466ecacc140ea8c66faf15b3d9f1000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000", + ] + `) +}) +it('adds abi call when data is not empty', async () => { + const result = await encodeAbi({ encodeAs: 'json', data: { foo: 'bar' } }) + expect( + generateRecordCallArray({ + namehash: namehash('test.eth'), + abi: result, + }), + ).toMatchInlineSnapshot(` + [ + "0x623195b0eb4f647bea6caa36333c816d7b46fdcb05f9466ecacc140ea8c66faf15b3d9f100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000d7b22666f6f223a22626172227d00000000000000000000000000000000000000", + ] + `) +})