Skip to content

Commit 6992d56

Browse files
authored
test: improve test coverage (#822)
1 parent a18917e commit 6992d56

7 files changed

Lines changed: 206 additions & 0 deletions

File tree

test/anyof.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,3 +790,23 @@ test('should build merged schemas twice', (t) => {
790790
t.assert.equal(stringify({ enums: 'BAR' }), '{"enums":"BAR"}')
791791
}
792792
})
793+
794+
test('invalid anyOf schema', (t) => {
795+
t.plan(1)
796+
797+
const schema = {
798+
type: 'object',
799+
properties: {
800+
prop: {
801+
anyOf: 'not array' // invalid, anyOf must be array
802+
}
803+
}
804+
}
805+
806+
try {
807+
build(schema)
808+
t.assert.fail('Should throw')
809+
} catch (err) {
810+
t.assert.ok(err.message.includes('schema is invalid'))
811+
}
812+
})

test/array.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,28 @@ test('array items is a schema and additionalItems is false', (t) => {
386386
t.assert.equal(validate({ foo: ['foo', 'bar'] }), true)
387387
})
388388

389+
test('array items is a list of schema and additionalItems is a schema', (t) => {
390+
t.plan(1)
391+
392+
const schema = {
393+
type: 'object',
394+
properties: {
395+
foo: {
396+
type: 'array',
397+
items: [
398+
{ type: 'string' }
399+
],
400+
additionalItems: { type: 'number' }
401+
}
402+
}
403+
}
404+
405+
const stringify = build(schema)
406+
const result = stringify({ foo: ['foo', 42] })
407+
408+
t.assert.equal(result, '{"foo":["foo",42]}')
409+
})
410+
389411
// https://github.com/fastify/fast-json-stringify/issues/279
390412
test('object array with anyOf and symbol', (t) => {
391413
t.plan(1)

test/debug-mode.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,24 @@ test('debug should restore the same serializer instance', t => {
119119
const compiled = fjs.restore(debugMode)
120120
t.assert.equal(compiled(3.95), 4)
121121
})
122+
123+
test('Serializer restoreFromState', t => {
124+
t.plan(1)
125+
126+
const Serializer = require('../lib/serializer')
127+
const serializer = new Serializer()
128+
const state = serializer.getState()
129+
const restored = Serializer.restoreFromState(state)
130+
t.assert.ok(restored instanceof Serializer)
131+
})
132+
133+
test('Validator restoreFromState', t => {
134+
t.plan(1)
135+
136+
const Validator = require('../lib/validator')
137+
const validator = new Validator()
138+
validator.addSchema({ type: 'string' }, 'test')
139+
const state = validator.getState()
140+
const restored = Validator.restoreFromState(state)
141+
t.assert.ok(restored instanceof Validator)
142+
})

test/if-then-else.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,75 @@ test('external recursive if/then/else', (t) => {
466466
const stringify = build(schema, { schema: { externalSchema } })
467467
t.assert.equal(stringify(data), '{"a":{"base":"a","bar":"42"},"b":{"base":"b","baz":"43"}}')
468468
})
469+
470+
test('invalid if-then-else schema', (t) => {
471+
t.plan(1)
472+
473+
const schema = {
474+
type: 'object',
475+
if: {
476+
type: 'object',
477+
properties: {
478+
kind: { type: 'string', enum: ['foobar'] }
479+
}
480+
},
481+
then: {
482+
type: 'object',
483+
properties: {
484+
foo: { type: 'string' }
485+
}
486+
},
487+
else: {
488+
type: 'object',
489+
properties: 'invalid' // properties must be object
490+
}
491+
}
492+
493+
try {
494+
build(schema)
495+
t.assert.fail('Should throw')
496+
} catch (err) {
497+
t.assert.ok(err.message.includes('schema is invalid'))
498+
}
499+
})
500+
501+
test('if-then-else with allOf', (t) => {
502+
t.plan(2)
503+
504+
const schema = {
505+
type: 'object',
506+
allOf: [
507+
{
508+
type: 'object',
509+
properties: {
510+
base: { type: 'string' }
511+
}
512+
}
513+
],
514+
if: {
515+
type: 'object',
516+
properties: {
517+
kind: { type: 'string', enum: ['foobar'] }
518+
}
519+
},
520+
then: {
521+
type: 'object',
522+
properties: {
523+
foo: { type: 'string' }
524+
}
525+
},
526+
else: {
527+
type: 'object',
528+
properties: {
529+
bar: { type: 'string' }
530+
}
531+
}
532+
}
533+
534+
const stringify = build(schema)
535+
const data = { base: 'test', kind: 'foobar', foo: 'value' }
536+
const output = stringify(data)
537+
538+
t.assert.doesNotThrow(() => JSON.parse(output))
539+
t.assert.equal(output, '{"base":"test","foo":"value"}')
540+
})

test/invalidSchema.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,23 @@ test('Should throw on invalid schema', t => {
1616
})
1717
}, { message: /^"invalid" schema is invalid:.*/ })
1818
})
19+
20+
test('invalid not schema', (t) => {
21+
t.plan(1)
22+
23+
const schema = {
24+
type: 'object',
25+
properties: {
26+
prop: {
27+
not: 'not object' // invalid, not must be object
28+
}
29+
}
30+
}
31+
32+
try {
33+
build(schema)
34+
t.assert.fail('Should throw')
35+
} catch (err) {
36+
t.assert.ok(err.message.includes('schema is invalid'))
37+
}
38+
})

test/oneof.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,23 @@ test('all array items does not match oneOf types', (t) => {
488488

489489
t.assert.throws(() => stringify({ data: [null, false, true, undefined, [], {}] }))
490490
})
491+
492+
test('invalid oneOf schema', (t) => {
493+
t.plan(1)
494+
495+
const schema = {
496+
type: 'object',
497+
properties: {
498+
prop: {
499+
oneOf: 'not array' // invalid, oneOf must be array
500+
}
501+
}
502+
}
503+
504+
try {
505+
build(schema)
506+
t.assert.fail('Should throw')
507+
} catch (err) {
508+
t.assert.ok(err.message.includes('schema is invalid'))
509+
}
510+
})

test/ref.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,3 +2044,34 @@ test('ref internal - throw if schema has definition twice with different shape',
20442044
t.assert.equal(err.message, 'There is already another anchor "#uri" in schema "test".')
20452045
}
20462046
})
2047+
2048+
test('ref nested', (t) => {
2049+
t.plan(2)
2050+
2051+
const schema = {
2052+
definitions: {
2053+
def1: {
2054+
$ref: '#/definitions/def2'
2055+
},
2056+
def2: {
2057+
type: 'string'
2058+
}
2059+
},
2060+
type: 'object',
2061+
properties: {
2062+
str: {
2063+
$ref: '#/definitions/def1'
2064+
}
2065+
}
2066+
}
2067+
2068+
const object = {
2069+
str: 'test'
2070+
}
2071+
2072+
const stringify = build(schema)
2073+
const output = stringify(object)
2074+
2075+
t.assert.doesNotThrow(() => JSON.parse(output))
2076+
t.assert.equal(output, '{"str":"test"}')
2077+
})

0 commit comments

Comments
 (0)