Increased unit test code coverage for adapters

This commit is contained in:
Chris Troutner
2021-07-11 19:33:26 -07:00
parent 0da7b705c9
commit 4cdb389706
7 changed files with 230 additions and 2 deletions
+5 -2
View File
@@ -6,9 +6,10 @@ const IpfsAdapter = require('./ipfs')
const IpfsCoordAdapter = require('./ipfs-coord')
class IPFS {
constructor (localConfig) {
constructor (localConfig = {}) {
// Encapsulate dependencies
this.ipfsAdapter = new IpfsAdapter()
this.IpfsCoordAdapter = IpfsCoordAdapter
this.ipfsCoordAdapter = {} // placeholder
@@ -28,11 +29,13 @@ class IPFS {
this.ipfs = this.ipfsAdapter.ipfs
// Start ipfs-coord
this.ipfsCoordAdapter = new IpfsCoordAdapter({
this.ipfsCoordAdapter = new this.IpfsCoordAdapter({
ipfs: this.ipfs
})
await this.ipfsCoordAdapter.start()
console.log('ipfs-coord is ready.')
return true
} catch (err) {
console.error('Error in adapters/ipfs/index.js/start()')
throw err
+1
View File
@@ -26,6 +26,7 @@ class IpfsCoordAdapter {
// Encapsulate dependencies
this.IpfsCoord = IpfsCoord
this.ipfsCoord = {}
this.bchjs = new BCHJS()
// this.rpc = new JSONRPC()
this.config = config
@@ -0,0 +1,81 @@
/*
Unit tests for the IPFS Adapter.
*/
const assert = require('chai').assert
const sinon = require('sinon')
const IPFSCoordAdapter = require('../../../src/adapters/ipfs/ipfs-coord')
const IPFSMock = require('../mocks/ipfs-mock')
const IPFSCoordMock = require('../mocks/ipfs-coord-mock')
describe('#IPFS', () => {
let uut
let sandbox
beforeEach(() => {
const ipfs = IPFSMock.create()
uut = new IPFSCoordAdapter({ ipfs })
sandbox = sinon.createSandbox()
})
afterEach(() => sandbox.restore())
describe('#constructor', () => {
it('should throw an error if ipfs instance is not included', () => {
try {
uut = new IPFSCoordAdapter()
assert.fail('Unexpected code path')
} catch (err) {
assert.include(
err.message,
'Instance of IPFS must be passed when instantiating ipfs-coord.'
)
}
})
})
describe('#start', () => {
it('should return a promise that resolves into an instance of IPFS.', async () => {
// Mock dependencies.
uut.IpfsCoord = IPFSCoordMock
const result = await uut.start()
// console.log('result: ', result)
assert.equal(result, true)
})
})
describe('#attachRPCRouter', () => {
it('should attached a router output', async () => {
// Mock dependencies
uut.ipfsCoord = {
privateLog: {},
ipfs: {
orbitdb: {
privateLog: {}
}
}
}
const router = console.log
uut.attachRPCRouter(router)
})
it('should throw an error if ipfs-coord has not been instantiated', () => {
try {
const router = console.log
uut.attachRPCRouter(router)
assert.fail('Unexpected code path')
} catch (err) {
assert.include(err.message, 'Cannot read property')
}
})
})
})
@@ -0,0 +1,49 @@
/*
Unit tests for the index.js file for the IPFS and ipfs-coord libraries.
*/
const assert = require('chai').assert
const sinon = require('sinon')
const IPFSLib = require('../../../src/adapters/ipfs')
const IPFSMock = require('../mocks/ipfs-mock')
const IPFSCoordMock = require('../mocks/ipfs-coord-mock')
describe('#IPFS', () => {
let uut
let sandbox
beforeEach(() => {
uut = new IPFSLib()
sandbox = sinon.createSandbox()
})
afterEach(() => sandbox.restore())
describe('#start', () => {
it('should return a promise that resolves into an instance of IPFS.', async () => {
// Mock dependencies.
uut.ipfsAdapter = new IPFSMock()
uut.IpfsCoordAdapter = IPFSCoordMock
const result = await uut.start()
assert.equal(result, true)
})
it('should catch and throw an error', async () => {
try {
// Force an error
sandbox.stub(uut.ipfsAdapter, 'start').rejects(new Error('test error'))
await uut.start()
assert.fail('Unexpected code path.')
} catch (err) {
console.log(err)
assert.include(err.message, 'test error')
}
})
})
})
+50
View File
@@ -0,0 +1,50 @@
/*
Unit tests for the IPFS Adapter.
*/
const assert = require('chai').assert
const sinon = require('sinon')
const IPFSLib = require('../../../src/adapters/ipfs/ipfs')
const IPFSMock = require('../mocks/ipfs-mock')
describe('#IPFS', () => {
let uut
let sandbox
beforeEach(() => {
uut = new IPFSLib()
sandbox = sinon.createSandbox()
})
afterEach(() => sandbox.restore())
describe('#start', () => {
it('should return a promise that resolves into an instance of IPFS.', async () => {
// Mock dependencies.
uut.IPFS = IPFSMock
const result = await uut.start()
// console.log('result: ', result)
assert.equal(uut.isReady, true)
assert.property(result, 'config')
})
it('should catch and throw an error', async () => {
try {
// Force an error
sandbox.stub(uut.IPFS, 'create').rejects(new Error('test error'))
await uut.start()
assert.fail('Unexpected code path.')
} catch (err) {
console.log(err)
assert.include(err.message, 'test error')
}
})
})
})
+13
View File
@@ -0,0 +1,13 @@
/*
Mocks for the ipfs-coord library
*/
class IPFSCoord {
async isReady () {
return true
}
async start () {}
}
module.exports = IPFSCoord
+31
View File
@@ -0,0 +1,31 @@
/*
Mocks for the js-ipfs
*/
class IPFS {
constructor () {
this.ipfs = {}
}
static create () {
const mockIpfs = new MockIpfsInstance()
return mockIpfs
}
async start () {}
}
class MockIpfsInstance {
constructor () {
this.config = {
profiles: {
apply: () => {}
}
}
}
stop () {}
}
module.exports = IPFS