Skip to content

Instantly share code, notes, and snippets.

class AcmeCompanyApi implements CompanyApi {
private readonly acmeCausesEndpoint: AcmeCausesEndpoint;
private readonly claimOpeningEndpoint: AcmeClaimOpeningEndpoint;
private readonly authTokenRetriever: AcmeAuthTokenRetriever;
constructor(config: AcmeApiConfig = new AcmeApiConfig()) {
this.acmeCausesEndpoint = new AcmeCausesEndpoint(config);
this.claimOpeningEndpoint = new AcmeClaimOpeningEndpoint(config);
this.authTokenRetriever = new AcmeAuthTokenRetriever(config);
}
class AcmeCompanyApi implements CompanyApi {
private readonly forGettingCauses: ForGettingCauses;
private readonly forOpeningClaim: ForOpeningClaim;
private readonly authTokenRetriever: AuthTokenRetriever;
constructor(config: AcmeApiConfig = new AcmeApiConfig()) {
this.forGettingCauses = new AcmeCausesEndpoint(config);
this.forOpeningClaim = new AcmeClaimOpeningEndpoint(config);
this.authTokenRetriever = new AcmeAuthTokenRetriever(config);
}
describe('AcmeCompanyApi', () => {
// some declarations
beforeEach(() => {
decoratedApi = {
open: jest.fn<Promise<OpeningResult>, [Claim]>()
};
api = new WithErrorHandlingCompanyApi(decoratedApi);
claim = aClaim(causeCodeInClaim);
class WithErrorHandlingCompanyApi implements CompanyApi {
constructor(private readonly decoratedApi: CompanyApi) {
}
async open(claim: Claim): Promise<OpeningResult> {
try {
return await this.decoratedApi.open(claim);
} catch (e) {
if (e instanceof CannotRetrieveTokenError) {
return OpeningResult.failed(claim, 'Acme API: failure retrieving token');
class AcmeCompanyApi implements CompanyApi {
constructor(
private readonly forGettingCauses: ForGettingCauses,
private readonly forOpeningClaim: ForOpeningClaim,
private readonly authTokenRetriever: AuthTokenRetriever) {
}
async open(claim: Claim): Promise<OpeningResult> {
const token = await this.authTokenRetriever.retrieveToken();
const causes = await this.forGettingCauses.getAllFor(claim, token);
class AcmeCompanyApi implements CompanyApi {
constructor(
private readonly forGettingCauses: ForGettingCauses,
private readonly forOpeningClaim: ForOpeningClaim,
private readonly authTokenRetriever: AuthTokenRetriever) {
}
async open(claim: Claim): Promise<OpeningResult> {
try {
// some imports
describe('AcmeCompanyApi', () => {
// some declarations
beforeEach(() => {
forGettingCauses = {
getAllFor: jest.fn<Promise<Cause[]>, [Claim, AuthToken]>()
};
forOpeningClaim = {
public abstract class OpeningResult
{
public static OpeningResult Success(ClaimId claimId, ReferenceInCompany referenceInCompany)
{
return new SuccessfulOpeningResult(claimId, referenceInCompany);
}
public static OpeningResult Failure(ClaimId claimId, string description)
{
return new FailingOpeningResult(claimId, description);
public class OpeningResult
{
private readonly string? _failureDescription;
private readonly ReferenceInCompany? _referenceInCompany;
private readonly ClaimId _claimId;
private OpeningResult(ReferenceInCompany? referenceInCompany, ClaimId claimId, string? failureDescription)
{
_failureDescription = failureDescription;
_claimId = claimId;
// some imports
public class NotifyOpenedClaimCommandBuilder {
// some fields
public static NotifyOpenedClaimCommandBuilder aNotifyOpenedCommand() {
return new NotifyOpenedClaimCommandBuilder();
}
public NotifyOpenedClaimCommandBuilder of(ClaimDataBuilder claimBuilder) {