increase coverage, replace null with undefined, try and catch on iota connection

This commit is contained in:
einhornimmond 2023-06-18 17:48:25 +02:00
parent 9d773fcd5a
commit 778664b7de
3 changed files with 35 additions and 11 deletions

View File

@ -13,9 +13,8 @@ describe('apis/IotaConnector/enabled', () => {
const messageString = 'Hello World - ' + now.toString()
const messageHexString = Buffer.from(messageString, 'utf8').toString('hex')
const indexHexString = Buffer.from(CONFIG.IOTA_COMMUNITY_ALIAS, 'utf8').toString('hex')
const iota = IotaClientSingleton.getInstance()
it('sends hello world message to iota tangle', async () => {
const iotaMessage = await iota?.sendDataMessage(messageString)
const iotaMessage = await IotaClientSingleton.getInstance()?.sendDataMessage(messageString)
expect(iotaMessage).toMatchObject({
message: {
payload: {
@ -29,7 +28,7 @@ describe('apis/IotaConnector/enabled', () => {
iotaMessage?.messageId ?? '5498130bc3918e1a7143969ce05805502417e3e1bd596d3c44d6a0adeea22710'
})
it('receives hello world message from iota tangle by message id', async () => {
const iotaMessage = await iota?.getMessage(messageId)
const iotaMessage = await IotaClientSingleton.getInstance()?.getMessage(messageId)
expect(iotaMessage).toMatchObject({
message: {
payload: {

View File

@ -28,14 +28,19 @@ class IotaClientSingleton {
* This implementation let you subclass the Singleton class while keeping
* just one instance of each subclass around.
*/
public static getInstance(): IotaClientSingleton | null {
public static getInstance(): IotaClientSingleton | undefined {
if (!CONFIG.IOTA || !CONFIG.IOTA_API_URL) {
logger.info(`Iota are disabled via config...`)
return null
logger.info(`Iota are disabled via config...`)
return
}
if (!IotaClientSingleton.instance) {
IotaClientSingleton.instance = new IotaClientSingleton()
IotaClientSingleton.instance.client = new ClientBuilder().node(CONFIG.IOTA_API_URL).build()
try {
IotaClientSingleton.instance.client = new ClientBuilder().node(CONFIG.IOTA_API_URL).build()
} catch(e){
logger.error('couldn\'t connect to iota')
return
}
}
return IotaClientSingleton.instance

View File

@ -1,11 +1,31 @@
import { IotaClientSingleton } from '@/apis/IotaConnector'
import { CONFIG } from '@/config'
import { backendLogger as logger } from '@/server/logger'
/*
jest.mock('@/config', () => ({
CONFIG: { IOTA: false },
CONFIG: { IOTA: true, IOTA_API_URL:'invalidUrl' },
}))
*/
describe('apis/IotaConnector/disabled', () => {
it('getInstance return null if iota is disabled', () => {
expect(IotaClientSingleton.getInstance()).toBeNull()
beforeEach(() => {
CONFIG.IOTA = false
})
it('getInstance return undefined if iota is disabled', () => {
const spyLog = jest.spyOn(logger, 'info')
expect(IotaClientSingleton.getInstance()).toBeUndefined()
expect(spyLog).toHaveBeenCalledWith('Iota are disabled via config...');
})
})
describe('apis/IotaConnector/invalidIotaUrl', () => {
beforeEach(() => {
CONFIG.IOTA = true
CONFIG.IOTA_API_URL = 'invalidUrl'
})
it('log "couldn\'t connect to iota"', () => {
const spyLog = jest.spyOn(logger, 'error')
expect(IotaClientSingleton.getInstance()).toBeUndefined()
expect(spyLog).toHaveBeenCalledWith('couldn\'t connect to iota')
})
})