-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Theme check for settings keys inside presets and default (#742)
* Move `block-utils` to common `utils` folder * Add `ValidSettingsKey` check for schema
- Loading branch information
Showing
10 changed files
with
474 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@shopify/theme-check-common': minor | ||
'@shopify/theme-check-node': minor | ||
--- | ||
|
||
Theme check verifies if setting key exists within block schemas and section schemas | ||
|
||
- Check if the keys inside `presets.[].settings` and `default.settings` exist as `settings.[].id` in the same file | ||
- Check if the keys inside `presets.[](recursive .blocks.[]).settings` and `default.blocks.[].settings` exist as `settings.[].id` inside the referenced block's file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
packages/theme-check-common/src/checks/valid-block-target/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
321 changes: 321 additions & 0 deletions
321
packages/theme-check-common/src/checks/valid-settings-key/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,321 @@ | ||
import { expect, describe, it } from 'vitest'; | ||
import { ValidSettingsKey } from './index'; | ||
import { check } from '../../test'; | ||
|
||
describe('Moduel: ValidSettingsKey', () => { | ||
const schemaTemplate = { | ||
name: 'Example', | ||
settings: [ | ||
{ | ||
id: 'existent-setting', | ||
type: 'text', | ||
label: 'Example Text Setting', | ||
}, | ||
], | ||
}; | ||
|
||
describe('default settings', () => { | ||
it('does not report an error when default setting exists', async () => { | ||
const theme = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
default: { | ||
settings: { | ||
'existent-setting': 'value', | ||
}, | ||
}, | ||
}), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it('reports an error when default setting does not exist', async () => { | ||
const theme = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
default: { | ||
settings: { | ||
'non-existent-setting': 'value', | ||
}, | ||
}, | ||
}), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(1); | ||
expect(offenses[0].message).to.equal( | ||
`Setting 'non-existent-setting' does not exist in schema.`, | ||
); | ||
}); | ||
|
||
it('does not report an error when missing default settings is referenced from a block schema', async () => { | ||
const theme = { | ||
'blocks/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
default: { | ||
settings: { | ||
'non-existent-setting': 'value', | ||
}, | ||
}, | ||
}), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
}); | ||
|
||
describe('presets settings', () => { | ||
it('does not report an error when presets setting exists', async () => { | ||
const theme = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
presets: [ | ||
{ | ||
settings: { | ||
'existent-setting': 'value', | ||
}, | ||
}, | ||
], | ||
}), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it('reports an error when presets setting does not exist', async () => { | ||
const theme = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
presets: [ | ||
{ | ||
settings: { | ||
'non-existent-setting-1': 'value', | ||
}, | ||
}, | ||
{ | ||
settings: { | ||
'non-existent-setting-2': 'value', | ||
}, | ||
}, | ||
], | ||
}), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(2); | ||
expect(offenses[0].message).to.equal( | ||
`Setting 'non-existent-setting-1' does not exist in schema.`, | ||
); | ||
expect(offenses[1].message).to.equal( | ||
`Setting 'non-existent-setting-2' does not exist in schema.`, | ||
); | ||
}); | ||
}); | ||
|
||
const tests = [ | ||
{ | ||
label: 'default', | ||
blockTemplate: (blocks: any[]) => { | ||
return { | ||
default: { | ||
blocks, | ||
}, | ||
}; | ||
}, | ||
}, | ||
{ | ||
label: 'presets', | ||
blockTemplate: (blocks: any[]) => { | ||
return { | ||
presets: [ | ||
{ | ||
blocks, | ||
}, | ||
], | ||
}; | ||
}, | ||
}, | ||
]; | ||
|
||
tests.forEach(({ label, blockTemplate }) => { | ||
describe(`${label} block settings`, () => { | ||
describe('referenced blocks', () => { | ||
const referencedBlock = { | ||
'blocks/referenced.liquid': toLiquidFile(schemaTemplate), | ||
}; | ||
|
||
it(`does not report an error when ${label} block setting exists in referenced file`, async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'referenced', | ||
settings: { | ||
'existent-setting': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check( | ||
{ | ||
...referencedBlock, | ||
...fileToTest, | ||
}, | ||
[ValidSettingsKey], | ||
); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it('does not report an error when referenced file does not exist', async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'non-existent-file', | ||
settings: { | ||
'non-existent-setting': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check( | ||
{ | ||
...referencedBlock, | ||
...fileToTest, | ||
}, | ||
[ValidSettingsKey], | ||
); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it(`reports an error when ${label} block setting does not exist in referenced file`, async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...schemaTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'referenced', | ||
settings: { | ||
'non-existent-setting': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check( | ||
{ | ||
...referencedBlock, | ||
...fileToTest, | ||
}, | ||
[ValidSettingsKey], | ||
); | ||
expect(offenses).to.have.length(1); | ||
expect(offenses[0].message).to.equal( | ||
`Setting 'non-existent-setting' does not exist in 'blocks/referenced.liquid'.`, | ||
); | ||
}); | ||
}); | ||
|
||
describe('local blocks', () => { | ||
const localBlocksTemplate = { | ||
blocks: [ | ||
{ | ||
type: 'local-block', | ||
name: 'Local block', | ||
settings: [ | ||
{ | ||
id: 'local-setting', | ||
type: 'text', | ||
label: 'Local Setting', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
it(`reports an error when ${label} block setting does not exist in existing local block`, async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...localBlocksTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'local-block', | ||
settings: { | ||
'non-existent-setting-1': 'value', | ||
'non-existent-setting-2': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check(fileToTest, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(2); | ||
expect(offenses[0].message).to.equal( | ||
`Setting 'non-existent-setting-1' does not exist in schema.`, | ||
); | ||
expect(offenses[1].message).to.equal( | ||
`Setting 'non-existent-setting-2' does not exist in schema.`, | ||
); | ||
}); | ||
|
||
it(`does not report an error when ${label} block setting does not exist in non-existent local block`, async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...localBlocksTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'non-existent-local-block', | ||
settings: { | ||
'non-existent-setting': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check(fileToTest, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it(`does not report an error when ${label} block setting exists in local block`, async () => { | ||
const fileToTest = { | ||
'sections/example.liquid': toLiquidFile({ | ||
...localBlocksTemplate, | ||
...blockTemplate([ | ||
{ | ||
type: 'local-block', | ||
settings: { | ||
'local-setting': 'value', | ||
}, | ||
}, | ||
]), | ||
}), | ||
}; | ||
|
||
const offenses = await check(fileToTest, [ValidSettingsKey]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
function toLiquidFile(content: any) { | ||
return ` | ||
{% schema %} | ||
${JSON.stringify(content)} | ||
{% endschema %} | ||
`; | ||
} |
Oops, something went wrong.