-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tests and github action file
- Loading branch information
Showing
7 changed files
with
137 additions
and
55 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
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 |
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,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); | ||
} | ||
|
||
} |
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,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(); | ||
}); |
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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