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

Commit b412899

Browse files
achingbrainvmx
authored andcommitted
fix: use correct property when checking if resolver exists
The right property is `.codec` and not `.format`. Adds a test for the same and also runs the custom format tests again.
1 parent 1a115e7 commit b412899

2 files changed

Lines changed: 62 additions & 53 deletions

File tree

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class IPLDResolver {
4545
*/
4646
addFormat (format) {
4747
const codec = format.codec
48-
if (this.resolvers[format.format]) {
48+
49+
if (this.resolvers[format.codec]) {
4950
const codecName = multicodec.print[codec]
5051
throw new Error(`Resolver already exists for codec "${codecName}"`)
5152
}

test/format-support.spec.js

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,80 @@ const inMemory = require('ipld-in-memory')
88

99
const IPLDResolver = require('../src')
1010

11-
module.exports = (repo) => {
12-
describe('IPLD format support', () => {
13-
let data, cid
11+
describe('IPLD format support', () => {
12+
let data, cid
1413

15-
before(async () => {
16-
const resolver = await inMemory(IPLDResolver)
14+
before(async () => {
15+
const resolver = await inMemory(IPLDResolver)
1716

18-
data = { now: Date.now() }
17+
data = { now: Date.now() }
1918

20-
cid = await resolver.put(data, multicodec.DAG_CBOR)
21-
})
22-
23-
describe('Dynamic format loading', () => {
24-
it('should fail to dynamically load format', async () => {
25-
const resolver = await inMemory(IPLDResolver, {
26-
formats: []
27-
})
19+
cid = await resolver.put(data, multicodec.DAG_CBOR)
20+
})
2821

29-
const result = resolver.resolve(cid, '')
30-
await expect(result.next()).to.be.rejectedWith(
31-
'No resolver found for codec "dag-cbor"')
22+
describe('Dynamic format loading', () => {
23+
it('should fail to dynamically load format', async () => {
24+
const resolver = await inMemory(IPLDResolver, {
25+
formats: []
3226
})
3327

34-
it('should fail to dynamically load format via loadFormat option', async () => {
35-
const errMsg = 'BOOM' + Date.now()
36-
const resolver = await inMemory(IPLDResolver, {
37-
formats: [],
38-
loadFormat (codec) {
39-
if (codec !== multicodec.DAG_CBOR) {
40-
throw new Error('unexpected codec')
41-
}
42-
throw new Error(errMsg)
43-
}
44-
})
28+
const result = resolver.resolve(cid, '')
29+
await expect(result.next()).to.be.rejectedWith(
30+
'No resolver found for codec "dag-cbor"')
31+
})
4532

46-
const result = resolver.resolve(cid, '')
47-
await expect(result.next()).to.be.rejectedWith(errMsg)
33+
it('should fail to dynamically load format via loadFormat option', async () => {
34+
const errMsg = 'BOOM' + Date.now()
35+
const resolver = await inMemory(IPLDResolver, {
36+
formats: [],
37+
loadFormat (codec) {
38+
if (codec !== multicodec.DAG_CBOR) {
39+
throw new Error('unexpected codec')
40+
}
41+
throw new Error(errMsg)
42+
}
4843
})
4944

50-
it('should dynamically load missing format', async () => {
51-
const resolver = await inMemory(IPLDResolver, {
52-
formats: [],
53-
loadFormat (codec) {
54-
if (codec !== multicodec.DAG_CBOR) {
55-
throw new Error('unexpected codec')
56-
}
57-
return dagCBOR
45+
const result = resolver.resolve(cid, '')
46+
await expect(result.next()).to.be.rejectedWith(errMsg)
47+
})
48+
49+
it('should dynamically load missing format', async () => {
50+
const resolver = await inMemory(IPLDResolver, {
51+
formats: [],
52+
loadFormat (codec) {
53+
if (codec !== multicodec.DAG_CBOR) {
54+
throw new Error('unexpected codec')
5855
}
59-
})
56+
return dagCBOR
57+
}
58+
})
6059

61-
const result = resolver.resolve(cid, '')
62-
const node = await result.first()
63-
expect(node.value).to.eql(data)
60+
cid = await resolver.put(data, multicodec.DAG_CBOR)
61+
62+
await expect(resolver.get(cid)).to.eventually.eql(data)
63+
})
64+
65+
it('should not dynamically load format added statically', async () => {
66+
const resolver = await inMemory(IPLDResolver, {
67+
formats: [dagCBOR],
68+
loadFormat (codec) {
69+
throw new Error(`unexpected load format ${codec}`)
70+
}
6471
})
6572

66-
it('should not dynamically load format added statically', async () => {
67-
const resolver = await inMemory(IPLDResolver, {
68-
formats: [dagCBOR],
69-
loadFormat (codec) {
70-
throw new Error(`unexpected load format ${codec}`)
71-
}
72-
})
73+
cid = await resolver.put(data, multicodec.DAG_CBOR)
74+
75+
await expect(resolver.get(cid)).to.eventually.eql(data)
76+
})
7377

74-
const result = resolver.resolve(cid, '')
75-
await result.next()
78+
it('should throw if adding format twice', async () => {
79+
const resolver = await inMemory(IPLDResolver, {
80+
formats: []
7681
})
82+
resolver.addFormat(dagCBOR)
83+
84+
expect(() => resolver.addFormat(dagCBOR)).to.throw(/Resolver already exists/)
7785
})
7886
})
79-
}
87+
})

0 commit comments

Comments
 (0)