-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { getErrorCodeByMessage, ValidationErrors } from "swissqrbill:errors"; | ||
import { } from "swissqrbill:shared:cleaner.js"; | ||
|
||
|
||
describe("getErrorCodeByMessage", () => { | ||
|
||
it("should return the correct error code", () => { | ||
const key = "ACCOUNT_IS_QR_IBAN_BUT_REFERENCE_IS_MISSING"; | ||
expect(getErrorCodeByMessage(ValidationErrors[key])).toBe(key); | ||
}); | ||
|
||
|
||
}); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { ValidationError, ValidationErrors } from "swissqrbill:errors"; | ||
import { cleanData } from "swissqrbill:shared:cleaner"; | ||
import { validateData } from "swissqrbill:shared:validator"; | ||
import { missingCreditor } from "swissqrbill:tests:data/invalid-data"; | ||
import { getValidationError } from "swissqrbill:tests:utils/errors"; | ||
|
||
|
||
describe("errors", () => { | ||
|
||
it("should be of instance 'Error' and 'ValidationError'", async () => { | ||
expect(getValidationError(() => validateData(cleanData(missingCreditor)))).toBeInstanceOf(Error); | ||
expect(getValidationError(() => validateData(cleanData(missingCreditor)))).toBeInstanceOf(ValidationError); | ||
}); | ||
|
||
it("should have an error code", async () => { | ||
const code = "CREDITOR_IS_UNDEFINED"; | ||
expect(getValidationError(() => validateData(cleanData(missingCreditor)))?.code).toBeDefined(); | ||
expect(getValidationError(() => validateData(cleanData(missingCreditor)))?.code).toBe(code); | ||
}); | ||
|
||
it("should have an error message", async () => { | ||
const code = "CREDITOR_IS_UNDEFINED"; | ||
expect(getValidationError(() => validateData(cleanData(missingCreditor)))?.message).toBeDefined(); | ||
expect(getValidationError(() => validateData(cleanData(missingCreditor)))?.message).toBe(ValidationErrors[code]); | ||
}); | ||
|
||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { ValidationError, ValidationErrors } from "swissqrbill:errors"; | ||
|
||
import { getValidationError } from "./errors"; | ||
|
||
|
||
describe("error", () => { | ||
|
||
it("should return the caught error", () => { | ||
expect(getValidationError(() => { throw new ValidationError(ValidationErrors.ACCOUNT_LENGTH_IS_INVALID); })).toBeInstanceOf(Error); | ||
expect(getValidationError(() => { throw new ValidationError(ValidationErrors.ACCOUNT_LENGTH_IS_INVALID); })?.message).toBe(ValidationErrors.ACCOUNT_LENGTH_IS_INVALID); | ||
}); | ||
|
||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ValidationError } from "swissqrbill:errors"; | ||
|
||
|
||
export function getValidationError(fn: (...params: unknown[]) => unknown): ValidationError | undefined { | ||
try { | ||
fn(); | ||
} catch (error){ | ||
if(error instanceof ValidationError){ | ||
return error; | ||
} | ||
} | ||
} |