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

Addon Vitest: Set default viewport if applicable #28905

Merged
merged 7 commits into from
Aug 19, 2024

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Aug 16, 2024

Closes #28901

What I did

The Vitest integration supports viewports, but it only sets them if they match the viewports object. So if you have a viewport like mobile1 which has 320px width and 568px height, then the plugin will use playwright to set the viewport to 320, 568. The thing is, the default viewport (responsive) is actually invalid, in the sense that it doesn’t exist in the viewports object and therefore doesn’t relate to any width and height values. This PR makes the plugin fall back to a desktop-like dimension of 1200 width and 900 height.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 76.3 MB 76.3 MB 0 B 0.33 0%
initSize 169 MB 169 MB 1.9 kB 0.93 0%
diffSize 92.7 MB 92.7 MB 1.9 kB 0.93 0%
buildSize 7.46 MB 7.46 MB 2.06 kB 1.01 0%
buildSbAddonsSize 1.61 MB 1.62 MB 1.24 kB 240.46 0.1%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 2.3 MB 2.3 MB 0 B 0.89 0%
buildSbPreviewSize 351 kB 351 kB 823 B 3.97 0.2%
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 4.46 MB 4.46 MB 2.06 kB 1.23 0%
buildPreviewSize 3 MB 3 MB 0 B 0.9 0%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 22.3s 21.1s -1s -235ms 0.79 -5.8%
generateTime 19.5s 18.8s -745ms -0.95 -4%
initTime 16.5s 15.8s -704ms -0.93 -4.4%
buildTime 12.2s 11.2s -1s -32ms -0.88 -9.2%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 9.5s 7.7s -1s -848ms -0.09 -23.9%
devManagerResponsive 5s 5.1s 88ms 0.4 1.7%
devManagerHeaderVisible 759ms 675ms -84ms -1.78 🔰-12.4%
devManagerIndexVisible 793ms 715ms -78ms -1.74 🔰-10.9%
devStoryVisibleUncached 1.4s 1.5s 156ms 1.24 🔺10%
devStoryVisible 792ms 710ms -82ms -1.76 🔰-11.5%
devAutodocsVisible 770ms 576ms -194ms -1.71 🔰-33.7%
devMDXVisible 671ms 574ms -97ms -1.45 🔰-16.9%
buildManagerHeaderVisible 880ms 640ms -240ms -1.13 -37.5%
buildManagerIndexVisible 884ms 646ms -238ms -1.14 -36.8%
buildStoryVisible 991ms 678ms -313ms -1.13 -46.2%
buildAutodocsVisible 766ms 581ms -185ms -1.55 🔰-31.8%
buildMDXVisible 634ms 593ms -41ms -0.98 -6.9%

Greptile Summary

This PR enhances viewport handling in the Vitest integration for Storybook, introducing a default viewport size and improving edge case management.

  • Added DEFAULT_VIEWPORT_DIMENSIONS (1200x900) in code/addons/vitest/src/plugin/viewports.ts for fallback
  • Implemented logic to handle invalid viewport dimensions in setViewport function
  • Added comprehensive unit tests in code/addons/vitest/src/plugin/viewports.test.ts to cover various scenarios
  • Improved handling of percentage-based and complex CSS calculation viewport dimensions
  • Ensured valid viewport setting for the default 'responsive' viewport, which previously lacked dimensions

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings

});

it('should set the viewport to the specified dimensions from INITIAL_VIEWPORTS', async () => {
const viewportsParam: any = {
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Using 'any' type here might be too permissive. Consider using a more specific type

});

it('should fallback to DEFAULT_VIEWPORT_DIMENSIONS if defaultViewport does not exist', async () => {
const viewportsParam: any = {
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Using 'any' type here might be too permissive. Consider using a more specific type

height: Number.parseInt(styles.height, 10),
};
await page.viewport(width, height);
const validPixelOrNumber = /^\d+(px)?$/;
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Consider using a more descriptive variable name, e.g. isValidDimension

Comment on lines 39 to 40
// if both dimensions are not valid numbers e.g. 'calc(100vh - 10px)' or '100%', use the default dimensions instead
if (validPixelOrNumber.test(styles.width) && validPixelOrNumber.test(styles.height)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

style: This comment is slightly misleading. The code uses default dimensions if either width or height is invalid, not only if both are invalid

Copy link

nx-cloud bot commented Aug 16, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 0969121. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

Copy link
Contributor

@kasperpeulen kasperpeulen left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vitest addon: Introduce initial/fallback viewport size
3 participants