|
| 1 | +import {describe, expect, test} from 'vitest' |
| 2 | +import {validateStudio} from '../../../src/index.js' |
| 3 | + |
| 4 | +const validStudio = { |
| 5 | + name: 'my-studio', |
| 6 | + type: 'sanity.studio', |
| 7 | + src: './studio', |
| 8 | + autoUpdates: {enabled: true}, |
| 9 | +} |
| 10 | + |
| 11 | +describe('validateStudio', () => { |
| 12 | + test('should return an error if config is falsey', () => { |
| 13 | + const errors = validateStudio(undefined) |
| 14 | + expect(errors).toContainEqual({type: 'invalid_value', message: 'Studio config must be provided'}) |
| 15 | + }) |
| 16 | + |
| 17 | + test('should return an error if config is not an object', () => { |
| 18 | + const errors = validateStudio(1) |
| 19 | + expect(errors).toContainEqual({type: 'invalid_type', message: 'Studio config must be an object'}) |
| 20 | + }) |
| 21 | + |
| 22 | + test('should return an error if type is not sanity.studio', () => { |
| 23 | + const errors = validateStudio({...validStudio, type: 'invalid'}) |
| 24 | + expect(errors).toContainEqual({type: 'invalid_value', message: 'Studio type must be `sanity.studio`'}) |
| 25 | + }) |
| 26 | + |
| 27 | + test('should return an error if src is not provided', () => { |
| 28 | + const {src: _src, ...noSrc} = validStudio |
| 29 | + const errors = validateStudio(noSrc) |
| 30 | + expect(errors).toContainEqual({type: 'missing_parameter', message: 'Studio src is required'}) |
| 31 | + }) |
| 32 | + |
| 33 | + test('should return an error if src is not a string', () => { |
| 34 | + const errors = validateStudio({...validStudio, src: 1}) |
| 35 | + expect(errors).toContainEqual({type: 'invalid_type', message: 'Studio src must be a string'}) |
| 36 | + }) |
| 37 | + |
| 38 | + test('should return an error if autoUpdates is not provided', () => { |
| 39 | + const {autoUpdates: _au, ...noAutoUpdates} = validStudio |
| 40 | + const errors = validateStudio(noAutoUpdates) |
| 41 | + expect(errors).toContainEqual({type: 'missing_parameter', message: 'Studio autoUpdates is required'}) |
| 42 | + }) |
| 43 | + |
| 44 | + test('should return an error if autoUpdates is not an object', () => { |
| 45 | + const errors = validateStudio({...validStudio, autoUpdates: 'yes'}) |
| 46 | + expect(errors).toContainEqual({type: 'invalid_type', message: 'Studio autoUpdates must be an object'}) |
| 47 | + }) |
| 48 | + |
| 49 | + test('should return an error if autoUpdates.enabled is not provided', () => { |
| 50 | + const errors = validateStudio({...validStudio, autoUpdates: {}}) |
| 51 | + expect(errors).toContainEqual({type: 'missing_parameter', message: 'Studio autoUpdates.enabled is required'}) |
| 52 | + }) |
| 53 | + |
| 54 | + test('should return an error if autoUpdates.enabled is not a boolean', () => { |
| 55 | + const errors = validateStudio({...validStudio, autoUpdates: {enabled: 'yes'}}) |
| 56 | + expect(errors).toContainEqual({type: 'invalid_type', message: 'Studio autoUpdates.enabled must be a boolean'}) |
| 57 | + }) |
| 58 | + |
| 59 | + test('should return an error if autoUpdates.version is not a string', () => { |
| 60 | + const errors = validateStudio({...validStudio, autoUpdates: {enabled: true, version: 1}}) |
| 61 | + expect(errors).toContainEqual({type: 'invalid_type', message: 'Studio autoUpdates.version must be a string'}) |
| 62 | + }) |
| 63 | + |
| 64 | + test('should return an error if basePath is not a string', () => { |
| 65 | + const errors = validateStudio({...validStudio, basePath: 1}) |
| 66 | + expect(errors).toContainEqual({type: 'invalid_type', message: 'Studio basePath must be a string'}) |
| 67 | + }) |
| 68 | + |
| 69 | + test('should return an error if minify is not a boolean', () => { |
| 70 | + const errors = validateStudio({...validStudio, minify: 'yes'}) |
| 71 | + expect(errors).toContainEqual({type: 'invalid_type', message: 'Studio minify must be a boolean'}) |
| 72 | + }) |
| 73 | + |
| 74 | + test('should return an error if reactCompiler is not a boolean', () => { |
| 75 | + const errors = validateStudio({...validStudio, reactCompiler: 'yes'}) |
| 76 | + expect(errors).toContainEqual({type: 'invalid_type', message: 'Studio reactCompiler must be a boolean'}) |
| 77 | + }) |
| 78 | + |
| 79 | + test('should return an error if sourceMap is not a boolean', () => { |
| 80 | + const errors = validateStudio({...validStudio, sourceMap: 'yes'}) |
| 81 | + expect(errors).toContainEqual({type: 'invalid_type', message: 'Studio sourceMap must be a boolean'}) |
| 82 | + }) |
| 83 | + |
| 84 | + test('should return an error if project is not a string', () => { |
| 85 | + const errors = validateStudio({...validStudio, project: 1}) |
| 86 | + expect(errors).toContainEqual({type: 'invalid_type', message: 'Studio project must be a string'}) |
| 87 | + }) |
| 88 | + |
| 89 | + test('should accept a valid configuration', () => { |
| 90 | + const errors = validateStudio({ |
| 91 | + ...validStudio, |
| 92 | + autoUpdates: {enabled: true, version: '^3.0.0'}, |
| 93 | + basePath: '/studio', |
| 94 | + minify: true, |
| 95 | + reactCompiler: false, |
| 96 | + sourceMap: true, |
| 97 | + project: 'abcdefg', |
| 98 | + }) |
| 99 | + expect(errors, JSON.stringify(errors)).toHaveLength(0) |
| 100 | + }) |
| 101 | +}) |
0 commit comments