-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: return Serializer instance in debug mode (#529)
- Loading branch information
1 parent
3a737e2
commit 26ef3cd
Showing
2 changed files
with
24 additions
and
7 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ const fjs = require('..') | |
|
||
const Ajv = require('ajv').default | ||
const Validator = require('../lib/validator') | ||
const Serializer = require('../lib/serializer') | ||
|
||
function build (opts) { | ||
return fjs({ | ||
|
@@ -20,33 +21,36 @@ function build (opts) { | |
} | ||
|
||
test('activate debug mode', t => { | ||
t.plan(4) | ||
t.plan(5) | ||
const debugMode = build({ debugMode: true }) | ||
|
||
t.type(debugMode, 'object') | ||
t.ok(debugMode.ajv instanceof Ajv) | ||
t.ok(debugMode.validator instanceof Validator) | ||
t.ok(debugMode.serializer instanceof Serializer) | ||
t.type(debugMode.code, 'string') | ||
}) | ||
|
||
test('activate debug mode truthy', t => { | ||
t.plan(4) | ||
t.plan(5) | ||
|
||
const debugMode = build({ debugMode: 'yes' }) | ||
|
||
t.type(debugMode, 'object') | ||
t.type(debugMode.code, 'string') | ||
t.ok(debugMode.ajv instanceof Ajv) | ||
t.ok(debugMode.validator instanceof Validator) | ||
t.ok(debugMode.serializer instanceof Serializer) | ||
}) | ||
|
||
test('to string auto-consistent', t => { | ||
t.plan(5) | ||
t.plan(6) | ||
const debugMode = build({ debugMode: 1 }) | ||
|
||
t.type(debugMode, 'object') | ||
t.type(debugMode.code, 'string') | ||
t.ok(debugMode.ajv instanceof Ajv) | ||
t.ok(debugMode.serializer instanceof Serializer) | ||
t.ok(debugMode.validator instanceof Validator) | ||
|
||
const compiled = fjs.restore(debugMode) | ||
|
@@ -55,7 +59,7 @@ test('to string auto-consistent', t => { | |
}) | ||
|
||
test('to string auto-consistent with ajv', t => { | ||
t.plan(5) | ||
t.plan(6) | ||
|
||
const debugMode = fjs({ | ||
title: 'object with multiple types field', | ||
|
@@ -75,6 +79,7 @@ test('to string auto-consistent with ajv', t => { | |
t.type(debugMode.code, 'string') | ||
t.ok(debugMode.ajv instanceof Ajv) | ||
t.ok(debugMode.validator instanceof Validator) | ||
t.ok(debugMode.serializer instanceof Serializer) | ||
|
||
const compiled = fjs.restore(debugMode) | ||
const tobe = JSON.stringify({ str: 'Foo' }) | ||
|
@@ -106,3 +111,11 @@ test('to string auto-consistent with ajv-formats', t => { | |
t.same(compiled({ str: '[email protected]' }), tobe) | ||
t.throws(() => compiled({ str: 'foo' })) | ||
}) | ||
|
||
test('debug should restore the same serializer instance', t => { | ||
t.plan(1) | ||
|
||
const debugMode = fjs({ type: 'integer' }, { debugMode: 1, rounding: 'ceil' }) | ||
const compiled = fjs.restore(debugMode) | ||
t.same(compiled(3.95), 4) | ||
}) |