This repository has been archived by the owner on Dec 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
2 changed files
with
71 additions
and
0 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,65 @@ | ||
import createConverter, { ConversionError } from "../src" | ||
import reflect from 'tinspector'; | ||
|
||
const convert = createConverter() | ||
|
||
describe("Durability test", () => { | ||
it("Should return non converted value if no expected type provided", () => { | ||
const result = convert({a: "123", b: 100}) | ||
expect(result).toEqual({a: "123", b: 100}) | ||
}) | ||
|
||
it("Should not error if provided non safe to string", () => { | ||
try { | ||
const result = convert(Object.create(null), Number) | ||
} | ||
catch (e) { | ||
expect(e.issues).toEqual({ | ||
path: [], | ||
messages: ['Unable to convert "[object Object]" into Number'], | ||
}) | ||
} | ||
}) | ||
|
||
it("Should provide friendly message if unable to instantiate object", () => { | ||
@reflect.parameterProperties() | ||
class AnimalClass { | ||
constructor( | ||
public id: number, | ||
public name: string, | ||
public deceased: boolean, | ||
public birthday: Date | ||
) { | ||
throw new Error("ERROR") | ||
} | ||
} | ||
|
||
try{ | ||
convert({ id: "200", name: "Mimi", deceased: "ON", birthday: "2018-1-1" }, AnimalClass) | ||
} | ||
catch(e){ | ||
expect(e.message).toContain("Unable to instantiate AnimalClass") | ||
} | ||
}) | ||
|
||
it("Should provide friendly message if unable to instantiate object", () => { | ||
@reflect.parameterProperties() | ||
class AnimalClass { | ||
constructor( | ||
public id: number, | ||
public name: string, | ||
public deceased: boolean, | ||
public birthday: Date | ||
) { | ||
throw "ERROR" | ||
} | ||
} | ||
|
||
try{ | ||
convert({ id: "200", name: "Mimi", deceased: "ON", birthday: "2018-1-1" }, AnimalClass) | ||
} | ||
catch(e){ | ||
expect(e.message).toContain("Unable to instantiate AnimalClass") | ||
} | ||
}) | ||
}) |