Skip to content

Commit

Permalink
test: add tests (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm authored Dec 7, 2023
1 parent 2bdacb3 commit 63020a0
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions test/ref-local.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
'use strict'

const { test } = require('tap')

const RefResolver = require('../ref-resolver')

test('Resolve absolute refs in schema', t => {
t.plan(1)
const opts = {
externalSchemas: [
{
$id: 'ObjectA',
type: 'object',
properties: {
example: {
type: 'string'
}
}
},
{
$id: 'ObjectC',
type: 'object',
properties: {
referencedObjA: {
$ref: 'ObjectA#'
},
referencedObjC: {
$ref: 'ObjectC#/properties/ObjectD'
},
ObjectD: {
type: 'object',
properties: {
d: {
type: 'string'
}
}
}
}
}
]
}
const resolver = RefResolver()

const out = resolver.resolve({
$ref: 'ObjectC#'
}, opts)

t.same(out, {
$ref: '#/definitions/def-1',
definitions: {
'def-0': {
$id: 'ObjectA',
type: 'object',
properties: {
example: {
type: 'string'
}
}
},
'def-1': {
$id: 'ObjectC',
type: 'object',
properties: {
referencedObjA: {
$ref: '#/definitions/def-0'
},
referencedObjC: {
$ref: '#/definitions/def-1/properties/ObjectD'
},
ObjectD: {
type: 'object',
properties: {
d: {
type: 'string'
}
}
}
}
}
}
})
})

test('Resolve relative refs in schema', t => {
t.plan(1)
const opts = {
externalSchemas: [
{
$id: 'ObjectA',
type: 'object',
properties: {
sample: {
type: 'object',
properties: {
a: { type: 'string' },
b: { type: 'object', properties: { d: { type: 'string' } } }
}
},
someValue: { type: 'string' },
relativeExample: {
$ref: '#/properties/sample'
}
}
}
]
}
const resolver = RefResolver()

const out = resolver.resolve({
$ref: 'ObjectA#/properties/relativeExample'
}, opts)

t.same(out, {
$ref: '#/definitions/def-0/properties/relativeExample',
definitions: {
'def-0': {
$id: 'ObjectA',
type: 'object',
properties: {
sample: {
type: 'object',
properties: {
a: {
type: 'string'
},
b: {
type: 'object',
properties: {
d: {
type: 'string'
}
}
}
}
},
someValue: {
type: 'string'
},
relativeExample: {
$ref: '#/properties/sample'
}
}
}
}
})
})

0 comments on commit 63020a0

Please sign in to comment.