parseAttestationBytes.spec.ts 2.3 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 44 45 46 47 48 49 50 51 52 53
  })

  it('should work for raw bytes', () => {
    const bytes = '0x420'
    expect(parseAttestationBytes(bytes, 'bytes')).toBe(bytes)
  })

  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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

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

  it('works for numbers', () => {
    const num = 123
    const bytes = BigNumber.from(num).toHexString() as WagmiBytes
    expect(parseNumber(bytes)).toEqual(BigNumber.from(num))
  })

  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)
  })
})