|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { validateSchema } from '../test-helpers'; |
| 3 | + |
| 4 | +const validate = validateSchema(); |
| 5 | + |
| 6 | +describe('Module: theme settings validation (config/settings_schema.json)', () => { |
| 7 | + describe('Unit: color_palette', () => { |
| 8 | + const colorPaletteSetting = { |
| 9 | + type: 'color_palette', |
| 10 | + id: 'color_palette', |
| 11 | + default: { |
| 12 | + background: '#ffffff', |
| 13 | + foreground: '#000000', |
| 14 | + }, |
| 15 | + }; |
| 16 | + |
| 17 | + const settings = (override: any) => `[ |
| 18 | + { |
| 19 | + "name": "setting category", |
| 20 | + "settings": [ |
| 21 | + ${JSON.stringify(Object.assign({}, colorPaletteSetting, override), null, 2)} |
| 22 | + ] |
| 23 | + } |
| 24 | + ]`; |
| 25 | + |
| 26 | + it('accepts a valid color_palette setting', async () => { |
| 27 | + const diagnostics = await validate('config/settings_schema.json', settings({})); |
| 28 | + expect(diagnostics).toHaveLength(0); |
| 29 | + }); |
| 30 | + |
| 31 | + it('refuses the label property', async () => { |
| 32 | + const diagnostics = await validate( |
| 33 | + 'config/settings_schema.json', |
| 34 | + settings({ label: 'uh oh' }), |
| 35 | + ); |
| 36 | + expect(diagnostics).toStrictEqual([ |
| 37 | + expect.objectContaining({ |
| 38 | + message: 'Property label is not allowed.', |
| 39 | + }), |
| 40 | + ]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('refuses the info property', async () => { |
| 44 | + const diagnostics = await validate( |
| 45 | + 'config/settings_schema.json', |
| 46 | + settings({ info: 'uh oh' }), |
| 47 | + ); |
| 48 | + expect(diagnostics).toStrictEqual([ |
| 49 | + expect.objectContaining({ |
| 50 | + message: 'Property info is not allowed.', |
| 51 | + }), |
| 52 | + ]); |
| 53 | + }); |
| 54 | + |
| 55 | + it('refuses the visible_if property', async () => { |
| 56 | + const diagnostics = await validate( |
| 57 | + 'config/settings_schema.json', |
| 58 | + settings({ visible_if: '{{ section.settings.show_palette }}' }), |
| 59 | + ); |
| 60 | + expect(diagnostics).toStrictEqual([ |
| 61 | + expect.objectContaining({ |
| 62 | + message: 'Property visible_if is not allowed.', |
| 63 | + }), |
| 64 | + ]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('requires the default property', async () => { |
| 68 | + const { default: _, ...settingWithoutDefault } = colorPaletteSetting; |
| 69 | + const json = `[ |
| 70 | + { |
| 71 | + "name": "setting category", |
| 72 | + "settings": [ |
| 73 | + ${JSON.stringify(settingWithoutDefault, null, 2)} |
| 74 | + ] |
| 75 | + } |
| 76 | + ]`; |
| 77 | + |
| 78 | + const diagnostics = await validate('config/settings_schema.json', json); |
| 79 | + |
| 80 | + expect(diagnostics).toStrictEqual([ |
| 81 | + expect.objectContaining({ |
| 82 | + message: 'Missing property "default".', |
| 83 | + }), |
| 84 | + ]); |
| 85 | + }); |
| 86 | + |
| 87 | + it('requires the id property', async () => { |
| 88 | + const { id: _, ...settingWithoutId } = colorPaletteSetting; |
| 89 | + const json = `[ |
| 90 | + { |
| 91 | + "name": "setting category", |
| 92 | + "settings": [ |
| 93 | + ${JSON.stringify(settingWithoutId, null, 2)} |
| 94 | + ] |
| 95 | + } |
| 96 | + ]`; |
| 97 | + |
| 98 | + const diagnostics = await validate('config/settings_schema.json', json); |
| 99 | + |
| 100 | + expect(diagnostics).toStrictEqual([ |
| 101 | + expect.objectContaining({ |
| 102 | + message: 'Missing property "id".', |
| 103 | + }), |
| 104 | + ]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('validates that default must be an object', async () => { |
| 108 | + const diagnostics = await validate( |
| 109 | + 'config/settings_schema.json', |
| 110 | + settings({ default: '#ffffff' }), |
| 111 | + ); |
| 112 | + expect(diagnostics).toStrictEqual([ |
| 113 | + expect.objectContaining({ |
| 114 | + message: 'Incorrect type. Expected "object".', |
| 115 | + }), |
| 116 | + ]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('validates that default values must be strings', async () => { |
| 120 | + const diagnostics = await validate( |
| 121 | + 'config/settings_schema.json', |
| 122 | + settings({ default: { background: 123 } }), |
| 123 | + ); |
| 124 | + expect(diagnostics).toStrictEqual([ |
| 125 | + expect.objectContaining({ |
| 126 | + message: 'Incorrect type. Expected "string".', |
| 127 | + }), |
| 128 | + ]); |
| 129 | + }); |
| 130 | + |
| 131 | + it('refuses default keys with hyphens', async () => { |
| 132 | + const diagnostics = await validate( |
| 133 | + 'config/settings_schema.json', |
| 134 | + settings({ default: { 'primary-1': '#fff' } }), |
| 135 | + ); |
| 136 | + expect(diagnostics).toStrictEqual([ |
| 137 | + expect.objectContaining({ |
| 138 | + message: expect.stringMatching(/does not match the pattern of "\^\[a-zA-Z\]\\w\*\$"/), |
| 139 | + }), |
| 140 | + ]); |
| 141 | + }); |
| 142 | + |
| 143 | + it('refuses default keys starting with a digit', async () => { |
| 144 | + const diagnostics = await validate( |
| 145 | + 'config/settings_schema.json', |
| 146 | + settings({ default: { '1bad': '#fff' } }), |
| 147 | + ); |
| 148 | + expect(diagnostics).toStrictEqual([ |
| 149 | + expect.objectContaining({ |
| 150 | + message: expect.stringMatching(/does not match the pattern of "\^\[a-zA-Z\]\\w\*\$"/), |
| 151 | + }), |
| 152 | + ]); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments