|
| 1 | +import { reparentBundleTarget } from '../reparentBundleTarget'; |
| 2 | + |
| 3 | +describe('reparentBundleTarget', () => { |
| 4 | + it.each<[string, string]>([['#', '#/components'], ['#/components', '#'], ['#/components/schemas', '#/components']])( |
| 5 | + 'given %p paths, should throw', |
| 6 | + (from, to) => { |
| 7 | + expect(reparentBundleTarget.bind(null, {}, from, to)).toThrow(); |
| 8 | + }, |
| 9 | + ); |
| 10 | + |
| 11 | + it('should reparent refs', () => { |
| 12 | + const document = { |
| 13 | + properties: { |
| 14 | + user: { |
| 15 | + $ref: '#/definitions/User', |
| 16 | + }, |
| 17 | + }, |
| 18 | + definitions: { |
| 19 | + Name: { |
| 20 | + type: 'string', |
| 21 | + }, |
| 22 | + Admin: { |
| 23 | + properties: { |
| 24 | + name: { |
| 25 | + $ref: '#/definitions/Name', |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + Editor: { |
| 30 | + properties: { |
| 31 | + name: { |
| 32 | + $ref: '#/definitions/Name', |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + Users: { |
| 37 | + oneOf: [ |
| 38 | + { |
| 39 | + $ref: '#/definitions/Admin', |
| 40 | + }, |
| 41 | + { |
| 42 | + $ref: '#/definitions/Editor', |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + reparentBundleTarget(document, '#/definitions', '#/$defs'); |
| 50 | + |
| 51 | + expect(document).toStrictEqual({ |
| 52 | + properties: { |
| 53 | + user: { |
| 54 | + $ref: '#/$defs/User', |
| 55 | + }, |
| 56 | + }, |
| 57 | + $defs: { |
| 58 | + Name: { |
| 59 | + type: 'string', |
| 60 | + }, |
| 61 | + Admin: { |
| 62 | + properties: { |
| 63 | + name: { |
| 64 | + $ref: '#/$defs/Name', |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + Editor: { |
| 69 | + properties: { |
| 70 | + name: { |
| 71 | + $ref: '#/$defs/Name', |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + Users: { |
| 76 | + oneOf: [ |
| 77 | + { |
| 78 | + $ref: '#/$defs/Admin', |
| 79 | + }, |
| 80 | + { |
| 81 | + $ref: '#/$defs/Editor', |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + }, |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('given missing source, should do nothing', () => { |
| 90 | + const document = { |
| 91 | + properties: { |
| 92 | + user: { |
| 93 | + $ref: '#/definitions/User', |
| 94 | + }, |
| 95 | + }, |
| 96 | + definitions: {}, |
| 97 | + }; |
| 98 | + |
| 99 | + reparentBundleTarget(document, '#/components/schemas', '#/$defs'); |
| 100 | + |
| 101 | + expect(document).toStrictEqual({ |
| 102 | + properties: { |
| 103 | + user: { |
| 104 | + $ref: '#/definitions/User', |
| 105 | + }, |
| 106 | + }, |
| 107 | + definitions: {}, |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('given invalid source, should do nothing', () => { |
| 112 | + let document: Record<string, unknown> = { |
| 113 | + properties: { |
| 114 | + user: { |
| 115 | + $ref: '#/definitions/User', |
| 116 | + }, |
| 117 | + }, |
| 118 | + components: null, |
| 119 | + }; |
| 120 | + |
| 121 | + reparentBundleTarget(document, '#/components/schemas', '#/$defs'); |
| 122 | + |
| 123 | + expect(document).toStrictEqual({ |
| 124 | + properties: { |
| 125 | + user: { |
| 126 | + $ref: '#/definitions/User', |
| 127 | + }, |
| 128 | + }, |
| 129 | + components: null, |
| 130 | + }); |
| 131 | + |
| 132 | + document = { |
| 133 | + properties: { |
| 134 | + user: { |
| 135 | + $ref: '#/definitions/User', |
| 136 | + }, |
| 137 | + }, |
| 138 | + components: { |
| 139 | + schemas: null, |
| 140 | + }, |
| 141 | + }; |
| 142 | + |
| 143 | + reparentBundleTarget(document, '#/components/schemas', '#/$defs'); |
| 144 | + |
| 145 | + expect(document).toStrictEqual({ |
| 146 | + properties: { |
| 147 | + user: { |
| 148 | + $ref: '#/definitions/User', |
| 149 | + }, |
| 150 | + }, |
| 151 | + components: { |
| 152 | + schemas: null, |
| 153 | + }, |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + it('given existing target, should do nothing', () => { |
| 158 | + const document = { |
| 159 | + properties: { |
| 160 | + user: { |
| 161 | + $ref: '#/definitions/User', |
| 162 | + }, |
| 163 | + }, |
| 164 | + $defs: {}, |
| 165 | + definitions: { |
| 166 | + Name: { |
| 167 | + type: 'string', |
| 168 | + }, |
| 169 | + Admin: { |
| 170 | + properties: { |
| 171 | + name: { |
| 172 | + $ref: '#/definitions/Name', |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + }; |
| 178 | + |
| 179 | + reparentBundleTarget(document, '#/definitions', '#/$defs'); |
| 180 | + |
| 181 | + expect(document).toStrictEqual({ |
| 182 | + properties: { |
| 183 | + user: { |
| 184 | + $ref: '#/definitions/User', |
| 185 | + }, |
| 186 | + }, |
| 187 | + $defs: {}, |
| 188 | + definitions: { |
| 189 | + Name: { |
| 190 | + type: 'string', |
| 191 | + }, |
| 192 | + Admin: { |
| 193 | + properties: { |
| 194 | + name: { |
| 195 | + $ref: '#/definitions/Name', |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + }, |
| 200 | + }); |
| 201 | + }); |
| 202 | +}); |
0 commit comments