Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
Increase coverage to 100
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Mar 30, 2019
1 parent 2b3fbe5 commit 173f849
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/array-converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,10 @@ describe("Array Converter", () => {
const b = convert("1", [Number])
expect(b).toEqual([1])
})

it("Should not error when provided single array on guess element mode", () => {
const convert = createConverter({ guessArrayElement: true })
const b = convert(["1"], [Number])
expect(b).toEqual([1])
})
})
65 changes: 65 additions & 0 deletions test/durability.spec.ts
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")
}
})
})

0 comments on commit 173f849

Please sign in to comment.