Files

124 lines
3.0 KiB
JavaScript

/*
Unit tests for the REST API handler for the /supported endpoints.
*/
// Public npm libraries
import { assert } from 'chai'
import sinon from 'sinon'
// Local support libraries
import adapters from '../../../mocks/adapters/index.js'
import UseCasesMock from '../../../mocks/use-cases/index.js'
// const app = require('../../../mocks/app-mock')
import SupportedRESTController from '../../../../../src/controllers/rest-api/supported/controller.js'
import { context as mockContext } from '../../../mocks/ctx-mock.js'
let uut
let sandbox
let ctx
describe('#Supported-REST-Controller', () => {
// const testUser = {}
beforeEach(() => {
const useCases = new UseCasesMock()
uut = new SupportedRESTController({ adapters, useCases })
sandbox = sinon.createSandbox()
// Mock the context object.
ctx = mockContext()
})
afterEach(() => sandbox.restore())
describe('#constructor', () => {
it('should throw an error if adapters are not passed in', () => {
try {
uut = new SupportedRESTController()
assert.fail('Unexpected code path')
} catch (err) {
assert.include(
err.message,
'Instance of Adapters library required when instantiating /supported REST Controller.'
)
}
})
it('should throw an error if useCases are not passed in', () => {
try {
uut = new SupportedRESTController({ adapters })
assert.fail('Unexpected code path')
} catch (err) {
assert.include(
err.message,
'Instance of Use Cases library required when instantifying /supported REST Controller.'
)
}
})
})
describe('#supported', () => {
it('should return the supported payment kinds and extensions', async () => {
// Mock dependencies
ctx.body = {}
const result = {
paymentKinds: ['x402'],
extensions: ['x402']
}
sandbox.stub(uut.useCases.supported, 'supported').resolves(result)
await uut.supported(ctx)
assert.isObject(ctx.body)
assert.property(ctx.body, 'paymentKinds')
assert.property(ctx.body, 'extensions')
})
it('should catch and throw useCases error', async () => {
try {
// Force an error
sandbox.stub(uut.useCases.supported, 'supported').throws(new Error('test error'))
await uut.supported(ctx)
} catch (err) {
// console.log('err: ', err)
assert.include(err.message, 'test error')
}
})
})
describe('#handleError', () => {
it('should still throw error if there is no message', () => {
try {
const err = {
status: 404
}
uut.handleError(ctx, err)
} catch (err) {
assert.include(err.message, 'Not Found')
}
})
it('should throw error with message', () => {
try {
const err = {
status: 422,
message: 'test error'
}
uut.handleError(ctx, err)
} catch (err) {
assert.include(err.message, 'test error')
}
})
})
})