|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tap').test |
| 4 | +const fjs = require('..') |
| 5 | + |
| 6 | +function build (opts) { |
| 7 | + return fjs({ |
| 8 | + title: 'default string', |
| 9 | + type: 'object', |
| 10 | + properties: { |
| 11 | + firstName: { |
| 12 | + type: 'string' |
| 13 | + } |
| 14 | + }, |
| 15 | + required: ['firstName'] |
| 16 | + }, opts) |
| 17 | +} |
| 18 | + |
| 19 | +test('activate debug mode', t => { |
| 20 | + t.plan(2) |
| 21 | + const debugMode = build({ debugMode: true }) |
| 22 | + t.type(debugMode, Array) |
| 23 | + t.like(debugMode.toString.toString(), 'join', 'to string override') |
| 24 | +}) |
| 25 | + |
| 26 | +test('activate debug mode truthy', t => { |
| 27 | + t.plan(2) |
| 28 | + const debugMode = build({ debugMode: 'yes' }) |
| 29 | + t.type(debugMode, Array) |
| 30 | + t.like(debugMode.toString.toString(), 'join', 'to string override') |
| 31 | +}) |
| 32 | + |
| 33 | +test('to string auto-consistent', t => { |
| 34 | + t.plan(2) |
| 35 | + const debugMode = build({ debugMode: 1 }) |
| 36 | + t.type(debugMode, Array) |
| 37 | + |
| 38 | + const str = debugMode.toString() |
| 39 | + const compiled = fjs.restore(str) |
| 40 | + const tobe = JSON.stringify({ firstName: 'Foo' }) |
| 41 | + t.deepEquals(compiled({ firstName: 'Foo', surname: 'bar' }), tobe, 'surname evicted') |
| 42 | +}) |
| 43 | + |
| 44 | +test('to string auto-consistent with ajv', t => { |
| 45 | + t.plan(2) |
| 46 | + const debugMode = fjs({ |
| 47 | + title: 'object with multiple types field', |
| 48 | + type: 'object', |
| 49 | + properties: { |
| 50 | + str: { |
| 51 | + anyOf: [{ |
| 52 | + type: 'string' |
| 53 | + }, { |
| 54 | + type: 'boolean' |
| 55 | + }] |
| 56 | + } |
| 57 | + } |
| 58 | + }, { debugMode: 1 }) |
| 59 | + t.type(debugMode, Array) |
| 60 | + |
| 61 | + const str = debugMode.toString() |
| 62 | + const compiled = fjs.restore(str) |
| 63 | + const tobe = JSON.stringify({ str: 'Foo' }) |
| 64 | + t.deepEquals(compiled({ str: 'Foo', void: 'me' }), tobe) |
| 65 | +}) |
0 commit comments