-
Notifications
You must be signed in to change notification settings - Fork 47
[Theme Checks] Added in a check for theme block settings validation #620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb87775
Added in a check for theme block settings validation
AribaRajput 625d79e
theme block theme check
AribaRajput 6f90bcc
Reworked
AribaRajput a262ff6
Added in default setting validation
AribaRajput 299e675
Reformatted tests to have clearer structure
AribaRajput b7a7bd6
Refactored theme check
AribaRajput File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
--- | ||
'@shopify/theme-check-common': minor | ||
'theme-check-vscode': minor | ||
--- | ||
|
||
Add the `ValidPresetAndDefaultSettings` check. This check will allow us to validate that the settings defined in sections and blocks are valid. They are valid if they exist in either the block settings or the section settings. |
This file contains hidden or 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
303 changes: 303 additions & 0 deletions
303
packages/theme-check-common/src/checks/valid-preset-and-default-settings/index.spec.ts
This file contains hidden or 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,303 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { check } from '../../test/test-helper'; | ||
import { MockTheme } from '../../test/MockTheme'; | ||
import { | ||
horizonNestedBlockSchema, | ||
createHorizonBlockSchema, | ||
createDawnSectionSchema, | ||
validDawnDefaultSchema, | ||
validDawnPresetSchema, | ||
validHorizonPresetSchema, | ||
validHorizonDefaultSchema, | ||
} from './valid-schema-utils'; | ||
import { ValidPresetAndDefaultSettings } from '.'; | ||
|
||
const invalidBlockPresetSchema = { | ||
name: 'Tabbed content', | ||
settings: { | ||
invalid_setting: 'this is an invalid setting as this key does not exist', | ||
}, | ||
blocks: [ | ||
{ | ||
type: '_tab', | ||
settings: { | ||
title: 'Tab 1', | ||
}, | ||
blocks: [ | ||
{ | ||
type: 'text', | ||
}, | ||
], | ||
}, | ||
{ | ||
type: '_tab', | ||
settings: { | ||
title: 'Tab 2', | ||
invalid_nested_setting: 'this is an invalid setting as this key does not exist', | ||
}, | ||
blocks: [ | ||
{ | ||
type: 'text', | ||
settings: { | ||
invalid_nested_setting_1: 'this is an invalid setting as this key does not exist', | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
type: '_tab', | ||
settings: { | ||
title: 'Tab 3', | ||
}, | ||
blocks: [ | ||
{ | ||
type: 'text', | ||
blocks: [ | ||
{ | ||
type: 'text', | ||
settings: { | ||
invalid_nested_setting_2: 'this is an invalid setting as this key does not exist', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const invalidBlockDefaultSchema = { | ||
settings: { | ||
invalid_setting: 'this is an invalid setting as this key does not exist', | ||
}, | ||
blocks: [ | ||
{ type: '_tab', settings: { title: 'Tab 1' }, blocks: [{ type: 'text' }] }, | ||
{ type: '_tab', settings: { title: 'Tab 2' }, blocks: [{ type: 'text' }] }, | ||
{ | ||
type: '_tab', | ||
settings: { | ||
title: 'Tab 3', | ||
invalid_nested_setting_1: 'this is an invalid setting as this key does not exist', | ||
}, | ||
blocks: [ | ||
{ | ||
type: 'text', | ||
settings: { | ||
invalid_nested_setting_2: 'this is an invalid setting as this key does not exist', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const invalidSectionPresetSchema = { | ||
name: 't:sections.announcement-bar.presets.name', | ||
settings: { | ||
invalid_setting: 'this is an invalid setting as this key does not exist', | ||
}, | ||
blocks: [ | ||
{ | ||
type: 'announcement', | ||
settings: { | ||
invalid_nested_setting_1: 'this is an invalid setting as this key does not exist', | ||
}, | ||
blocks: [ | ||
{ | ||
type: 'text', | ||
settings: { | ||
invalid_nested_setting_3: 'this is an invalid setting as this key does not exist', | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
type: '_tab', | ||
settings: { | ||
title: 'Tab 1', | ||
invalid_nested_setting_2: 'this is an invalid setting as this key does not exist', | ||
}, | ||
blocks: [ | ||
{ | ||
type: 'text', | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
const invalidSectionDefaultSchema = { | ||
settings: { | ||
invalid_setting: 'this is an invalid setting as this key does not exist', | ||
}, | ||
blocks: [ | ||
{ | ||
type: 'announcement', | ||
settings: { | ||
title: 'Tab 1', | ||
invalid_nested_setting_1: 'this is an invalid setting as this key does not exist', | ||
}, | ||
blocks: [ | ||
{ | ||
type: 'text', | ||
settings: { | ||
invalid_nested_setting_2: 'this is an invalid setting as this key does not exist', | ||
}, | ||
}, | ||
{ | ||
type: 'text', | ||
settings: { | ||
invalid_nested_setting_3: 'this is an invalid setting as this key does not exist', | ||
}, | ||
blocks: [ | ||
{ | ||
type: 'text', | ||
settings: { | ||
invalid_nested_setting_4: 'this is an invalid setting as this key does not exist', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
|
||
describe('ValidPresetAndDefaultSettings', () => { | ||
describe('blocks', () => { | ||
it('should report invalid keys within preset settings', async () => { | ||
const theme: MockTheme = { | ||
'blocks/_tab.liquid': horizonNestedBlockSchema, | ||
'blocks/tabs.liquid': createHorizonBlockSchema(invalidBlockPresetSchema, null), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidPresetAndDefaultSettings]); | ||
expect(offenses).to.have.length(4); | ||
expect(offenses[0].message).to.include( | ||
'Preset setting "invalid_setting" does not exist in settings', | ||
); | ||
expect(offenses[1].message).to.include( | ||
'Preset block setting "invalid_nested_setting" does not exist in settings', | ||
); | ||
expect(offenses[2].message).to.include( | ||
'Preset block setting "invalid_nested_setting_1" does not exist in settings', | ||
); | ||
expect(offenses[3].message).to.include( | ||
'Preset block setting "invalid_nested_setting_2" does not exist in settings', | ||
); | ||
}); | ||
|
||
it('should report invalid keys within default settings', async () => { | ||
const theme: MockTheme = { | ||
'blocks/_tab.liquid': horizonNestedBlockSchema, | ||
'blocks/tabs.liquid': createHorizonBlockSchema(null, invalidBlockDefaultSchema), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidPresetAndDefaultSettings]); | ||
expect(offenses).to.have.length(3); | ||
expect(offenses[0].message).to.include( | ||
'Default setting "invalid_setting" does not exist in settings', | ||
); | ||
expect(offenses[1].message).to.include( | ||
'Default block setting "invalid_nested_setting_1" does not exist in settings', | ||
); | ||
expect(offenses[2].message).to.include( | ||
'Default block setting "invalid_nested_setting_2" does not exist in settings', | ||
); | ||
}); | ||
|
||
it('should not report when all preset settings in the block are valid', async () => { | ||
const theme: MockTheme = { | ||
'blocks/_tab.liquid': horizonNestedBlockSchema, | ||
'blocks/tabs.liquid': validHorizonPresetSchema, | ||
}; | ||
|
||
const offenses = await check(theme, [ValidPresetAndDefaultSettings]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it('should not report when all default settings in the block are valid', async () => { | ||
const theme: MockTheme = { | ||
'blocks/_tab.liquid': horizonNestedBlockSchema, | ||
'blocks/tabs.liquid': validHorizonDefaultSchema, | ||
}; | ||
|
||
const offenses = await check(theme, [ValidPresetAndDefaultSettings]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
}); | ||
|
||
describe('sections', () => { | ||
it('should report invalid keys within preset setting', async () => { | ||
const theme: MockTheme = { | ||
'blocks/_tab.liquid': horizonNestedBlockSchema, | ||
'blocks/announcement.liquid': horizonNestedBlockSchema, | ||
'sections/announcement-bar.liquid': createDawnSectionSchema( | ||
invalidSectionPresetSchema, | ||
null, | ||
), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidPresetAndDefaultSettings]); | ||
expect(offenses).to.have.length(4); | ||
expect(offenses[0].message).to.include( | ||
`Preset setting "invalid_setting" does not exist in settings`, | ||
); | ||
expect(offenses[1].message).to.include( | ||
`Preset block setting "invalid_nested_setting_1" does not exist in settings`, | ||
); | ||
expect(offenses[2].message).to.include( | ||
`Preset block setting "invalid_nested_setting_3" does not exist in settings`, | ||
); | ||
expect(offenses[3].message).to.include( | ||
`Preset block setting "invalid_nested_setting_2" does not exist in settings`, | ||
); | ||
}); | ||
|
||
it('should report invalid keys in a sections default setting', async () => { | ||
const theme: MockTheme = { | ||
'blocks/_tab.liquid': horizonNestedBlockSchema, | ||
'sections/announcement-bar.liquid': createDawnSectionSchema( | ||
null, | ||
invalidSectionDefaultSchema, | ||
), | ||
}; | ||
|
||
const offenses = await check(theme, [ValidPresetAndDefaultSettings]); | ||
expect(offenses).to.have.length(5); | ||
expect(offenses[0].message).to.include( | ||
`Default setting "invalid_setting" does not exist in settings`, | ||
); | ||
expect(offenses[1].message).to.include( | ||
`Default block setting "invalid_nested_setting_1" does not exist in settings`, | ||
); | ||
expect(offenses[2].message).to.include( | ||
`Default block setting "invalid_nested_setting_2" does not exist in settings`, | ||
); | ||
expect(offenses[3].message).to.include( | ||
`Default block setting "invalid_nested_setting_3" does not exist in settings`, | ||
); | ||
expect(offenses[4].message).to.include( | ||
`Default block setting "invalid_nested_setting_4" does not exist in settings`, | ||
); | ||
}); | ||
|
||
it('should not report when all preset settings are valid', async () => { | ||
const theme: MockTheme = { | ||
'sections/announcement-bar.liquid': validDawnPresetSchema, | ||
}; | ||
|
||
const offenses = await check(theme, [ValidPresetAndDefaultSettings]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
|
||
it('should not report when all default settings are valid', async () => { | ||
const theme: MockTheme = { | ||
'sections/announcement-bar.liquid': validDawnDefaultSchema, | ||
}; | ||
|
||
const offenses = await check(theme, [ValidPresetAndDefaultSettings]); | ||
expect(offenses).to.have.length(0); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.