|
| 1 | +import assert from 'node:assert' |
| 2 | + |
| 3 | +import {describe, expect, test} from '@jest/globals' |
| 4 | +import {defineType} from '@sanity/types' |
| 5 | + |
| 6 | +import {Schema} from '../../src/legacy/Schema' |
| 7 | +import {extractSchema} from '../../src/sanity/extractSchema' |
| 8 | +import {groupProblems} from '../../src/sanity/groupProblems' |
| 9 | +import {validateSchema} from '../../src/sanity/validateSchema' |
| 10 | +import schemaFixtures from '../legacy/fixtures/schemas' |
| 11 | +// built-in types |
| 12 | +import assetSourceData from './fixtures/assetSourceData' |
| 13 | +import Block from './fixtures/block' |
| 14 | +import fileAsset from './fixtures/fileAsset' |
| 15 | +import geopoint from './fixtures/geopoint' |
| 16 | +import imageAsset from './fixtures/imageAsset' |
| 17 | +import imageCrop from './fixtures/imageCrop' |
| 18 | +import imageDimensions from './fixtures/imageDimensions' |
| 19 | +import imageHotspot from './fixtures/imageHotspot' |
| 20 | +import imageMetadata from './fixtures/imageMetadata' |
| 21 | +import imagePalette from './fixtures/imagePalette' |
| 22 | +import imagePaletteSwatch from './fixtures/imagePaletteSwatch' |
| 23 | +import slug from './fixtures/slug' |
| 24 | + |
| 25 | +const builtinTypes = [ |
| 26 | + assetSourceData, |
| 27 | + slug, |
| 28 | + geopoint, |
| 29 | + imageAsset, |
| 30 | + fileAsset, |
| 31 | + imageCrop, |
| 32 | + imageHotspot, |
| 33 | + imageMetadata, |
| 34 | + imageDimensions, |
| 35 | + imagePalette, |
| 36 | + imagePaletteSwatch, |
| 37 | +] |
| 38 | + |
| 39 | +// taken from sanity/src/core/schema/createSchema.ts |
| 40 | +function createSchema(schemaDef: {name: string; types: any[]}) { |
| 41 | + const validated = validateSchema(schemaDef.types).getTypes() |
| 42 | + const validation = groupProblems(validated) |
| 43 | + const hasErrors = validation.some((group) => |
| 44 | + group.problems.some((problem) => problem.severity === 'error'), |
| 45 | + ) |
| 46 | + |
| 47 | + return Schema.compile({ |
| 48 | + name: 'test', |
| 49 | + types: hasErrors ? [] : [...schemaDef.types, ...builtinTypes].filter(Boolean), |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +describe('Extract schema test', () => { |
| 54 | + test('Extracts schema general', () => { |
| 55 | + const schema = createSchema({ |
| 56 | + name: 'test', |
| 57 | + types: [ |
| 58 | + defineType({ |
| 59 | + title: 'Valid document', |
| 60 | + name: 'validDocument', |
| 61 | + type: 'document', |
| 62 | + fields: [ |
| 63 | + { |
| 64 | + title: 'Title', |
| 65 | + name: 'title', |
| 66 | + type: 'string', |
| 67 | + }, |
| 68 | + { |
| 69 | + title: 'List', |
| 70 | + name: 'list', |
| 71 | + type: 'string', |
| 72 | + options: { |
| 73 | + list: ['a', 'b', 'c'], |
| 74 | + }, |
| 75 | + validation: (Rule) => Rule.required(), |
| 76 | + }, |
| 77 | + { |
| 78 | + title: 'Number', |
| 79 | + name: 'number', |
| 80 | + type: 'number', |
| 81 | + }, |
| 82 | + { |
| 83 | + title: 'some other object', |
| 84 | + name: 'someInlinedObject', |
| 85 | + type: 'obj', |
| 86 | + }, |
| 87 | + { |
| 88 | + title: 'Manuscript', |
| 89 | + name: 'manuscript', |
| 90 | + type: 'manuscript', |
| 91 | + }, |
| 92 | + { |
| 93 | + title: 'customStringType', |
| 94 | + name: 'customStringType', |
| 95 | + type: 'customStringType', |
| 96 | + }, |
| 97 | + { |
| 98 | + title: 'Blocks', |
| 99 | + name: 'blocks', |
| 100 | + type: 'array', |
| 101 | + of: [{type: 'block'}], |
| 102 | + }, |
| 103 | + { |
| 104 | + type: 'reference', |
| 105 | + name: 'other', |
| 106 | + to: { |
| 107 | + type: 'otherValidDocument', |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + type: 'reference', |
| 112 | + name: 'others', |
| 113 | + to: [ |
| 114 | + { |
| 115 | + type: 'otherValidDocument', |
| 116 | + }, |
| 117 | + ], |
| 118 | + }, |
| 119 | + ], |
| 120 | + }), |
| 121 | + { |
| 122 | + title: 'Author', |
| 123 | + name: 'author', |
| 124 | + type: 'document', |
| 125 | + fields: [ |
| 126 | + { |
| 127 | + title: 'Name', |
| 128 | + name: 'name', |
| 129 | + type: 'string', |
| 130 | + }, |
| 131 | + { |
| 132 | + title: 'Profile picture', |
| 133 | + name: 'profilePicture', |
| 134 | + type: 'image', |
| 135 | + options: { |
| 136 | + hotspot: true, |
| 137 | + }, |
| 138 | + fields: [ |
| 139 | + { |
| 140 | + name: 'caption', |
| 141 | + type: 'string', |
| 142 | + title: 'Caption', |
| 143 | + }, |
| 144 | + { |
| 145 | + name: 'attribution', |
| 146 | + type: 'string', |
| 147 | + title: 'Attribution', |
| 148 | + }, |
| 149 | + ], |
| 150 | + }, |
| 151 | + ], |
| 152 | + }, |
| 153 | + { |
| 154 | + title: 'Book', |
| 155 | + name: 'book', |
| 156 | + type: 'document', |
| 157 | + fields: [ |
| 158 | + { |
| 159 | + title: 'Name', |
| 160 | + name: 'name', |
| 161 | + type: 'string', |
| 162 | + }, |
| 163 | + ], |
| 164 | + }, |
| 165 | + Block, |
| 166 | + { |
| 167 | + title: 'Other valid document', |
| 168 | + name: 'otherValidDocument', |
| 169 | + type: 'document', |
| 170 | + fields: [ |
| 171 | + { |
| 172 | + title: 'Title', |
| 173 | + name: 'title', |
| 174 | + type: 'string', |
| 175 | + }, |
| 176 | + ], |
| 177 | + }, |
| 178 | + { |
| 179 | + type: 'object', |
| 180 | + name: 'obj', |
| 181 | + fields: [ |
| 182 | + { |
| 183 | + title: 'Field #1', |
| 184 | + name: 'field1', |
| 185 | + type: 'string', |
| 186 | + }, |
| 187 | + { |
| 188 | + title: 'Field #2', |
| 189 | + name: 'field2', |
| 190 | + type: 'number', |
| 191 | + }, |
| 192 | + ], |
| 193 | + }, |
| 194 | + defineType({ |
| 195 | + name: 'customStringType', |
| 196 | + title: 'My custom string type', |
| 197 | + type: 'string', |
| 198 | + }), |
| 199 | + { |
| 200 | + type: 'object', |
| 201 | + name: 'code', |
| 202 | + fields: [ |
| 203 | + { |
| 204 | + title: 'The Code!', |
| 205 | + name: 'thecode', |
| 206 | + type: 'string', |
| 207 | + }, |
| 208 | + ], |
| 209 | + }, |
| 210 | + { |
| 211 | + title: 'Manuscript', |
| 212 | + name: 'manuscript', |
| 213 | + type: 'file', |
| 214 | + fields: [ |
| 215 | + { |
| 216 | + name: 'description', |
| 217 | + type: 'string', |
| 218 | + title: 'Description', |
| 219 | + }, |
| 220 | + { |
| 221 | + name: 'author', |
| 222 | + type: 'reference', |
| 223 | + title: 'Author', |
| 224 | + to: {type: 'author'}, |
| 225 | + }, |
| 226 | + ], |
| 227 | + }, |
| 228 | + ], |
| 229 | + }) |
| 230 | + |
| 231 | + const extracted = extractSchema(schema) |
| 232 | + expect(extracted.map((v) => v.name)).toStrictEqual([ |
| 233 | + 'sanity.imagePaletteSwatch', |
| 234 | + 'sanity.imagePalette', |
| 235 | + 'sanity.imageDimensions', |
| 236 | + 'geopoint', |
| 237 | + 'slug', |
| 238 | + 'sanity.fileAsset', |
| 239 | + 'code', |
| 240 | + 'customStringType', |
| 241 | + 'blocksTest', |
| 242 | + 'book', |
| 243 | + 'author', |
| 244 | + 'sanity.imageCrop', |
| 245 | + 'sanity.imageHotspot', |
| 246 | + 'sanity.imageAsset', |
| 247 | + 'sanity.assetSourceData', |
| 248 | + 'sanity.imageMetadata', |
| 249 | + 'validDocument', |
| 250 | + 'otherValidDocument', |
| 251 | + 'manuscript', |
| 252 | + 'obj', |
| 253 | + ]) |
| 254 | + const validDocument = extracted.find((type) => type.name === 'validDocument') |
| 255 | + expect(validDocument).toBeDefined() |
| 256 | + assert(validDocument !== undefined) // this is a workaround for TS, but leave the expect above for clarity in case of failure |
| 257 | + |
| 258 | + expect(validDocument.name).toEqual('validDocument') |
| 259 | + expect(validDocument.type).toEqual('document') |
| 260 | + assert(validDocument.type === 'document') // this is a workaround for TS https://github.com/DefinitelyTyped/DefinitelyTyped/issues/41179 |
| 261 | + expect(Object.keys(validDocument.attributes)).toStrictEqual([ |
| 262 | + '_id', |
| 263 | + '_type', |
| 264 | + '_createdAt', |
| 265 | + '_updatedAt', |
| 266 | + '_rev', |
| 267 | + 'title', |
| 268 | + 'list', |
| 269 | + 'number', |
| 270 | + 'someInlinedObject', |
| 271 | + 'manuscript', |
| 272 | + 'customStringType', |
| 273 | + 'blocks', |
| 274 | + 'other', |
| 275 | + 'others', |
| 276 | + ]) |
| 277 | + |
| 278 | + // Check that the block type is extracted correctly, as an array |
| 279 | + expect(validDocument.attributes.blocks.type).toEqual('objectAttribute') |
| 280 | + expect(validDocument.attributes.blocks.value.type).toEqual('array') |
| 281 | + assert(validDocument.attributes.blocks.value.type === 'array') // this is a workaround for TS |
| 282 | + expect(validDocument.attributes.blocks.value.of.type).toEqual('object') |
| 283 | + assert(validDocument.attributes.blocks.value.of.type === 'object') // this is a workaround for TS |
| 284 | + expect(Object.keys(validDocument.attributes.blocks.value.of.attributes)).toStrictEqual([ |
| 285 | + 'children', |
| 286 | + 'style', |
| 287 | + 'listItem', |
| 288 | + 'markDefs', |
| 289 | + 'level', |
| 290 | + '_type', |
| 291 | + ]) |
| 292 | + |
| 293 | + expect(validDocument.attributes.blocks.value.of.attributes.children.value.type).toEqual('array') |
| 294 | + assert(validDocument.attributes.blocks.value.of.attributes.children.value.type === 'array') // this is a workaround for TS |
| 295 | + expect(validDocument.attributes.blocks.value.of.attributes.children.value.of.type).toEqual( |
| 296 | + 'object', |
| 297 | + ) |
| 298 | + assert(validDocument.attributes.blocks.value.of.attributes.children.value.of.type === 'object') // this is a workaround for TS |
| 299 | + expect( |
| 300 | + Object.keys(validDocument.attributes.blocks.value.of.attributes.children.value.of.attributes), |
| 301 | + ).toStrictEqual(['marks', 'text', '_type']) |
| 302 | + |
| 303 | + expect(extracted).toMatchSnapshot() |
| 304 | + }) |
| 305 | + |
| 306 | + test('order of types does not matter', () => { |
| 307 | + const schema1 = createSchema({ |
| 308 | + name: 'test', |
| 309 | + types: [ |
| 310 | + { |
| 311 | + title: 'Author', |
| 312 | + name: 'author', |
| 313 | + type: 'object', |
| 314 | + fields: [ |
| 315 | + { |
| 316 | + title: 'Name', |
| 317 | + name: 'name', |
| 318 | + type: 'string', |
| 319 | + }, |
| 320 | + ], |
| 321 | + }, |
| 322 | + { |
| 323 | + title: 'Book', |
| 324 | + name: 'book', |
| 325 | + type: 'document', |
| 326 | + fields: [ |
| 327 | + { |
| 328 | + title: 'Name', |
| 329 | + name: 'name', |
| 330 | + type: 'string', |
| 331 | + }, |
| 332 | + { |
| 333 | + title: 'Author', |
| 334 | + name: 'author', |
| 335 | + type: 'author', |
| 336 | + }, |
| 337 | + ], |
| 338 | + }, |
| 339 | + ], |
| 340 | + }) |
| 341 | + |
| 342 | + expect(extractSchema(schema1).map((v) => v.name)).toStrictEqual([ |
| 343 | + 'sanity.imagePaletteSwatch', |
| 344 | + 'sanity.imagePalette', |
| 345 | + 'sanity.imageDimensions', |
| 346 | + 'sanity.imageHotspot', |
| 347 | + 'sanity.imageCrop', |
| 348 | + 'sanity.fileAsset', |
| 349 | + 'sanity.imageAsset', |
| 350 | + 'sanity.imageMetadata', |
| 351 | + 'geopoint', |
| 352 | + 'slug', |
| 353 | + 'sanity.assetSourceData', |
| 354 | + 'book', |
| 355 | + 'author', |
| 356 | + ]) |
| 357 | + }) |
| 358 | + |
| 359 | + describe('Can extract sample fixtures', () => { |
| 360 | + const cases = Object.keys(schemaFixtures).map((schemaName) => { |
| 361 | + const schema = createSchema(schemaFixtures[schemaName]) |
| 362 | + if (schema._original.types.length === 0) { |
| 363 | + return {schemaName, schema: null} |
| 364 | + } |
| 365 | + return {schemaName, schema} |
| 366 | + }) |
| 367 | + const passes = cases.filter((v): v is {schemaName: string; schema: Schema} => v.schema !== null) |
| 368 | + |
| 369 | + test.each(passes)('extracts schema $schemaName', ({schema}) => { |
| 370 | + const extracted = extractSchema(schema) |
| 371 | + expect(extracted.length).toBeGreaterThan(0) // we don't really care about the exact number, just that it passes :+1: |
| 372 | + }) |
| 373 | + |
| 374 | + const skips = cases.filter((v): v is {schemaName: string; schema: null} => v.schema === null) |
| 375 | + test.skip.each(skips)('extracts schema $schemaName', () => { |
| 376 | + // Add a test for the skipped cases so we can track them in the test report |
| 377 | + }) |
| 378 | + }) |
| 379 | +}) |
0 commit comments