import { readFileSync } from 'node:fs'; import Ajv from 'ajv'; import glob from 'fast-glob'; import { beforeEach, describe, expect, it, test } from 'vitest'; import themeSchema from '../../schemas/json/theme.json'; describe( 'theme.json schema', () => { const jsonFiles = glob.sync( [ 'packages/*/src/**/theme.json', '{lib,phpunit,test}/**/theme.json' ], { onlyFiles: true, ignore: [ '**/node_modules/**' ] } ); const invalidFiles = glob.sync( [ 'test/integration/fixtures/schemas/*.json' ], { onlyFiles: true } ); let ajv; beforeEach( () => { ajv = new Ajv( { // Used for matching unknown blocks without repeating core blocks names // with patternProperties in settings.blocks and settings.styles allowMatchingProperties: true, } ); } ); it( 'strictly adheres to the draft-07 meta schema', () => { // Use ajv.compile instead of ajv.validateSchema to validate the schema // because validateSchema only checks syntax, whereas, compile checks // if the schema is semantically correct with strict mode. // See https://github.com/ajv-validator/ajv/issues/1434#issuecomment-822982571 const result = ajv.compile( themeSchema ); expect( result.errors ).toBe( null ); } ); test( 'found theme.json files', () => { expect( jsonFiles.length ).toBeGreaterThan( 0 ); } ); test( 'accepts supported text input pseudo-selectors', () => { const theme = { version: 3, styles: { elements: { textInput: { ':focus': { color: { text: 'blue' }, }, ':user-invalid': { spacing: { padding: { top: '1rem' } }, }, '::placeholder': { typography: { fontStyle: 'italic' }, }, }, }, }, }; expect( ajv.validate( themeSchema, theme ) ).toBe( true ); } ); test( 'rejects unsupported text input pseudo-selectors', () => { const theme = { version: 3, styles: { elements: { textInput: { ':hover': { color: { text: 'green' }, }, }, }, }, }; expect( ajv.validate( themeSchema, theme ) ).toBe( false ); } ); test( 'accepts separate row and column block gaps in block styles', () => { const theme = { version: 3, styles: { blocks: { 'core/group': { spacing: { blockGap: { top: '1.3em', left: '0.75em' } }, }, }, }, }; expect( ajv.validate( themeSchema, theme ) ).toBe( true ); } ); test( 'rejects separate row and column block gaps in root styles', () => { const theme = { version: 3, styles: { spacing: { blockGap: { top: '1.3em', left: '0.75em' } }, }, }; expect( ajv.validate( themeSchema, theme ) ).toBe( false ); } ); test( 'rejects separate row and column block gaps in element styles', () => { const theme = { version: 3, styles: { elements: { button: { spacing: { blockGap: { top: '1.3em', left: '0.75em' } }, }, }, }, }; expect( ajv.validate( themeSchema, theme ) ).toBe( false ); } ); test.each( jsonFiles )( 'validates schema for `%s`', ( filepath ) => { // We want to validate the theme.json file using the local schema. const { $schema, ...metadata } = JSON.parse( readFileSync( filepath, 'utf8' ) ); // we expect the $schema property to be present in the theme.json file expect( $schema ).toBeTruthy(); const result = ajv.validate( themeSchema, metadata ) || ajv.errors; expect( result ).toBe( true ); } ); test( 'accepts supported select pseudo-selectors', () => { const theme = { version: 3, styles: { elements: { select: { ':hover': { color: { background: 'blue' }, }, ':focus-visible': { outline: { style: 'solid' }, }, }, }, }, }; expect( ajv.validate( themeSchema, theme ) ).toBe( true ); } ); test( 'rejects unsupported select pseudo-selectors', () => { const theme = { version: 3, styles: { elements: { select: { ':visited': { color: { text: 'green' }, }, }, }, }, }; expect( ajv.validate( themeSchema, theme ) ).toBe( false ); } ); test.each( invalidFiles )( 'invalidates schema for `%s`', ( filepath ) => { // We want to validate the theme.json file using the local schema. const { $schema, ...metadata } = JSON.parse( readFileSync( filepath, 'utf8' ) ); const result = ajv.validate( themeSchema, metadata ); expect( result ).toBe( false ); } ); } );