Skip to content
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

Conditional theme settings and split theme-settings unit tests into separate suites #899

Merged
merged 4 commits into from
Feb 4, 2025

Conversation

albchu
Copy link
Contributor

@albchu albchu commented Jan 25, 2025

Whats in this

https://vault.shopify.io/gsd/projects/44140-Conditional-Settings-for-Themes

Few changes here:

  • I split apart all the theme-settings unit tests from the 800 line spec file into separate suites by what each describe block was testing. Most notably the color_scheme_group unit tests were split into its own suite. I build upon this suite for conditional settings.
  • Added validation for conditional settings. Specifically added it for all approved settings types and added unit tests for both approved and explicitly unsupported settings types
  • color_scheme_group nested settings must not allow conditional settings. This is also now applied and unit tested.

Closes https://github.com/Shopify/shopify/issues/572105

image

@albchu albchu force-pushed the albchu/visible_if_doc branch from 57cd221 to a5f115f Compare February 4, 2025 17:04
@albchu albchu force-pushed the albchu/visible_if_doc branch from a5f115f to 05b0d74 Compare February 4, 2025 17:05
@albchu albchu marked this pull request as ready for review February 4, 2025 17:08
@albchu albchu requested a review from a team as a code owner February 4, 2025 17:08
Copy link
Contributor

@graygilmore graygilmore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻 test refactors make sense to me though I can't comment on the schemas/theme/setting.json changes

Copy link

@six-shopify six-shopify left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, nice work! A few questions/suggestions, but you're much more of a maintainer for this repo than I am so I think it's your call.

expect(diagnostics).toStrictEqual([]);
});

it.each(SETTINGS_TYPES_NOT_SUPPORTING_VISIBLE_IF)('should not allow visible_if on %s setting type', async (settingType) => {
Copy link

@six-shopify six-shopify Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how feasible this is so it's just a question — but rather than

for each forbidden setting type, test that the diagnostic correctly forbids it

maybe it should be

gather the diagnostic result for every setting type, and check that the result list contains exactly the list of forbidden types

I feel like that's maybe testing a more useful property. (I know you already have the handwritten schemas with allowed conditional settings, but if there's a list of all settings types at hand somewhere this could bolster the handwritten stuff with a broader property-based test.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting way to look at it! I had considered an approach similar to what you have in mind, but the issue came down to consciously handwriting invalid settings in with our valid ones. My concern was that it would make that fixture a bit too difficult to scan and compromises its value as a self documenting example of valid settings.

The approach I opted for instead is to include all valid setting types that support conditional settings within the theme-block-conditional-settings.json fixture and then only targeting the ones we explicitly do not wish to support (as listed in Clauderic's decision figjam).

Comment on lines 15 to 26
const SETTINGS_TYPES_NOT_SUPPORTING_VISIBLE_IF = [
'article',
'blog',
'collection',
'collection_list',
'metaobject',
'metaobject_list',
'page',
'product',
'product_list',
];

Copy link

@six-shopify six-shopify Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth lifting to a shared file within the tests dir since it's repeated multiple times?

Comment on lines 8 to 43
const inputSettingTypes = [
'article',
'blog',
'checkbox',
'collection_list',
'collection',
'color_background',
'color_scheme_group',
'color_scheme',
'color',
'font_picker',
'html',
'image_picker',
'inline_richtext',
'link_list',
'liquid',
'metaobject',
'metaobject_list',
'number',
'page',
'product_list',
'product',
'radio',
'range',
'richtext',
'select',
'style.layout_panel',
'style.size_panel',
'style.spacing_panel',
'text_alignment',
'text',
'textarea',
'url',
'video_url',
'video',
] as const;
Copy link

@six-shopify six-shopify Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe worth lifting this to the same shared file. and i guess this would be the list of all settings types that could potentially be validated against the set of settings types not supporting visible_if ?

Comment on lines +62 to +75
{
message: 'Incorrect type. Expected "array".',
severity: 1,
range: expect.objectContaining({
start: expect.objectContaining({
character: expect.any(Number),
line: expect.any(Number),
}),
end: expect.objectContaining({
character: expect.any(Number),
line: expect.any(Number),
}),
}),
},
Copy link

@six-shopify six-shopify Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So quite a lot of the tests that were added look like this:

const result = doSomething();
expect(result).toStrictEqual(
  /* some object with an exact shape including some specific message */
);

These are basically just snapshot tests, and to ease writing and maintenance I'd ask if we can't just use the snapshot API. It's not super well known, but snapshots can use expect.any(Number) matchers just like you're doing here (that link is towards the Jest docs since they're more detailed, but Vitest supports the same functionality).

The benefit is that if a diagnostic message changes, we can just update the snapshots automatically and then only need to validate the git diff. It also makes it a lot quicker to add new test cases while sticking to the existing pattern. Up to you, though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im scared of making too many drastic changes to the existing unit tests that I refactored so I will be avoiding this one simply to limit the churn Ive done here 😅

@albchu albchu merged commit c049c90 into main Feb 4, 2025
5 checks passed
@albchu albchu deleted the albchu/visible_if_doc branch February 4, 2025 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants