/** @jsxImportSource @compiled/react */ // eslint-disable-next-line import/no-extraneous-dependencies import { cssMap as cssMapLoose } from '@compiled/react'; import { render } from '@testing-library/react'; import { cssMap } from '../../create-strict-api/__tests__/__fixtures__/strict-api'; import type { XCSSProp } from '../../create-strict-api/__tests__/__fixtures__/strict-api'; import type { XCSSAllProperties, XCSSAllPseudos } from '../index'; function CSSPropComponent({ testId, xcss, }: { testId?: string; xcss: ReturnType>; }) { return (
foo
); } const styles = cssMap({ invalidMediaObject: { // @ts-expect-error — @media at rule object is not allowed in strict cssMap '@media': { screen: { color: 'red' }, }, }, invalidMediaQuery: { // @ts-expect-error — this specific @media is not in our allowed types '@media (min-width: 100px)': { color: 'red', }, }, valid: { '@media (min-width: 110rem)': { // @ts-expect-error — color should be a value from the strict schema color: 'red', }, }, }); const looseStyles = cssMapLoose({ invalidMediaObject: { '@media': { screen: { color: 'var(--ds-text)' }, }, }, invalidMediaQuery: { '@media (min-width: 100px)': { color: 'red', }, }, validMediaQueryInvalidProperty: { '@media (min-width: 110rem)': { color: 'red', }, }, valid: { '@media (min-width: 110rem)': { color: 'var(--ds-text)', }, }, }); describe('xcss prop', () => { it('should block all usage of loose media queries in strict api as it is unsafe', () => { const { getByText } = render( ); expect(getByText('foo')).toHaveCompiledCss('color', 'var(--ds-text)', { media: '(min-width: 110rem)', }); }); it('should type error invalid media queries from loose api', () => { const { getByTestId } = render( <> ); expect(getByTestId('foobar')).toHaveCompiledCss('color', 'red', { media: 'screen' }); }); it('should allow valid media queries in inline xcss prop', () => { const { getByText } = render( ); expect(getByText('foo')).toHaveCompiledCss('color', 'var(--ds-text)', { media: '(min-width: 110rem)', }); }); it('should allow valid media queries in inline xcss prop', () => { const { getByText } = render( ); expect(getByText('foo')).toHaveCompiledCss('color', 'red', { media: '(min-width: 110rem)' }); }); it('should allow valid psuedo through inline xcss prop', () => { const { getByText } = render( ); expect(getByText('foo')).toHaveCompiledCss('color', 'var(--ds-text-hover)', { media: '(min-width: 30rem)', target: ':hover', }); }); it('should type error for unexpected media query', () => { const { getByTestId } = render( <> ); expect(getByTestId('foobar')).toHaveCompiledCss('color', 'var(--ds-text)', { media: '(min-width: 100px)', }); }); it('should type check top level media query styles from cssMap', () => { const { getByText } = render(); expect(getByText('foo')).toHaveCompiledCss('color', 'red', { media: '(min-width: 110rem)' }); }); });