|
| 1 | +import { resolveInlineRef } from '../resolveInlineRef'; |
| 2 | + |
| 3 | +describe('resolveInlineRef', () => { |
| 4 | + test('should follow refs', () => { |
| 5 | + const doc = { |
| 6 | + a: { |
| 7 | + $ref: '#/b/foo', |
| 8 | + }, |
| 9 | + b: { |
| 10 | + $ref: '#/c', |
| 11 | + }, |
| 12 | + c: { |
| 13 | + $ref: '#/d', |
| 14 | + }, |
| 15 | + d: { |
| 16 | + foo: 'woo!', |
| 17 | + }, |
| 18 | + }; |
| 19 | + |
| 20 | + expect(resolveInlineRef(doc, '#/a')).toEqual('woo!'); |
| 21 | + }); |
| 22 | + |
| 23 | + test('should follow refs #2', () => { |
| 24 | + const doc = { |
| 25 | + a: { |
| 26 | + $ref: '#/b/foo', |
| 27 | + }, |
| 28 | + b: { |
| 29 | + foo: { |
| 30 | + $ref: '#/c', |
| 31 | + }, |
| 32 | + bar: { |
| 33 | + $ref: '#/e', |
| 34 | + }, |
| 35 | + }, |
| 36 | + c: { |
| 37 | + $ref: '#/b/bar', |
| 38 | + }, |
| 39 | + e: 'woo!', |
| 40 | + }; |
| 41 | + |
| 42 | + expect(resolveInlineRef(doc, '#/a')).toEqual('woo!'); |
| 43 | + }); |
| 44 | + |
| 45 | + test('should handle direct circular refs', () => { |
| 46 | + const doc = { |
| 47 | + a: { |
| 48 | + $ref: '#/b', |
| 49 | + }, |
| 50 | + b: { |
| 51 | + $ref: '#/a', |
| 52 | + }, |
| 53 | + }; |
| 54 | + |
| 55 | + expect(resolveInlineRef(doc, '#/a')).toEqual({ |
| 56 | + $ref: '#/a', |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + test('should handle direct circular refs #2', () => { |
| 61 | + const doc = { |
| 62 | + a: { |
| 63 | + $ref: '#/b/foo', |
| 64 | + }, |
| 65 | + b: { |
| 66 | + foo: { |
| 67 | + $ref: '#/c', |
| 68 | + }, |
| 69 | + bar: { |
| 70 | + $ref: '#/e', |
| 71 | + }, |
| 72 | + }, |
| 73 | + c: { |
| 74 | + $ref: '#/b/bar', |
| 75 | + }, |
| 76 | + e: { |
| 77 | + $ref: '#/a', |
| 78 | + }, |
| 79 | + }; |
| 80 | + |
| 81 | + expect(resolveInlineRef(doc, '#/a')).toEqual({ |
| 82 | + $ref: '#/a', |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + test('given external reference, should throw', () => { |
| 87 | + const doc = { |
| 88 | + a: { |
| 89 | + $ref: './foo#/b/foo', |
| 90 | + }, |
| 91 | + }; |
| 92 | + |
| 93 | + expect(resolveInlineRef.bind(null, doc, '#/a')).toThrowError('Cannot resolve external references'); |
| 94 | + }); |
| 95 | + |
| 96 | + test('given missing segment, should throw', () => { |
| 97 | + const doc = { |
| 98 | + a: { |
| 99 | + $ref: '#/b/foo', |
| 100 | + }, |
| 101 | + b: { |
| 102 | + bar: false, |
| 103 | + }, |
| 104 | + }; |
| 105 | + |
| 106 | + expect(resolveInlineRef.bind(null, doc, '#/a')).toThrowError("Could not resolve '#/b/foo'"); |
| 107 | + }); |
| 108 | + |
| 109 | + test('given path pointing at invalid data, should throw', () => { |
| 110 | + const doc = { |
| 111 | + a: { |
| 112 | + $ref: '#/b/foo/bar', |
| 113 | + }, |
| 114 | + b: { |
| 115 | + foo: null, |
| 116 | + }, |
| 117 | + }; |
| 118 | + |
| 119 | + expect(resolveInlineRef.bind(null, doc, '#/a')).toThrowError("Could not resolve '#/b/foo/bar'"); |
| 120 | + }); |
| 121 | + |
| 122 | + test('given invalid $ref type, should throw', () => { |
| 123 | + const doc = { |
| 124 | + a: { |
| 125 | + $ref: 2, |
| 126 | + }, |
| 127 | + }; |
| 128 | + |
| 129 | + expect(resolveInlineRef.bind(null, doc, '#/a')).toThrowError('$ref should be a string'); |
| 130 | + }); |
| 131 | +}); |
0 commit comments