Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
chore: test with both Buffer and Uint8Array
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Jul 22, 2020
1 parent 2b8127b commit e6b6bb2
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const encodeHex = string => {

const encodeText = string => textEncoder.encode(string)

const they = (description, test) => {
it('description (Buffer)', () => test(Buffer.from))
it('description (Uint8Array)', () => test(encodeText))
}

describe('multihash', () => {
describe('toHexString', () => {
it('valid', () => {
Expand Down Expand Up @@ -94,7 +99,7 @@ describe('multihash', () => {
})

describe('fromB58String', () => {
it('valid', () => {
they('valid', (encode) => {
const src = 'QmPfjpVaf593UQJ9a5ECvdh2x17XuJYG5Yanv5UFnH3jPE'
const expected = encodeHex('122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461')

Expand All @@ -105,7 +110,7 @@ describe('multihash', () => {
)

expect(
mh.fromB58String(encodeText(src))
mh.fromB58String(encode(src))
).to.be.eql(
expected
)
Expand Down Expand Up @@ -161,7 +166,7 @@ describe('multihash', () => {
})
})

it('invalid', () => {
they('invalid', (encode) => {
expect(
() => mh.encode()
).to.throw(
Expand All @@ -175,7 +180,7 @@ describe('multihash', () => {
)

expect(
() => mh.encode(encodeText('hello'), 0x11, 2)
() => mh.encode(encode('hello'), 0x11, 2)
).to.throw(
/length should be equal/
)
Expand Down Expand Up @@ -287,7 +292,7 @@ describe('multihash', () => {
})
})

it('invalid', () => {
they('invalid', (encode) => {
const invalidNames = [
'sha256',
'sha9',
Expand All @@ -303,7 +308,7 @@ describe('multihash', () => {
})

expect(
() => mh.coerceCode(encodeText('hello'))
() => mh.coerceCode(encode('hello'))
).to.throw(
/should be a number/
)
Expand All @@ -316,14 +321,14 @@ describe('multihash', () => {
})
})

it('prefix', () => {
const multihash = mh.encode(encodeText('hey'), 0x11, 3)
they('prefix', (encode) => {
const multihash = mh.encode(encode('hey'), 0x11, 3)
const prefix = mh.prefix(multihash)
expect(prefix.toString('hex')).to.eql('1103')
})

it('prefix throws on invalid multihash', () => {
const multihash = encodeText('definitely not valid')
they('prefix throws on invalid multihash', (encode) => {
const multihash = encode('definitely not valid')

expect(() => mh.prefix(multihash)).to.throw()
})
Expand Down

0 comments on commit e6b6bb2

Please sign in to comment.