@@ -0,0 +1,66 @@
|
||||
import BaseService from "../base-service"
|
||||
|
||||
describe("BaseService", () => {
|
||||
describe("addDecorator", () => {
|
||||
const baseService = new BaseService()
|
||||
|
||||
it("successfully adds decorator", () => {
|
||||
baseService.addDecorator(obj => {
|
||||
return (obj.decorator1 = true)
|
||||
})
|
||||
|
||||
expect(baseService.decorators_.length).toEqual(1)
|
||||
})
|
||||
|
||||
it("throws if decorator is not a function", () => {
|
||||
expect(() => baseService.addDecorator("not a function")).toThrow(
|
||||
"Decorators must be of type function"
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("runDecorators_", () => {
|
||||
it("returns success when passwords match", async () => {
|
||||
const baseService = new BaseService()
|
||||
|
||||
baseService.addDecorator(obj => {
|
||||
obj.decorator1 = true
|
||||
return obj
|
||||
})
|
||||
baseService.addDecorator(obj => {
|
||||
obj.decorator2 = true
|
||||
return obj
|
||||
})
|
||||
|
||||
const result = await baseService.runDecorators_({ data: "initial" })
|
||||
expect(result).toEqual({
|
||||
data: "initial",
|
||||
decorator1: true,
|
||||
decorator2: true,
|
||||
})
|
||||
})
|
||||
|
||||
it("skips failing decorator", async () => {
|
||||
const baseService = new BaseService()
|
||||
|
||||
baseService.addDecorator(obj => {
|
||||
obj.decorator1 = true
|
||||
return obj
|
||||
})
|
||||
baseService.addDecorator(obj => {
|
||||
return Promise.reject("fail")
|
||||
})
|
||||
baseService.addDecorator(obj => {
|
||||
obj.decorator3 = true
|
||||
return Promise.resolve(obj)
|
||||
})
|
||||
|
||||
const result = await baseService.runDecorators_({ data: "initial" })
|
||||
expect(result).toEqual({
|
||||
data: "initial",
|
||||
decorator1: true,
|
||||
decorator3: true,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -2,5 +2,35 @@
|
||||
* Common functionality for Services
|
||||
* @interface
|
||||
*/
|
||||
class BaseService {}
|
||||
class BaseService {
|
||||
constructor() {
|
||||
this.decorators_ = []
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a decorator to a service. The decorator must be a function and should
|
||||
* return a decorated object.
|
||||
* @param {function} fn - the decorator to add to the service
|
||||
*/
|
||||
addDecorator(fn) {
|
||||
if (typeof fn !== "function") {
|
||||
throw Error("Decorators must be of type function")
|
||||
}
|
||||
|
||||
this.decorators_.push(fn)
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the decorators registered on the service. The decorators are run in
|
||||
* the order they have been registered in. Failing decorators will be skipped
|
||||
* in order to ensure deliverability in spite of breaking code.
|
||||
* @param {object} obj - the object to decorate.
|
||||
* @return {object} the decorated object.
|
||||
*/
|
||||
runDecorators_(obj) {
|
||||
return this.decorators_.reduce(async (acc, next) => {
|
||||
return acc.then(res => next(res)).catch(() => acc)
|
||||
}, Promise.resolve(obj))
|
||||
}
|
||||
}
|
||||
export default BaseService
|
||||
|
||||
Reference in New Issue
Block a user