Skip to content

Commit

Permalink
feat: refactor tests and method code.
Browse files Browse the repository at this point in the history
  • Loading branch information
imabp committed Oct 5, 2022
1 parent 8182852 commit fbf15b4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
4 changes: 2 additions & 2 deletions __tests__/_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export default class ProblemContextHelper {
}


constructor(customKeys?:string[]) {
this._problemInstance = new Problem(this._instanceOptions, customKeys);
constructor() {
this._problemInstance = new Problem(this._instanceOptions);
}

}
5 changes: 2 additions & 3 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { COPY_MODE, Problem } from "../src";
import { Problem } from "../src";
import { COPY_MODE } from "../src/constants";
import ProblemContextHelper from "./_helper";

const _testContext = new ProblemContextHelper();
Expand All @@ -13,7 +14,6 @@ describe("Class Methods Test Suite", () => {

test("Method: Copy, mode: LEAVE_PROPS", () => {
const _problemCopy = _testContext._problemInstance.copy(
_testContext._problemInstance,
COPY_MODE.LEAVE_PROPS,
[]
);
Expand All @@ -24,7 +24,6 @@ describe("Class Methods Test Suite", () => {

test("Method: Copy, mode: SKIP_PROPS", () => {
const _copiedProblem = _testContext._problemInstance.copy(
_testContext._problemInstance,
COPY_MODE.SKIP_PROPS,
["details"]
);
Expand Down
3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const config: Config.InitialOptions = {
collectCoverageFrom: [
'src/**'
],
testPathIgnorePatterns: [
"__tests__/_helper.ts"
]
};

export default config;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"description": "AsyncAPI Problem interface",
"scripts": {
"build": "tsc",
"test": "cross-env CI=true jest --coverage __tests__/*.test.ts",
"test": "cross-env CI=true jest --coverage",
"lint": "eslint --no-error-on-unmatched-pattern --max-warnings 0 --config \".eslintrc\" \".\"",
"lint:fix": "eslint --no-error-on-unmatched-pattern --max-warnings 0 --config \".eslintrc\" \".\" --fix",
"generate:readme:toc": "markdown-toc -i \"README.md\""
},

"bugs": {
"url": "https://github.com/imabp/asyncapi_problem/issues"
},
Expand Down
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ export const DEFAULT_KEYS = [
"instance",
"stack",
];

export enum COPY_MODE {
SKIP_PROPS = "skipProps",
LEAVE_PROPS = "leaveProps",
}
33 changes: 15 additions & 18 deletions src/problem.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
import { DEFAULT_KEYS } from "./constants";
import {
httpObject,
HttpObject,
ProblemInterface,
ToJsonParamType,
UpdateProblemParamType,
} from "./types";

export enum COPY_MODE {
SKIP_PROPS = "skipProps",
LEAVE_PROPS = "leaveProps",
}
import { COPY_MODE } from "./constants";

export class Problem extends Error implements ProblemInterface {
public type: string;
public title: string;
public instance?: string;
public detail?: string;
public http?: httpObject;
public http?: HttpObject;
[key: string]: any;

constructor(
protected readonly problem: ProblemInterface,
) {
constructor(protected readonly problem: ProblemInterface) {
super(problem.detail || problem.title);
this.http = problem.http;
this.type = problem.type;
Expand All @@ -39,19 +34,21 @@ export class Problem extends Error implements ProblemInterface {
copy(mode: COPY_MODE = COPY_MODE.LEAVE_PROPS, props: string[] = []): Problem {
switch (mode) {
case COPY_MODE.LEAVE_PROPS:
return new Problem(problem, props);
const newProblem = new Problem(this.problem);
props.forEach((prop) => (newProblem[prop] = undefined));
return newProblem;

case COPY_MODE.SKIP_PROPS:
default:
default: {
const newProblem = new Problem(this.problem);
let keysToBeCopied: string[] = [];
for (let key in problem) {
if (props.includes(key)) continue;
for (let key in newProblem) {
// Default Keys cannot be skipped
if (props.includes(key) && !DEFAULT_KEYS.includes(key)) continue;
keysToBeCopied.push(key);
}
return new Problem(
{ type: problem.type, title: problem.title },
keysToBeCopied
);
return newProblem;
}
}
}

Expand All @@ -73,7 +70,7 @@ export class Problem extends Error implements ProblemInterface {
}

update({ updates }: UpdateProblemParamType) {
Object.keys(updates).forEach((i) => {
Object.keys(updates).forEach((i) => {
this[i] = updates[i];
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Problem } from "../problem";

export type ProblemInterface = {
type: string;
title: string; // Title should be description of http status, if type is not present.
http?: httpObject;
title: string;
http?: HttpObject;
detail?: string;
instance?: string; // Details to reproduce the error.
instance?: string;
stack?: string;
[key: string]: any; // Custom Field of Problem
[key: string]: any;
};

export type HttpObject = {
Expand Down

0 comments on commit fbf15b4

Please sign in to comment.