From 63020a0dbaaac1b088e16e6e17435af8670cd8af Mon Sep 17 00:00:00 2001 From: Manuel Spigolon Date: Thu, 7 Dec 2023 18:09:14 +0100 Subject: [PATCH] test: add tests (#8) --- test/ref-local.test.js | 146 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 test/ref-local.test.js diff --git a/test/ref-local.test.js b/test/ref-local.test.js new file mode 100644 index 0000000..38afaa8 --- /dev/null +++ b/test/ref-local.test.js @@ -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' + } + } + } + } + }) +})