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
1 parent
aabca6f
commit 85c5098
Showing
2 changed files
with
143 additions
and
126 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,143 @@ | ||
import * as iots from 'io-ts'; | ||
import * as test from 'tape'; | ||
|
||
import { reporter } from '../src'; | ||
|
||
test('formats keyof unions as "regular" types', t => { | ||
const WithKeyOf = iots.interface({ | ||
oneOf: iots.keyof({ a: null, b: null, c: null }) | ||
}); | ||
|
||
t.deepEqual(reporter(WithKeyOf.decode({ oneOf: '' })), [ | ||
'Expecting "a" | "b" | "c" at oneOf but instead got: ""' | ||
]); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('union of interfaces', t => { | ||
const UnionOfInterfaces = iots.union([ | ||
iots.interface({ key: iots.string }), | ||
iots.interface({ code: iots.number }) | ||
]); | ||
const WithUnion = iots.interface({ data: UnionOfInterfaces }); | ||
|
||
t.deepEqual(reporter(WithUnion.decode({})), [ | ||
[ | ||
'Expecting one of:', | ||
' { key: string }', | ||
' { code: number }', | ||
'at data but instead got: undefined' | ||
].join('\n') | ||
]); | ||
|
||
t.deepEqual(reporter(WithUnion.decode({ data: '' })), [ | ||
[ | ||
'Expecting one of:', | ||
' { key: string }', | ||
' { code: number }', | ||
'at data but instead got: ""' | ||
].join('\n') | ||
]); | ||
|
||
t.deepEqual(reporter(WithUnion.decode({ data: {} })), [ | ||
[ | ||
'Expecting one of:', | ||
' { key: string }', | ||
' { code: number }', | ||
'at data but instead got: {}' | ||
].join('\n') | ||
]); | ||
|
||
t.deepEqual(reporter(WithUnion.decode({ data: { code: '123' } })), [ | ||
[ | ||
'Expecting one of:', | ||
' { key: string }', | ||
' { code: number }', | ||
'at data but instead got: {"code":"123"}' | ||
].join('\n') | ||
]); | ||
|
||
t.end(); | ||
}); | ||
|
||
const Gender = iots.union([ | ||
iots.literal('Male'), | ||
iots.literal('Female'), | ||
iots.literal('Other') | ||
]); | ||
|
||
test('string union when provided undefined', t => { | ||
const Person = iots.interface({ name: iots.string, gender: Gender }); | ||
|
||
t.deepEqual(reporter(Person.decode({ name: 'Jane' })), [ | ||
[ | ||
'Expecting one of:', | ||
' "Male"', | ||
' "Female"', | ||
' "Other"', | ||
'at gender but instead got: undefined' | ||
].join('\n') | ||
]); | ||
t.end(); | ||
}); | ||
|
||
test('string union when provided another string', t => { | ||
const Person = iots.interface({ name: iots.string, gender: Gender }); | ||
|
||
t.deepEqual(reporter(Person.decode({ name: 'Jane', gender: 'female' })), [ | ||
[ | ||
'Expecting one of:', | ||
' "Male"', | ||
' "Female"', | ||
' "Other"', | ||
'at gender but instead got: "female"' | ||
].join('\n') | ||
]); | ||
|
||
t.deepEqual(reporter(Person.decode({ name: 'Jane' })), [ | ||
[ | ||
'Expecting one of:', | ||
' "Male"', | ||
' "Female"', | ||
' "Other"', | ||
'at gender but instead got: undefined' | ||
].join('\n') | ||
]); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('string union deeply nested', t => { | ||
const Person = iots.interface({ | ||
name: iots.string, | ||
children: iots.array(iots.interface({ gender: Gender })) | ||
}); | ||
|
||
t.deepEqual( | ||
reporter( | ||
Person.decode({ | ||
name: 'Jane', | ||
children: [{}, { gender: 'Whatever' }] | ||
}) | ||
), | ||
[ | ||
[ | ||
'Expecting one of:', | ||
' "Male"', | ||
' "Female"', | ||
' "Other"', | ||
'at children.0.gender but instead got: undefined' | ||
].join('\n'), | ||
[ | ||
'Expecting one of:', | ||
' "Male"', | ||
' "Female"', | ||
' "Other"', | ||
'at children.0.gender but instead got: "Whatever"' | ||
].join('\n') | ||
] | ||
); | ||
|
||
t.end(); | ||
}); |