console-app/test/spec/types.ts

103 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-10-03 15:14:14 +02:00
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of console-app which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, October 2020
*/
import { TypeValidation } from '../../lib/TypeValidation/TypeValidation';
// eslint-disable-next-line max-lines-per-function
describe ('type validation', () => {
it ('string', async () => {
const validator = new TypeValidation ('string');
const res = await validator.to_type ('foo');
expect (res)
.toEqual ('foo');
});
it ('no number', () => {
const validator = new TypeValidation ('number');
expect (
() => validator.to_type ('foo')
)
.toThrowError ('value is not a number');
});
it ('number', async () => {
const validator = new TypeValidation ('number');
const res = await validator.to_type ('123.4');
expect (res)
.toEqual (123.4);
});
it ('int', async () => {
const validator = new TypeValidation ('int');
const res = await validator.to_type ('123.4');
expect (res)
.toEqual (123);
});
it ('no boolean', () => {
const validator = new TypeValidation ('boolean');
expect (
() => validator.to_type ('foo')
)
.toThrowError ('value is not a boolean');
});
it ('boolean', async () => {
const validator = new TypeValidation ('boolean');
const r1 = await validator.to_type ('false');
const r2 = await validator.to_type ('true');
expect (r1)
.toEqual (false);
expect (r2)
.toEqual (true);
});
it ('boolean number', async () => {
const validator = new TypeValidation ('boolean');
const r1 = await validator.to_type (0);
const r2 = await validator.to_type (1);
expect (r1)
.toEqual (false);
expect (r2)
.toEqual (true);
});
it ('no array', () => {
const validator = new TypeValidation ('array');
expect (
() => validator.to_type (1)
)
.toThrowError ('value is not an array');
});
it ('array', async () => {
const validator = new TypeValidation ('array');
const res = await validator.to_type ([
'foo',
'bar',
'baz'
]);
expect (res)
.toEqual ([
'foo',
'bar',
'baz'
]);
});
it ('string array', async () => {
const validator = new TypeValidation ('array');
const res = await validator.to_type ('f o o,bar , baz');
expect (res)
.toEqual ([
'f o o',
'bar',
'baz'
]);
});
});