parseAttestationBytes.spec.ts 2.81 KB
Newer Older
Will Cory's avatar
Will Cory committed
1 2 3 4 5
import { BigNumber } from 'ethers'
import { toUtf8Bytes } from 'ethers/lib/utils.js'
import { expect, describe, it } from 'vitest'

import { WagmiBytes } from '../types/WagmiBytes'
Will Cory's avatar
Will Cory committed
6 7 8 9 10 11 12
import {
  parseNumber,
  parseAddress,
  parseBool,
  parseString,
  parseAttestationBytes,
} from './parseAttestationBytes'
Will Cory's avatar
Will Cory committed
13 14 15 16 17 18 19 20 21 22 23

describe(parseAttestationBytes.name, () => {
  it('works for strings', () => {
    const str = 'Hello World'
    const bytes = BigNumber.from(toUtf8Bytes(str)).toHexString() as WagmiBytes
    expect(parseAttestationBytes(bytes, 'string')).toBe(str)
  })

  it('works for numbers', () => {
    const num = 123
    const bytes = BigNumber.from(num).toHexString() as WagmiBytes
Will Cory's avatar
Will Cory committed
24 25 26 27 28 29
    expect(parseAttestationBytes(bytes, 'number')).toMatchInlineSnapshot(`
      {
        "hex": "0x7b",
        "type": "BigNumber",
      }
    `)
Will Cory's avatar
Will Cory committed
30 31 32 33 34 35 36 37 38 39
  })

  it('works for addresses', () => {
    const addr = '0x1234567890123456789012345678901234567890'
    const bytes = BigNumber.from(addr).toHexString() as WagmiBytes
    expect(parseAttestationBytes(bytes, 'address')).toBe(addr)
  })

  it('works for booleans', () => {
    const bytes = BigNumber.from(1).toHexString() as WagmiBytes
Will Cory's avatar
Will Cory committed
40
    expect(parseAttestationBytes(bytes, 'bool')).toBe(true)
Will Cory's avatar
Will Cory committed
41 42 43
  })

  it('should work for raw bytes', () => {
Will Cory's avatar
Will Cory committed
44 45 46 47 48
    expect(parseAttestationBytes('0x420', 'bytes')).toMatchInlineSnapshot(
      '"0x420"'
    )
    expect(parseAttestationBytes('0x', 'string')).toMatchInlineSnapshot('""')
    expect(parseAttestationBytes('0x0', 'string')).toMatchInlineSnapshot('""')
Will Cory's avatar
Will Cory committed
49 50 51 52 53 54 55 56
  })

  it('should return raw bytes for invalid type', () => {
    const bytes = '0x420'
    // @ts-expect-error - this is a test for an error case
    expect(parseAttestationBytes(bytes, 'foo')).toBe(bytes)
  })
})
Will Cory's avatar
Will Cory committed
57 58 59 60 61 62

describe('parseFoo', () => {
  it('works for strings', () => {
    const str = 'Hello World'
    const bytes = BigNumber.from(toUtf8Bytes(str)).toHexString() as WagmiBytes
    expect(parseString(bytes)).toBe(str)
Will Cory's avatar
Will Cory committed
63 64 65
    expect(parseString('0x')).toMatchInlineSnapshot('""')
    expect(parseString('0x0')).toMatchInlineSnapshot('""')
    expect(parseString('0x0')).toMatchInlineSnapshot('""')
Will Cory's avatar
Will Cory committed
66 67 68 69 70 71
  })

  it('works for numbers', () => {
    const num = 123
    const bytes = BigNumber.from(num).toHexString() as WagmiBytes
    expect(parseNumber(bytes)).toEqual(BigNumber.from(num))
Will Cory's avatar
Will Cory committed
72
    expect(parseNumber('0x')).toEqual(BigNumber.from(0))
Will Cory's avatar
Will Cory committed
73 74 75 76 77 78 79 80 81 82 83
  })

  it('works for addresses', () => {
    const addr = '0x1234567890123456789012345678901234567890'
    const bytes = BigNumber.from(addr).toHexString() as WagmiBytes
    expect(parseAddress(bytes)).toBe(addr)
  })

  it('works for booleans', () => {
    const bytes = BigNumber.from(1).toHexString() as WagmiBytes
    expect(parseBool(bytes)).toBe(true)
Will Cory's avatar
Will Cory committed
84 85 86
    expect(parseBool('0x')).toBe(false)
    expect(parseBool('0x0')).toBe(false)
    expect(parseBool('0x00000')).toBe(false)
Will Cory's avatar
Will Cory committed
87 88
  })
})