Skip to content

Commit 597edbb

Browse files
committed
beef up test case
1 parent 3f0032d commit 597edbb

4 files changed

Lines changed: 170 additions & 7 deletions

File tree

schemas/theme/setting.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,10 @@
357357
"type": "object",
358358
"minProperties": 1,
359359
"maxProperties": 20,
360-
"propertyNames": { "pattern": "^[a-zA-Z][\\w-]*$" },
360+
"propertyNames": { "pattern": "^[a-zA-Z]\\w*$" },
361361
"additionalProperties": { "type": "string" },
362-
"description": "A map of palette color names to color values. Each key is a slot name (you choose, e.g. \"background\", \"foreground\"), and each value is a CSS color (typically a hex code).",
363-
"markdownDescription": "A map of palette color names to color values.\n\nEach **key** is a slot name (you choose them — e.g. `background`, `foreground`, `accent`). Each **value** is a CSS color, typically a hex code.\n\n**Example:**\n```json\n\"default\": {\n \"background\": \"#ffffff\",\n \"foreground\": \"#000000\"\n}\n```\n\n---\n\n[Shopify reference](https://shopify.dev/docs/themes/architecture/settings/input-settings#color_palette)"
362+
"description": "A map of palette color names to color values. Each key is an arbitrary name and each value is a CSS color.",
363+
"markdownDescription": "A map of palette color names to color values.\n\nEach **key** is an arbitrary name. Each **value** is a CSS color, typically a hex code.\n\n**Example:**\n```json\n\"default\": {\n \"background\": \"#ffffff\",\n \"foreground\": \"#000000\"\n}\n```\n\n---\n\n[Shopify reference](https://shopify.dev/docs/themes/architecture/settings/input-settings#color_palette)"
364364
}
365365
},
366366
"additionalProperties": false

tests/section.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import set from 'lodash.set';
22
import { assert, describe, expect, it } from 'vitest';
3-
import { INPUT_SETTING_TYPES, SETTINGS_TYPES_NOT_SUPPORTING_VISIBLE_IF } from './test-constants';
3+
import {
4+
INPUT_SETTING_TYPES,
5+
SETTINGS_TYPES_EXCLUSIVE_TO_SETTINGS_SCHEMA,
6+
SETTINGS_TYPES_NOT_SUPPORTING_VISIBLE_IF,
7+
} from './test-constants';
48
import { complete, getService, hover, loadFixture, validateSchema } from './test-helpers';
59

610
const sectionSchema1 = loadFixture('section-schema-1.json');
@@ -220,7 +224,7 @@ describe('JSON Schema validation of Liquid theme section schema tags', () => {
220224

221225
const settingsTypesSupportingVisibleIf = INPUT_SETTING_TYPES.filter(
222226
(settingType) =>
223-
!SETTINGS_TYPES_NOT_SUPPORTING_VISIBLE_IF.concat('color_scheme_group').includes(settingType),
227+
!SETTINGS_TYPES_NOT_SUPPORTING_VISIBLE_IF.concat(SETTINGS_TYPES_EXCLUSIVE_TO_SETTINGS_SCHEMA).includes(settingType),
224228
);
225229

226230
it.each(settingsTypesSupportingVisibleIf)(

tests/test-constants.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export const SETTINGS_TYPES_NOT_SUPPORTING_VISIBLE_IF = [
99
'page',
1010
'product',
1111
'product_list',
12-
// Not featured here is `color_scheme_group` which is exclusive to settings_schema.json.
13-
// That setting type is tested in `color_scheme_group.spec.ts`.
12+
// Not featured here is `color_scheme_group` and `color_palette` which are exclusive to settings_schema.json.
13+
// Those setting types are tested in `color_scheme_group.spec.ts` and `color_palette.spec.ts`.
1414
];
1515

1616
export const INPUT_SETTING_TYPES = [
@@ -49,6 +49,10 @@ export const INPUT_SETTING_TYPES = [
4949
'video',
5050
];
5151

52+
// Setting types that are only valid in config/settings_schema.json,
53+
// not in section or block schemas. Tested in their own *.spec.ts files.
54+
export const SETTINGS_TYPES_EXCLUSIVE_TO_SETTINGS_SCHEMA = ['color_scheme_group', 'color_palette'];
55+
5256
export const SIDEBAR_SETTING_TYPES = ['header', 'paragraph'];
5357

5458
export const RESOURCE_LIST_SETTING_TYPES = ['article_list', 'collection_list', 'metaobject_list', 'product_list'];
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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

Comments
 (0)