Skip to content

Commit

Permalink
JS: unit test for setLogger extension point
Browse files Browse the repository at this point in the history
  • Loading branch information
exyi committed Mar 18, 2024
1 parent 4ec7b9d commit dcecf98
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
31 changes: 31 additions & 0 deletions src/Framework/Framework/Resources/Scripts/tests/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { globalValidationObject as validation, ValidationErrorDescriptor } from "../validation/validation"
import { createComplexObservableSubViewmodel, createComplexObservableViewmodel, ObservableHierarchy, ObservableSubHierarchy } from "./observableHierarchies"
import { getErrors } from "../validation/error"
import { setLogger } from "../utils/logging";


describe("DotVVM.Validation - public API", () => {
Expand Down Expand Up @@ -139,6 +140,36 @@ describe("DotVVM.Validation - public API", () => {
}
})

test("addErrors - second level nonexistent property (setLogger capture)", () => {
//Setup
const vm = createComplexObservableViewmodel();
const logMessages: any[][] = []
let captureLog = true
setLogger((next, level, area, ...args) => {
if (captureLog && level == "warn")
logMessages.push([ "area", ...args ])
else
next(level, area, ...args)
})

try {
//Act
validation.addErrors(
[
{ errorMessage: "Does not matter", propertyPath: "/Prop1/NonExistent" },
],
{ root: ko.observable(vm) }
);

//Check
expect(logMessages.length).toBe(1);
expect(logMessages[0][1]).toContain("Validation error could not been applied to property specified by propertyPath /Prop1/NonExistent. Property with name NonExistent does not exist on /Prop1.");
}
finally {
captureLog = false
}
})

test("addErrors - root level nonexistent property", () => {
//Setup
const vm = createComplexObservableViewmodel();
Expand Down
14 changes: 8 additions & 6 deletions src/Framework/Framework/Resources/Scripts/utils/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ type LogLevel = "normal" | "verbose";

export const level = getLogLevel();

let logger = function defaultLogger(level: "warn" | "log" | "error" | "trace", area: DotvvmLoggingArea, ...args: any) {
type ConsoleLogLevel = "warn" | "log" | "error" | "trace";

let logger = function defaultLogger(level: ConsoleLogLevel, area: DotvvmLoggingArea, ...args: any) {
console[level](area, ...args)
}

/**
* Instead of calling console.log, console.warn or console.error, DotVVM will call this function instead.
* Please keep in mind that the exact wording of error message is not DotVVM public API and may change without notice.
* Please keep in mind that the exact wording of log messages is not DotVVM public API and may change without notice.
* @example
* dotvvm.log.setLogger((previous, level, area, ...args) => {
* dotvvm.log.setLogger((next, level, area, ...args) => {
* if (area == "validation" && /^This message should be an error$/.test(args[0])) {
* level = "error"
* }
* previous(level, area, ...args) // call the default logger
* })
*/
export function setLogger(newLogger: (previous: typeof logger, level: "warn" | "log" | "error" | "trace", area: DotvvmLoggingArea, ...args: any) => void) {
const previousLogger = logger;
logger = (...args) => newLogger(previousLogger, ...args);
export function setLogger(newLogger: (next: typeof logger, level: ConsoleLogLevel, area: DotvvmLoggingArea, ...args: any) => void) {
const nextLogger = logger;
logger = (...args) => newLogger(nextLogger, ...args);
}

export type DotvvmLoggingArea = (
Expand Down

0 comments on commit dcecf98

Please sign in to comment.