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

Commit e6b6bb2

Browse files
committed
chore: test with both Buffer and Uint8Array
1 parent 2b8127b commit e6b6bb2

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

test/index.spec.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ const encodeHex = string => {
3434

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

37+
const they = (description, test) => {
38+
it('description (Buffer)', () => test(Buffer.from))
39+
it('description (Uint8Array)', () => test(encodeText))
40+
}
41+
3742
describe('multihash', () => {
3843
describe('toHexString', () => {
3944
it('valid', () => {
@@ -94,7 +99,7 @@ describe('multihash', () => {
9499
})
95100

96101
describe('fromB58String', () => {
97-
it('valid', () => {
102+
they('valid', (encode) => {
98103
const src = 'QmPfjpVaf593UQJ9a5ECvdh2x17XuJYG5Yanv5UFnH3jPE'
99104
const expected = encodeHex('122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461')
100105

@@ -105,7 +110,7 @@ describe('multihash', () => {
105110
)
106111

107112
expect(
108-
mh.fromB58String(encodeText(src))
113+
mh.fromB58String(encode(src))
109114
).to.be.eql(
110115
expected
111116
)
@@ -161,7 +166,7 @@ describe('multihash', () => {
161166
})
162167
})
163168

164-
it('invalid', () => {
169+
they('invalid', (encode) => {
165170
expect(
166171
() => mh.encode()
167172
).to.throw(
@@ -175,7 +180,7 @@ describe('multihash', () => {
175180
)
176181

177182
expect(
178-
() => mh.encode(encodeText('hello'), 0x11, 2)
183+
() => mh.encode(encode('hello'), 0x11, 2)
179184
).to.throw(
180185
/length should be equal/
181186
)
@@ -287,7 +292,7 @@ describe('multihash', () => {
287292
})
288293
})
289294

290-
it('invalid', () => {
295+
they('invalid', (encode) => {
291296
const invalidNames = [
292297
'sha256',
293298
'sha9',
@@ -303,7 +308,7 @@ describe('multihash', () => {
303308
})
304309

305310
expect(
306-
() => mh.coerceCode(encodeText('hello'))
311+
() => mh.coerceCode(encode('hello'))
307312
).to.throw(
308313
/should be a number/
309314
)
@@ -316,14 +321,14 @@ describe('multihash', () => {
316321
})
317322
})
318323

319-
it('prefix', () => {
320-
const multihash = mh.encode(encodeText('hey'), 0x11, 3)
324+
they('prefix', (encode) => {
325+
const multihash = mh.encode(encode('hey'), 0x11, 3)
321326
const prefix = mh.prefix(multihash)
322327
expect(prefix.toString('hex')).to.eql('1103')
323328
})
324329

325-
it('prefix throws on invalid multihash', () => {
326-
const multihash = encodeText('definitely not valid')
330+
they('prefix throws on invalid multihash', (encode) => {
331+
const multihash = encode('definitely not valid')
327332

328333
expect(() => mh.prefix(multihash)).to.throw()
329334
})

0 commit comments

Comments
 (0)