|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +/* |
| 10 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 11 | + * or more contributor license agreements. Licensed under the Elastic License |
| 12 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 13 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 14 | + * Side Public License, v 1; you may not use this file except |
| 15 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 16 | + * Side Public License, v 1. |
| 17 | + */ |
| 18 | + |
| 19 | +import React from 'react'; |
| 20 | +import { render } from '../../test/rtl'; |
| 21 | +import { useEuiTheme } from '../../services'; |
| 22 | +import { euiFlyoutStyles, composeFlyoutInlineStyles } from './flyout.styles'; |
| 23 | + |
| 24 | +// Mock the flyout constants |
| 25 | +jest.mock('./const', () => ({ |
| 26 | + isEuiFlyoutSizeNamed: jest.fn((size: string | number) => { |
| 27 | + return ['s', 'm', 'l', 'fill'].includes(size as string); |
| 28 | + }), |
| 29 | +})); |
| 30 | + |
| 31 | +describe('flyout.styles', () => { |
| 32 | + describe('euiFlyoutStyles', () => { |
| 33 | + const TestComponent = () => { |
| 34 | + const { euiTheme } = useEuiTheme(); |
| 35 | + const styles = euiFlyoutStyles({ |
| 36 | + euiTheme, |
| 37 | + colorMode: 'LIGHT', |
| 38 | + modifications: {}, |
| 39 | + highContrastMode: false, |
| 40 | + }); |
| 41 | + return <div data-testid="styles">{JSON.stringify(styles)}</div>; |
| 42 | + }; |
| 43 | + |
| 44 | + it('should include fill size styles', () => { |
| 45 | + const { getByTestId } = render(<TestComponent />); |
| 46 | + const stylesText = getByTestId('styles').textContent; |
| 47 | + expect(stylesText).toContain('fill'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should apply correct fill size CSS', () => { |
| 51 | + const { getByTestId } = render(<TestComponent />); |
| 52 | + const stylesText = getByTestId('styles').textContent; |
| 53 | + expect(stylesText).toContain('90vw'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should include all named size styles', () => { |
| 57 | + const { getByTestId } = render(<TestComponent />); |
| 58 | + const stylesText = getByTestId('styles').textContent; |
| 59 | + expect(stylesText).toContain('s'); |
| 60 | + expect(stylesText).toContain('m'); |
| 61 | + expect(stylesText).toContain('l'); |
| 62 | + expect(stylesText).toContain('fill'); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('composeFlyoutInlineStyles - basic functionality', () => { |
| 67 | + it('should handle custom width values (non-named sizes)', () => { |
| 68 | + const result = composeFlyoutInlineStyles( |
| 69 | + '400px', |
| 70 | + 'stacked', |
| 71 | + null, |
| 72 | + null, |
| 73 | + undefined |
| 74 | + ); |
| 75 | + expect(result).toEqual({ inlineSize: '400px' }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle fill size in stacked mode', () => { |
| 79 | + const result = composeFlyoutInlineStyles( |
| 80 | + 'fill', |
| 81 | + 'stacked', |
| 82 | + null, |
| 83 | + null, |
| 84 | + undefined |
| 85 | + ); |
| 86 | + expect(result).toEqual({}); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should calculate dynamic width for fill size in side-by-side mode', () => { |
| 90 | + const result = composeFlyoutInlineStyles( |
| 91 | + 'fill', |
| 92 | + 'side-by-side', |
| 93 | + 'sibling-id', |
| 94 | + 300, |
| 95 | + undefined |
| 96 | + ); |
| 97 | + expect(result).toEqual({ |
| 98 | + inlineSize: 'calc(90vw - 300px)', |
| 99 | + minInlineSize: '0', |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should handle maxWidth for non-fill sizes', () => { |
| 104 | + const result = composeFlyoutInlineStyles('m', 'stacked', null, null, 800); |
| 105 | + expect(result).toEqual({ |
| 106 | + maxInlineSize: '800px', |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should not apply dynamic styles when not fill size', () => { |
| 111 | + const result = composeFlyoutInlineStyles( |
| 112 | + 'm', |
| 113 | + 'side-by-side', |
| 114 | + 'sibling-id', |
| 115 | + 300, |
| 116 | + undefined |
| 117 | + ); |
| 118 | + expect(result).toEqual({}); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should not apply dynamic styles when not side-by-side mode', () => { |
| 122 | + const result = composeFlyoutInlineStyles( |
| 123 | + 'fill', |
| 124 | + 'stacked', |
| 125 | + 'sibling-id', |
| 126 | + 300, |
| 127 | + undefined |
| 128 | + ); |
| 129 | + expect(result).toEqual({}); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe('composeFlyoutInlineStyles - maxWidth handling', () => { |
| 134 | + it('should handle maxWidth for fill size without sibling', () => { |
| 135 | + const result = composeFlyoutInlineStyles( |
| 136 | + 'fill', |
| 137 | + 'stacked', |
| 138 | + null, |
| 139 | + null, |
| 140 | + 600 |
| 141 | + ); |
| 142 | + expect(result).toEqual({ |
| 143 | + maxInlineSize: '600px', |
| 144 | + minInlineSize: 'min(600px, 90vw)', |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should handle maxWidth for fill size with sibling', () => { |
| 149 | + const result = composeFlyoutInlineStyles( |
| 150 | + 'fill', |
| 151 | + 'side-by-side', |
| 152 | + 'sibling-id', |
| 153 | + 300, |
| 154 | + 600 |
| 155 | + ); |
| 156 | + expect(result).toEqual({ |
| 157 | + inlineSize: 'calc(90vw - 300px)', |
| 158 | + maxInlineSize: 'min(600px, calc(90vw - 300px))', |
| 159 | + minInlineSize: 'min(600px, calc(90vw - 300px))', |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should handle string maxWidth values', () => { |
| 164 | + const result = composeFlyoutInlineStyles( |
| 165 | + 'fill', |
| 166 | + 'stacked', |
| 167 | + null, |
| 168 | + null, |
| 169 | + '50%' |
| 170 | + ); |
| 171 | + expect(result).toEqual({ |
| 172 | + maxInlineSize: '50%', |
| 173 | + minInlineSize: 'min(50%, 90vw)', |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should handle boolean maxWidth (should be ignored)', () => { |
| 178 | + const result = composeFlyoutInlineStyles( |
| 179 | + 'fill', |
| 180 | + 'stacked', |
| 181 | + null, |
| 182 | + null, |
| 183 | + true |
| 184 | + ); |
| 185 | + // Boolean maxWidth should be ignored, but the function still processes it |
| 186 | + // because the condition `if (isFill && maxWidth)` evaluates to true for boolean true |
| 187 | + expect(result).toEqual({ |
| 188 | + maxInlineSize: true, |
| 189 | + minInlineSize: undefined, |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should handle fill size with maxWidth but no sibling in side-by-side mode', () => { |
| 194 | + // This tests the case where we're in side-by-side mode but there's no sibling |
| 195 | + const result = composeFlyoutInlineStyles( |
| 196 | + 'fill', |
| 197 | + 'side-by-side', |
| 198 | + null, |
| 199 | + null, |
| 200 | + 600 |
| 201 | + ); |
| 202 | + expect(result).toEqual({ |
| 203 | + maxInlineSize: '600px', |
| 204 | + minInlineSize: 'min(600px, 90vw)', |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should handle maxWidth with sibling but no dynamic width calculation', () => { |
| 209 | + // This tests the case where maxWidth is provided but dynamic width calculation |
| 210 | + // is not applied (e.g., not fill size, not side-by-side, etc.) |
| 211 | + const result = composeFlyoutInlineStyles( |
| 212 | + 'm', |
| 213 | + 'side-by-side', |
| 214 | + 'sibling-id', |
| 215 | + 300, |
| 216 | + 600 |
| 217 | + ); |
| 218 | + expect(result).toEqual({ |
| 219 | + maxInlineSize: '600px', |
| 220 | + }); |
| 221 | + }); |
| 222 | + }); |
| 223 | +}); |
0 commit comments