Skip to content

Commit 63020a0

Browse files
authored
test: add tests (#8)
1 parent 2bdacb3 commit 63020a0

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

test/ref-local.test.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
5+
const RefResolver = require('../ref-resolver')
6+
7+
test('Resolve absolute refs in schema', t => {
8+
t.plan(1)
9+
const opts = {
10+
externalSchemas: [
11+
{
12+
$id: 'ObjectA',
13+
type: 'object',
14+
properties: {
15+
example: {
16+
type: 'string'
17+
}
18+
}
19+
},
20+
{
21+
$id: 'ObjectC',
22+
type: 'object',
23+
properties: {
24+
referencedObjA: {
25+
$ref: 'ObjectA#'
26+
},
27+
referencedObjC: {
28+
$ref: 'ObjectC#/properties/ObjectD'
29+
},
30+
ObjectD: {
31+
type: 'object',
32+
properties: {
33+
d: {
34+
type: 'string'
35+
}
36+
}
37+
}
38+
}
39+
}
40+
]
41+
}
42+
const resolver = RefResolver()
43+
44+
const out = resolver.resolve({
45+
$ref: 'ObjectC#'
46+
}, opts)
47+
48+
t.same(out, {
49+
$ref: '#/definitions/def-1',
50+
definitions: {
51+
'def-0': {
52+
$id: 'ObjectA',
53+
type: 'object',
54+
properties: {
55+
example: {
56+
type: 'string'
57+
}
58+
}
59+
},
60+
'def-1': {
61+
$id: 'ObjectC',
62+
type: 'object',
63+
properties: {
64+
referencedObjA: {
65+
$ref: '#/definitions/def-0'
66+
},
67+
referencedObjC: {
68+
$ref: '#/definitions/def-1/properties/ObjectD'
69+
},
70+
ObjectD: {
71+
type: 'object',
72+
properties: {
73+
d: {
74+
type: 'string'
75+
}
76+
}
77+
}
78+
}
79+
}
80+
}
81+
})
82+
})
83+
84+
test('Resolve relative refs in schema', t => {
85+
t.plan(1)
86+
const opts = {
87+
externalSchemas: [
88+
{
89+
$id: 'ObjectA',
90+
type: 'object',
91+
properties: {
92+
sample: {
93+
type: 'object',
94+
properties: {
95+
a: { type: 'string' },
96+
b: { type: 'object', properties: { d: { type: 'string' } } }
97+
}
98+
},
99+
someValue: { type: 'string' },
100+
relativeExample: {
101+
$ref: '#/properties/sample'
102+
}
103+
}
104+
}
105+
]
106+
}
107+
const resolver = RefResolver()
108+
109+
const out = resolver.resolve({
110+
$ref: 'ObjectA#/properties/relativeExample'
111+
}, opts)
112+
113+
t.same(out, {
114+
$ref: '#/definitions/def-0/properties/relativeExample',
115+
definitions: {
116+
'def-0': {
117+
$id: 'ObjectA',
118+
type: 'object',
119+
properties: {
120+
sample: {
121+
type: 'object',
122+
properties: {
123+
a: {
124+
type: 'string'
125+
},
126+
b: {
127+
type: 'object',
128+
properties: {
129+
d: {
130+
type: 'string'
131+
}
132+
}
133+
}
134+
}
135+
},
136+
someValue: {
137+
type: 'string'
138+
},
139+
relativeExample: {
140+
$ref: '#/properties/sample'
141+
}
142+
}
143+
}
144+
}
145+
})
146+
})

0 commit comments

Comments
 (0)