aboutsummaryrefslogtreecommitdiffstats
path: root/test/schema_test.ts
blob: 133914e6d2433ab74f311d4af907cb35b5d9a71b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import 'mocha';
import * as _ from 'lodash';
import * as chai from 'chai';
import * as BigNumber from 'bignumber.js';
import promisify = require('es6-promisify');
import {numberSchema} from '../src/schemas/order_schemas';
import {SchemaValidator} from '../src/utils/schema_validator';

chai.config.includeStack = true;
const expect = chai.expect;

describe('Schema', () => {
    const validator = new SchemaValidator();
    describe('#numberSchema', () => {
        describe('number regex', () => {
            it('should validate valid numbers', () => {
                const testCases = ['42', '0', '1.3', '0.2', '00.00'];
                _.forEach(testCases, (testCase: string) => {
                    expect(validator.validate(testCase as any, numberSchema).errors).to.be.lengthOf(0);
                });
            });
            it('should fail for invalid numbers', () => {
                const testCases = ['.3', '1.', 'abacaba', 'и', '1..0'];
                _.forEach(testCases, (testCase: string) => {
                    expect(validator.validate(testCase as any, numberSchema).errors).to.be.lengthOf(1);
                });
            });
        });
    });
    describe('BigNumber serialization', () => {
        it.only('should correctly serialize BigNumbers', () => {
            const testCases = {
                '42': '42',
                '0': '0',
                '1.3': '1.3',
                '0.2': '0.2',
                '00.00': '0',
                '.3': '0.3',
            };
            _.forEach(testCases, (serialized: string, input: string) => {
                expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber(input)))
                    .to.be.equal(serialized);
            });
        });
    });
});