File size: 3,872 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
import sinon from 'sinon'
import { expect } from 'chai'
import { ActionContext } from '../../actions/action.interface.js'
import BaseResource from '../../adapters/resource/base-resource.js'
import { CurrentAdmin } from '../../../current-admin.interface.js'
import BaseRecord from '../../adapters/record/base-record.js'
import ValidationError from '../../utils/errors/validation-error.js'
import ActionErrorHandler from './action-error-handler.js'
import ForbiddenError from '../../utils/errors/forbidden-error.js'
import { ActionDecorator } from '../../decorators/index.js'
describe('ActionErrorHandler', function () {
let resource: BaseResource
let record: BaseRecord
let context: ActionContext
let action: ActionDecorator
const notice = {
message: 'thereWereValidationErrors',
type: 'error',
}
const currentAdmin = {} as CurrentAdmin
beforeEach(function () {
resource = sinon.createStubInstance(BaseResource)
record = sinon.createStubInstance(BaseRecord) as unknown as BaseRecord
// translateMessage = sinon.stub().returns(notice.message)
action = { name: 'myAction' } as ActionDecorator
context = { resource, record, currentAdmin, action } as ActionContext
})
afterEach(function () {
sinon.restore()
})
it('returns record with validation error when ValidationError is thrown', function () {
const errors = {
fieldWithError: {
type: 'required', message: 'Field is required',
},
}
const error = new ValidationError(errors)
expect(ActionErrorHandler(error, context)).to.deep.equal({
record: {
baseError: null,
errors,
params: {},
populated: {},
},
notice,
records: [],
meta: undefined,
})
})
it('returns meta when ValidationError is thrown for the list action', function () {
const errors = {
fieldWithError: {
type: 'required', message: 'Field is required',
},
}
const error = new ValidationError(errors)
action.name = 'list'
expect(ActionErrorHandler(error, context)).to.deep.equal({
record: {
baseError: null,
errors,
params: {},
populated: {},
},
notice,
records: [],
meta: {
total: 0,
perPage: 0,
page: 0,
direction: null,
sortBy: null,
},
})
})
it('throws any undefined error back to the app', function () {
const unknownError = new Error()
expect(() => {
ActionErrorHandler(unknownError, context)
}).to.throw(unknownError)
})
it('returns record with forbidden error when ForbiddenError is thrown', function () {
const errorMessage = 'You cannot perform this action'
const error = new ForbiddenError(errorMessage)
expect(ActionErrorHandler(error, context)).to.deep.equal({
record: {
baseError: {
message: errorMessage,
type: 'ForbiddenError',
},
errors: {},
params: {},
populated: {},
},
records: [],
notice: {
message: errorMessage,
type: 'error',
},
meta: undefined,
})
})
it('returns meta when ForbiddenError is thrown for the list action', function () {
const errorMessage = 'You cannot perform this action'
const error = new ForbiddenError(errorMessage)
action.name = 'list'
expect(ActionErrorHandler(error, context)).to.deep.equal({
record: {
baseError: {
message: errorMessage,
type: 'ForbiddenError',
},
errors: {},
params: {},
populated: {},
},
notice: {
message: errorMessage,
type: 'error',
},
records: [],
meta: {
total: 0,
perPage: 0,
page: 0,
direction: null,
sortBy: null,
},
})
})
})
|