Skip to content

Commit

Permalink
test: Split in more granular tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gillchristian committed May 21, 2020
1 parent aabca6f commit 85c5098
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 126 deletions.
126 changes: 0 additions & 126 deletions tests/index.test.ts
Expand Up @@ -38,129 +38,3 @@ test('formats nested array item mismatches correctly', t => {
]);
t.end();
});

test('handles union types properly', t => {
const Unions = iots.interface({
oneOf: iots.keyof({ a: null, b: null, c: null }),
stringUnion: iots.union([
iots.literal('a'),
iots.literal('b'),
iots.literal('c')
]),
interfaceUnion: iots.union([
iots.interface({ key: iots.string }),
iots.interface({ code: iots.number })
])
});

t.deepEqual(
reporter(
Unions.decode({
oneOf: '',
stringUnion: '',
interfaceUnion: ''
})
),
[
[
'Expecting one of:',
' { key: string }',
' { code: number }',
'at interfaceUnion but instead got: ""'
].join('\n'),
'Expecting "a" | "b" | "c" at oneOf but instead got: ""',
[
'Expecting one of:',
' "a"',
' "b"',
' "c"',
'at stringUnion but instead got: ""'
].join('\n')
]
);

t.deepEqual(
reporter(
Unions.decode({
oneOf: 'a',
stringUnion: 'a',
interfaceUnion: {}
})
),
[
[
'Expecting one of:',
' { key: string }',
' { code: number }',
'at interfaceUnion but instead got: {}'
].join('\n')
]
);

t.deepEqual(
reporter(
Unions.decode({
oneOf: 'a',
stringUnion: 'a',
interfaceUnion: { code: '123' }
})
),
[
[
'Expecting one of:',
' { key: string }',
' { code: number }',
'at interfaceUnion but instead got: {"code":"123"}'
].join('\n')
]
);

const Gender = iots.union([
iots.literal('Male'),
iots.literal('Female'),
iots.literal('Other')
]);
const Person = iots.interface({
name: iots.string,
gender: Gender,
children: iots.array(iots.interface({ gender: Gender }))
});

t.deepEqual(reporter(Person.decode({ name: 'Jane', children: [] })), [
[
'Expecting one of:',
' "Male"',
' "Female"',
' "Other"',
'at gender but instead got: undefined'
].join('\n')
]);

t.deepEqual(
reporter(
Person.decode({
name: 'Jane',
gender: 'female',
children: [{ gender: 'Whatever' }]
})
),
[
[
'Expecting one of:',
' "Male"',
' "Female"',
' "Other"',
'at gender but instead got: "female"'
].join('\n'),
[
'Expecting one of:',
' "Male"',
' "Female"',
' "Other"',
'at children.0.gender but instead got: "Whatever"'
].join('\n')
]
);

t.end();
});
143 changes: 143 additions & 0 deletions tests/unions.test.ts
@@ -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();
});

0 comments on commit 85c5098

Please sign in to comment.