Last active
January 22, 2025 23:45
-
-
Save flpms/820ee9cf66776b2093dbf3cce7b97cbb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const UseCase = (dependencies) => { | |
const { | |
logger, | |
repositories: { | |
repository, | |
}, | |
services: { | |
someExternalCall, | |
}, | |
} | |
const specificCase = ({ data, onFinish }) => { | |
try { | |
// os dados já devem chegar preparados para manipulação, uma camada de controller e validation fazem os parses necessários | |
const [serviceResult, dbCallResult] = await Promise.all([ | |
someExternalCall(data), | |
repository.find(data), | |
]); | |
return onFinish.OK(); | |
} catch (err) { | |
// excessões também são só tratadas aqui | |
logger.warn(err); | |
onFinish.SERVICE_UNAVAILABLE() | |
} | |
} | |
return { | |
specificCase, | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect } from 'chai'; | |
import sinon from 'sinon'; | |
import { UseCase } from './use-case-example.js'; | |
describe('[APP - V1 - USE CASES] - UseCase', () => { | |
let useCases; | |
const logger = { | |
warn: sbox.stub() | |
}; | |
const repository = { | |
find: sbox.stub(), | |
}; | |
const someExternalCall = sbox.stub(); | |
const dependencies = { | |
logger, | |
repositories: { | |
repository, | |
}, | |
services: { | |
someExternalCall, | |
}, | |
}; | |
before(() => { | |
useCases = UseCases(dependencies); | |
}); | |
context('instance', () => { | |
it('should have a specificCase as a function', () => { | |
expect(useCases.specificCase).to.be.a('function'); | |
}); | |
}); | |
context('specificCase', () => { | |
const onFinish = { | |
OK: sbox.stub(), | |
SERVICE_UNAVAILABLE: sbox.stub(), | |
}; | |
context('OK', () => { | |
before(async() => { | |
sbox.reset(); | |
repository.find.resolves(mockedData); | |
someExternalCall.resolves({}); | |
await useCases.specificCase({ data, onFinish }); | |
}); | |
it('should call someExternalCall', () => { | |
expect(someExternalCall.calledOnce).to.be.true; | |
}); | |
it('should call repository find', () => { | |
expect(repository.find.calledOnce).to.be.true; | |
}); | |
it('expect onFinish.OK to be called', () => { | |
expect(onFinish.OK.calledOnce).to.be.true; | |
}); | |
// Se quiser pode ser mais específico sobre o retorno e é até aconselhável | |
}); | |
context('SERVICE_UNAVAILABLE', () => { | |
before(async() => { | |
sbox.reset(); | |
repository.find.rejects(); | |
await useCases.specificCase({ data, onFinish }); | |
}); | |
it('should call repository find', () => { | |
expect(logger.warn.calledOnce).to.be.true; | |
}); | |
it('expect onFinish.SERVICE_UNAVAILABLE to be called', () => { | |
expect(onFinish.SERVICE_UNAVAILABLE.calledOnce).to.be.true; | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment