Skip to content

Commit

Permalink
feat: add tests and github action file
Browse files Browse the repository at this point in the history
  • Loading branch information
imabp committed Aug 5, 2022
1 parent c99b63a commit 2289f6d
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 55 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/node.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Problem CI

on:
push:
branches: [ main ]
pull:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '12.x'

- name: Install Dependencies
run: yarn install

- name: Run tests
run: yarn test

- name: Build Problem Library
run: yarn build
19 changes: 19 additions & 0 deletions __tests__/_helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Problem } from "../src/index"
import { ProblemInterface } from "../src/types"

export default class ProblemContextHelper {

_problemInstance: Problem;

_instanceOptions: ProblemInterface = {
type: "null-or-falsey-document",
title: "The AsyncAPI document is null or a JS falsey value.",
detail: "The AsyncAPI document is null or a JS falsey value."
}


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

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

const _testContext = new ProblemContextHelper();

test("create problem with custom keys", () => {
const customKey = "RCA";
const testProblem = new Problem(_testContext._problemInstance, ["RCA"]);
expect(testProblem).toHaveProperty(customKey);
});

test('copy problem with mode="LEAVE PROPS"', () => {
const _problemCopy = _testContext._problemInstance.copy(
_testContext._problemInstance,
COPY_MODE.LEAVE_PROPS,
[]
);
expect(_problemCopy).toBeInstanceOf(Problem);
expect(_problemCopy.type).toBe(_testContext._problemInstance.type);
expect(_problemCopy.title).toBe(_testContext._problemInstance.title);
});

test('copy problem with mode="SKIP PROPS"', () => {
const _copiedProblem = _testContext._problemInstance.copy(
_testContext._problemInstance,
COPY_MODE.SKIP_PROPS,
["details"]
);
expect(_copiedProblem.details).toBeUndefined();
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "AsyncAPI Problem interface",
"scripts": {
"build": "tsc",
"test": "cross-env CI=true jest --coverage",
"test": "cross-env CI=true jest --coverage __tests__/*.test.ts",
"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\""
Expand Down
103 changes: 50 additions & 53 deletions src/problem.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,59 @@
import { httpObject, ProblemInterface } from "types";
import { httpObject, ProblemInterface } from "./types";

enum COPY_MODE {
SKIP_PROPS = 'skipProps',
LEAVE_PROPS = 'leaveProps'
export enum COPY_MODE {
SKIP_PROPS = "skipProps",
LEAVE_PROPS = "leaveProps",
}

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


constructor(problem: ProblemInterface, customKeys?: string[]) {
super(problem.detail || problem.title);
this.http = problem.http
this.type = problem.type
this.title = problem.title;
this.detail = problem.detail;
this.instance = problem.instance;
this.stack = problem.stack;
customKeys?.map((customKey) => {
this[customKey] = problem[customKey];
})
}

copy(problem: ProblemInterface, mode: COPY_MODE, props: string[]): Problem {
switch (mode) {

case COPY_MODE.LEAVE_PROPS:
return new Problem(problem, props);

case COPY_MODE.SKIP_PROPS:
default:
let keysToBeCopied: string[] = [];
for (let key in problem) {
if (props.includes(key))
continue;
keysToBeCopied.push(key)
}
return new Problem(problem, keysToBeCopied)
public type: string;
public title: string;
public instance?: string;
public detail?: string;
public http?: httpObject;
[key: string]: any;

constructor(problem: ProblemInterface, customKeys?: string[]) {
super(problem.detail || problem.title);
this.http = problem.http;
this.type = problem.type;
this.title = problem.title;
this.detail = problem.detail;
this.instance = problem.instance;
this.stack = problem.stack;
customKeys?.map((customKey) => {
this[customKey] = problem[customKey];
});
}

copy(problem: ProblemInterface, mode: COPY_MODE, props: string[]): Problem {
switch (mode) {
case COPY_MODE.LEAVE_PROPS:
return new Problem(problem, props);

case COPY_MODE.SKIP_PROPS:
default:
let keysToBeCopied: string[] = [];
for (let key in problem) {
if (props.includes(key)) continue;
keysToBeCopied.push(key);
}
};

toJSON(problem: Problem, includeStack = false): ProblemInterface {

const { name, message, stack, ...rest } = problem;
return new Problem(
{ type: problem.type, title: problem.title },
keysToBeCopied
);
}
}

const jsonObject = {
...rest
}
toJSON(problem: Problem, includeStack = false): ProblemInterface {
const { name, message, stack, ...rest } = problem;

if (includeStack)
jsonObject.stack = stack;
const jsonObject = {
...rest,
};

return jsonObject;
if (includeStack) jsonObject.stack = stack;

}
};
return jsonObject;
}
}
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type ProblemInterface = {
http?: httpObject,
type: string,
title: string, // Title should be description of http status, if type is not present.
http?: httpObject,
detail?: string,
instance?: string, // Details to reproduce the error.
stack?: string;
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"lib": [
"esnext"
],
"paths": {
"@types":["./src/types"]
},
"composite": true,
"declaration": true,
"allowJs": true,
"skipLibCheck": true,
Expand All @@ -19,7 +23,12 @@
"resolveJsonModule": true,
"isolatedModules": true,
},

"include": [
"src"
],
"exclude": [
"__tests__",
"./lib"
]
}

0 comments on commit 2289f6d

Please sign in to comment.