Skip to content

Commit

Permalink
test: add convertHexToUtf8 unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
tiendn committed Aug 14, 2023
1 parent c42098d commit 79ffbdd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
20 changes: 19 additions & 1 deletion utils/__tests__/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from '@jest/globals'
import { convertBalanceToView, getAmountFromBignumber } from 'utils/helper'
import { convertBalanceToView, convertHexToUtf8, getAmountFromBignumber } from 'utils/helper'

describe('helper.getAmountFromBignumber() fnc', () => {
test('with normal bignumber', () => {
Expand Down Expand Up @@ -42,3 +42,21 @@ describe('helper.convertBalanceToView() fnc', () => {
expect(convertBalanceToView('1', '0')).toBe('1')
})
})

describe('helper.convertHexToUtf8() fnc', () => {
test('with hex string', () => {
expect(
convertHexToUtf8(
'0x4769e1baa369207468c6b0e1bb9f6e67204d696e6967616d65205468c3aa6d2062e1baa16e207468c3aa6d20415341'
).replace(/\0/g, '') // remove null character from received string
).toBe('Giải thưởng Minigame Thêm bạn thêm ASA')
})

test('with empty string', () => {
expect(convertHexToUtf8('')).toBe('')
})

test('with null', () => {
expect(convertHexToUtf8(null)).toBe('')
})
})
1 change: 1 addition & 0 deletions utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export function convertBigNumberToString(x: any) {
export const isMainnet = process.env.NEXT_PUBLIC_ENV === 'mainnet'

export const convertHexToUtf8 = (hex: string) => {
if (hex === null || hex === undefined || hex === '') return ''
var s = ''
for (var i = 0; i < hex.length; i += 2) {
s += String.fromCharCode(parseInt(hex.substr(i, 2), 16))
Expand Down

0 comments on commit 79ffbdd

Please sign in to comment.