|
| 1 | +/** @jsxImportSource @compiled/react */ |
| 2 | +// eslint-disable-next-line import/no-extraneous-dependencies |
| 3 | +import { cssMap as cssMapLoose } from '@compiled/react'; |
| 4 | +import { render } from '@testing-library/react'; |
| 5 | + |
| 6 | +import { cssMap } from '../../create-strict-api/__tests__/__fixtures__/strict-api'; |
| 7 | +import type { XCSSProp } from '../../create-strict-api/__tests__/__fixtures__/strict-api'; |
| 8 | +import type { XCSSAllProperties, XCSSAllPseudos } from '../index'; |
| 9 | + |
| 10 | +function CSSPropComponent({ |
| 11 | + testId, |
| 12 | + xcss, |
| 13 | +}: { |
| 14 | + testId?: string; |
| 15 | + xcss: ReturnType<typeof XCSSProp<XCSSAllProperties, XCSSAllPseudos>>; |
| 16 | +}) { |
| 17 | + return ( |
| 18 | + <div data-testid={testId} className={xcss}> |
| 19 | + foo |
| 20 | + </div> |
| 21 | + ); |
| 22 | +} |
| 23 | + |
| 24 | +const styles = cssMap({ |
| 25 | + invalidMediaObject: { |
| 26 | + // @ts-expect-error — @media at rule object is not allowed in strict cssMap |
| 27 | + '@media': { |
| 28 | + screen: { color: 'red' }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + invalidMediaQuery: { |
| 32 | + // @ts-expect-error — this specific @media is not in our allowed types |
| 33 | + '@media (min-width: 100px)': { |
| 34 | + color: 'red', |
| 35 | + }, |
| 36 | + }, |
| 37 | + valid: { |
| 38 | + '@media (min-width: 110rem)': { |
| 39 | + // @ts-expect-error — color should be a value from the strict schema |
| 40 | + color: 'red', |
| 41 | + }, |
| 42 | + }, |
| 43 | +}); |
| 44 | + |
| 45 | +const looseStyles = cssMapLoose({ |
| 46 | + invalidMediaObject: { |
| 47 | + '@media': { |
| 48 | + screen: { color: 'var(--ds-text)' }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + invalidMediaQuery: { |
| 52 | + '@media (min-width: 100px)': { |
| 53 | + color: 'red', |
| 54 | + }, |
| 55 | + }, |
| 56 | + validMediaQueryInvalidProperty: { |
| 57 | + '@media (min-width: 110rem)': { |
| 58 | + color: 'red', |
| 59 | + }, |
| 60 | + }, |
| 61 | + valid: { |
| 62 | + '@media (min-width: 110rem)': { |
| 63 | + color: 'var(--ds-text)', |
| 64 | + }, |
| 65 | + }, |
| 66 | +}); |
| 67 | + |
| 68 | +describe('xcss prop', () => { |
| 69 | + it('should block all usage of loose media queries in strict api as it is unsafe', () => { |
| 70 | + const { getByText } = render( |
| 71 | + <CSSPropComponent |
| 72 | + // @ts-expect-error — Block all media queries in strict xcss prop |
| 73 | + xcss={looseStyles.valid} |
| 74 | + /> |
| 75 | + ); |
| 76 | + |
| 77 | + expect(getByText('foo')).toHaveCompiledCss('color', 'var(--ds-text)', { |
| 78 | + media: '(min-width: 110rem)', |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should type error invalid media queries from loose api', () => { |
| 83 | + const { getByTestId } = render( |
| 84 | + <> |
| 85 | + <CSSPropComponent |
| 86 | + // @ts-expect-error — Types of property '"@media"' are incompatible. |
| 87 | + xcss={looseStyles.invalidMediaObject} |
| 88 | + /> |
| 89 | + <CSSPropComponent testId="foobar" xcss={styles.invalidMediaObject} /> |
| 90 | + </> |
| 91 | + ); |
| 92 | + |
| 93 | + expect(getByTestId('foobar')).toHaveCompiledCss('color', 'red', { media: 'screen' }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should allow valid media queries in inline xcss prop', () => { |
| 97 | + const { getByText } = render( |
| 98 | + <CSSPropComponent |
| 99 | + xcss={{ |
| 100 | + '@media (min-width: 110rem)': { |
| 101 | + color: 'var(--ds-text)', |
| 102 | + }, |
| 103 | + }} |
| 104 | + /> |
| 105 | + ); |
| 106 | + |
| 107 | + expect(getByText('foo')).toHaveCompiledCss('color', 'var(--ds-text)', { |
| 108 | + media: '(min-width: 110rem)', |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should allow valid media queries in inline xcss prop', () => { |
| 113 | + const { getByText } = render( |
| 114 | + <CSSPropComponent |
| 115 | + xcss={{ |
| 116 | + '@media (min-width: 110rem)': { |
| 117 | + // @ts-expect-error — color should be a value from the strict schema |
| 118 | + color: 'red', |
| 119 | + }, |
| 120 | + }} |
| 121 | + /> |
| 122 | + ); |
| 123 | + |
| 124 | + expect(getByText('foo')).toHaveCompiledCss('color', 'red', { media: '(min-width: 110rem)' }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should allow valid psuedo through inline xcss prop', () => { |
| 128 | + const { getByText } = render( |
| 129 | + <CSSPropComponent |
| 130 | + xcss={{ '@media (min-width: 30rem)': { '&:hover': { color: 'var(--ds-text-hover)' } } }} |
| 131 | + /> |
| 132 | + ); |
| 133 | + |
| 134 | + expect(getByText('foo')).toHaveCompiledCss('color', 'var(--ds-text-hover)', { |
| 135 | + media: '(min-width: 30rem)', |
| 136 | + target: ':hover', |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should type error for unexpected media query', () => { |
| 141 | + const { getByTestId } = render( |
| 142 | + <> |
| 143 | + <CSSPropComponent |
| 144 | + // NOTE: This doesn't currently error as the output isn't the generic value |
| 145 | + // when the cssMap call has type supressions. While not ideal this is acceptable |
| 146 | + // for now. Hopefully we can fix this in the future. |
| 147 | + xcss={styles.invalidMediaQuery} |
| 148 | + /> |
| 149 | + <CSSPropComponent |
| 150 | + // @ts-expect-error — Passing loose media queries to XCSS prop is not supported. |
| 151 | + xcss={looseStyles.invalidMediaQuery} |
| 152 | + /> |
| 153 | + <CSSPropComponent |
| 154 | + // @ts-expect-error — Passing loose media queries to XCSS prop is not supported. |
| 155 | + xcss={looseStyles.validMediaQueryInvalidProperty} |
| 156 | + /> |
| 157 | + <CSSPropComponent |
| 158 | + testId="foobar" |
| 159 | + xcss={{ |
| 160 | + // @ts-expect-error — Types of property '"@media"' are incompatible. |
| 161 | + '@media (min-width: 100px)': { |
| 162 | + color: 'var(--ds-text)', |
| 163 | + }, |
| 164 | + }} |
| 165 | + /> |
| 166 | + </> |
| 167 | + ); |
| 168 | + |
| 169 | + expect(getByTestId('foobar')).toHaveCompiledCss('color', 'var(--ds-text)', { |
| 170 | + media: '(min-width: 100px)', |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should type check top level media query styles from cssMap', () => { |
| 175 | + const { getByText } = render(<CSSPropComponent xcss={styles.valid} />); |
| 176 | + |
| 177 | + expect(getByText('foo')).toHaveCompiledCss('color', 'red', { media: '(min-width: 110rem)' }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments