-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6c4a442
set default viewport if applicable
yannbf b4fc809
fix lint issues
yannbf 25d2a8e
refactor return type
yannbf 52903fa
fix tests
yannbf a853126
refactor
yannbf 223e2f9
update error message
yannbf 0969121
fix lint errors
yannbf 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 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,151 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
import { page } from '@vitest/browser/context'; | ||
|
||
import { DEFAULT_VIEWPORT_DIMENSIONS, type ViewportsParam, setViewport } from './viewports'; | ||
|
||
vi.mock('@vitest/browser/context', () => ({ | ||
page: { | ||
viewport: vi.fn(), | ||
}, | ||
})); | ||
|
||
describe('setViewport', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
globalThis.__vitest_browser__ = true; | ||
}); | ||
|
||
afterEach(() => { | ||
globalThis.__vitest_browser__ = false; | ||
}); | ||
|
||
it('should no op outside when not in Vitest browser mode', async () => { | ||
globalThis.__vitest_browser__ = false; | ||
|
||
await setViewport(); | ||
expect(page.viewport).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should fall back to DEFAULT_VIEWPORT_DIMENSIONS if defaultViewport does not exist', async () => { | ||
const viewportsParam: any = { | ||
defaultViewport: 'nonExistentViewport', | ||
}; | ||
|
||
await setViewport(viewportsParam); | ||
expect(page.viewport).toHaveBeenCalledWith(1200, 900); | ||
}); | ||
|
||
it('should set the dimensions of viewport from INITIAL_VIEWPORTS', async () => { | ||
const viewportsParam: any = { | ||
// supported by default in addon viewports | ||
defaultViewport: 'ipad', | ||
}; | ||
|
||
await setViewport(viewportsParam); | ||
expect(page.viewport).toHaveBeenCalledWith(768, 1024); | ||
}); | ||
|
||
it('should set custom defined viewport dimensions', async () => { | ||
const viewportsParam: ViewportsParam = { | ||
defaultViewport: 'customViewport', | ||
viewports: { | ||
customViewport: { | ||
name: 'Custom Viewport', | ||
type: 'mobile', | ||
styles: { | ||
width: '800px', | ||
height: '600px', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
await setViewport(viewportsParam); | ||
expect(page.viewport).toHaveBeenCalledWith(800, 600); | ||
}); | ||
|
||
it('should correctly handle percentage-based dimensions', async () => { | ||
const viewportsParam: ViewportsParam = { | ||
defaultViewport: 'percentageViewport', | ||
viewports: { | ||
percentageViewport: { | ||
name: 'Percentage Viewport', | ||
type: 'desktop', | ||
styles: { | ||
width: '50%', | ||
height: '50%', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
await setViewport(viewportsParam); | ||
expect(page.viewport).toHaveBeenCalledWith(600, 450); // 50% of 1920 and 1080 | ||
}); | ||
|
||
it('should correctly handle vw and vh based dimensions', async () => { | ||
const viewportsParam: ViewportsParam = { | ||
defaultViewport: 'viewportUnits', | ||
viewports: { | ||
viewportUnits: { | ||
name: 'VW/VH Viewport', | ||
type: 'desktop', | ||
styles: { | ||
width: '50vw', | ||
height: '50vh', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
await setViewport(viewportsParam); | ||
expect(page.viewport).toHaveBeenCalledWith(600, 450); // 50% of 1920 and 1080 | ||
}); | ||
|
||
it('should correctly handle em based dimensions', async () => { | ||
const viewportsParam: ViewportsParam = { | ||
defaultViewport: 'viewportUnits', | ||
viewports: { | ||
viewportUnits: { | ||
name: 'em/rem Viewport', | ||
type: 'mobile', | ||
styles: { | ||
width: '20em', | ||
height: '40rem', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
await setViewport(viewportsParam); | ||
expect(page.viewport).toHaveBeenCalledWith(320, 640); // dimensions * 16 | ||
}); | ||
|
||
it('should throw an error for unsupported dimension values', async () => { | ||
const viewportsParam: ViewportsParam = { | ||
defaultViewport: 'invalidViewport', | ||
viewports: { | ||
invalidViewport: { | ||
name: 'Invalid Viewport', | ||
type: 'desktop', | ||
styles: { | ||
width: 'calc(100vw - 20px)', | ||
height: '10pc', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
await expect(setViewport(viewportsParam)).rejects.toThrowErrorMatchingInlineSnapshot(` | ||
[SB_ADDON_VITEST_0001 (UnsupportedViewportDimensionError): Encountered an unsupported value "calc(100vw - 20px)" when setting the viewport width dimension. | ||
|
||
The Storybook plugin only supports values in the following units: | ||
- px, vh, vw, em, rem and %. | ||
|
||
You can either change the viewport for this story to use one of the supported units or skip the test by adding '!test' to the story's tags per https://storybook.js.org/docs/writing-stories/tags] | ||
`); | ||
expect(page.viewport).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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
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.
There was a problem hiding this comment.
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