|
1 | 1 | /** |
2 | 2 | * WordPress dependencies |
3 | 3 | */ |
| 4 | +import { store as blockEditorStore } from '@wordpress/block-editor'; |
4 | 5 | import { store as coreStore } from '@wordpress/core-data'; |
5 | 6 | import { store as preferencesStore } from '@wordpress/preferences'; |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * Internal dependencies |
9 | 10 | */ |
10 | 11 | import { |
| 12 | + getCanvasHeight, |
11 | 13 | getDefaultRenderingMode, |
12 | 14 | getPostBlocksByName, |
13 | 15 | isCollaborationEnabledForCurrentPost, |
@@ -251,3 +253,72 @@ describe( 'getDefaultRenderingMode', () => { |
251 | 253 | } ); |
252 | 254 | } ); |
253 | 255 | } ); |
| 256 | + |
| 257 | +describe( 'getCanvasHeight', () => { |
| 258 | + // Default viewport breakpoints: Mobile 480, Tablet 782. |
| 259 | + function setupRegistry( { isZoomOut = false, viewport } = {} ) { |
| 260 | + getCanvasHeight.registry = { |
| 261 | + select: ( store ) => { |
| 262 | + if ( store === blockEditorStore ) { |
| 263 | + // In a real registry, `unlock( select( blockEditorStore ) )` |
| 264 | + // returns the combined public + private selectors, so both |
| 265 | + // getSettings and isZoomOut live in the unlocked map. |
| 266 | + const selectors = {}; |
| 267 | + lock( selectors, { |
| 268 | + getSettings: () => ( { |
| 269 | + __experimentalFeatures: viewport |
| 270 | + ? { viewport } |
| 271 | + : {}, |
| 272 | + } ), |
| 273 | + isZoomOut: () => isZoomOut, |
| 274 | + } ); |
| 275 | + return selectors; |
| 276 | + } |
| 277 | + }, |
| 278 | + }; |
| 279 | + } |
| 280 | + |
| 281 | + it( 'returns the portrait height at the mobile preset width', () => { |
| 282 | + // Mobile aspect ratio is 8/5 (portrait): 480 * 8/5 = 768. |
| 283 | + setupRegistry(); |
| 284 | + expect( getCanvasHeight( { canvasWidth: 480 } ) ).toBe( 768 ); |
| 285 | + } ); |
| 286 | + |
| 287 | + it( 'returns the portrait height at the tablet preset width', () => { |
| 288 | + // Tablet aspect ratio is 4/3 (portrait): 782 * 4/3 = 1043. |
| 289 | + setupRegistry(); |
| 290 | + expect( getCanvasHeight( { canvasWidth: 782 } ) ).toBe( 1043 ); |
| 291 | + } ); |
| 292 | + |
| 293 | + it( 'returns undefined for desktop (no aspect ratio applies)', () => { |
| 294 | + setupRegistry(); |
| 295 | + expect( getCanvasHeight( { canvasWidth: 1200 } ) ).toBeUndefined(); |
| 296 | + } ); |
| 297 | + |
| 298 | + it( 'returns undefined when zoom-out is active', () => { |
| 299 | + setupRegistry( { isZoomOut: true } ); |
| 300 | + expect( getCanvasHeight( { canvasWidth: 480 } ) ).toBeUndefined(); |
| 301 | + } ); |
| 302 | + |
| 303 | + it( 'returns undefined when the width is dragged within a device band but is not the preset', () => { |
| 304 | + // 400 resolves to Mobile (at or below the 480 breakpoint) but is not the |
| 305 | + // 480 preset, so the device height does not apply and the canvas fills. |
| 306 | + setupRegistry(); |
| 307 | + expect( getCanvasHeight( { canvasWidth: 400 } ) ).toBeUndefined(); |
| 308 | + } ); |
| 309 | + |
| 310 | + it( 'returns undefined when canvasWidth is not set', () => { |
| 311 | + setupRegistry(); |
| 312 | + expect( getCanvasHeight( {} ) ).toBeUndefined(); |
| 313 | + } ); |
| 314 | + |
| 315 | + it( 'uses custom viewport breakpoints when provided', () => { |
| 316 | + // Custom mobile preset 640: 640 * 8/5 = 1024. |
| 317 | + setupRegistry( { |
| 318 | + viewport: { mobile: '640px', tablet: '1024px' }, |
| 319 | + } ); |
| 320 | + expect( getCanvasHeight( { canvasWidth: 640 } ) ).toBe( 1024 ); |
| 321 | + // Custom tablet preset 1024: 1024 * 4/3 = 1365 (rounded). |
| 322 | + expect( getCanvasHeight( { canvasWidth: 1024 } ) ).toBe( 1365 ); |
| 323 | + } ); |
| 324 | +} ); |
0 commit comments