/** @jsxImportSource @compiled/react */ // eslint-disable-next-line import/no-extraneous-dependencies import { cssMap, cx } from '@compiled/react'; import { render } from '@testing-library/react'; import { expectTypeOf } from 'expect-type'; import type { XCSSProp, XCSSAllProperties, XCSSAllPseudos } from '../index'; describe('xcss prop', () => { it('should allow all styles from xcss prop to class name when no constraints are applied', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp }) { return
foo
; } const styles = cssMap({ redColor: { color: 'red', '&::after': { backgroundColor: 'green' } }, }); const { getByText } = render(); expect(getByText('foo')).toHaveCompiledCss('color', 'red'); }); it('should type error when given a pseudo and none are allowed', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp }) { return
foo
; } const styles = cssMap({ redColor: { color: 'red', '&::after': { backgroundColor: 'green' } }, }); const { getByText } = render( ); expect(getByText('foo')).toHaveCompiledCss('color', 'red'); }); it('should concat styles from class name and xcss prop', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp }) { return (
foo
); } const styles = cssMap({ redColor: { color: 'red' }, }); const { getByText } = render(); expect(getByText('foo')).toHaveCompiledCss('color', 'red'); }); it('should type error when passing styles that are not defined', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp<'color', XCSSAllPseudos> }) { return
foo
; } const styles = cssMap({ redColor: { backgroundColor: 'red' }, }); expectTypeOf( ).toBeObject(); }); it('should concat styles together', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp }) { return
foo
; } const styles = cssMap({ redColor: { color: 'red' }, greenBackground: { color: 'blue', backgroundColor: 'green' }, }); const { getByText } = render( ); expect(getByText('foo')).toHaveCompiledCss('color', 'blue'); expect(getByText('foo')).toHaveCompiledCss('backgroundColor', 'green'); }); it('should conditionally apply styles directly', () => { const styles = cssMap({ redColor: { color: 'red' }, blueColor: { color: 'blue' }, }); function CSSPropComponent({ xcss }: { xcss: XCSSProp<'color', never> }) { return
foo
; } function Parent({ isRed }: { isRed: boolean }) { return ; } const { getByText, rerender } = render(); expect(getByText('foo')).toHaveCompiledCss('color', 'red'); expect(getByText('foo')).not.toHaveCompiledCss('color', 'blue'); rerender(); expect(getByText('foo')).toHaveCompiledCss('color', 'blue'); expect(getByText('foo')).not.toHaveCompiledCss('color', 'red'); }); it('should conditionally apply styles via cx function', () => { const styles = cssMap({ redColor: { color: 'red' }, blueColor: { color: 'blue' }, }); function CSSPropComponent({ xcss }: { xcss: XCSSProp<'color', never> }) { return
foo
; } function Parent({ isRed }: { isRed: boolean }) { return ; } const { getByText, rerender } = render(); expect(getByText('foo')).toHaveCompiledCss('color', 'red'); expect(getByText('foo')).not.toHaveCompiledCss('color', 'blue'); rerender(); expect(getByText('foo')).toHaveCompiledCss('color', 'blue'); expect(getByText('foo')).not.toHaveCompiledCss('color', 'red'); }); it('should transform inline object', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp<'color', XCSSAllPseudos> }) { return
foo
; } const { getByText } = render(); expect(getByText('foo')).toHaveCompiledCss('color', 'green'); }); it('should type error when passing in a disallowed value in a pseudo mixed with allowed values', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp<'color', '&:hover'> }) { return
foo
; } const styles = cssMap({ redColor: { color: 'red', '&:hover': { color: 'red', backgroundColor: 'red' } }, }); expectTypeOf( ).toBeObject(); expectTypeOf( ).toBeObject(); }); it('should type error when passing in @media property to xcss prop', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp<'color', '&:hover'> }) { return
foo
; } const styles = cssMap({ redColor: { color: 'red', '@media': { 'screen and': { color: 'red' } } }, }); expectTypeOf( ).toBeObject(); expectTypeOf( ).toBeObject(); }); it('should block selectors api from CSS Map', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp<'color', '&:hover'> }) { return
foo
; } const styles = cssMap({ primary: { selectors: { '&:not(:first-child):last-child': { color: 'red' } }, }, }); expectTypeOf( ' is not assignable to type 'undefined'. xcss={styles.primary} /> ).toBeObject(); }); it('should mark a xcss prop as required', () => { function CSSPropComponent({ xcss, }: { xcss: XCSSProp<'color' | 'backgroundColor', '&:hover', { requiredProperties: 'color' }>; }) { return
foo
; } expectTypeOf( '. xcss={{}} /> ).toBeObject(); }); it('should mark a xcss prop inside a pseudo as required', () => { function CSSPropComponent({ xcss, }: { xcss: XCSSProp<'color' | 'backgroundColor', '&:hover', { requiredProperties: 'color' }>; }) { return
foo
; } expectTypeOf( ).toBeObject(); }); it('should allow chained pseudo elements', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp }) { return
foo
; } const styles = cssMap({ redColor: { color: 'red', '&:hover::after': { backgroundColor: 'green' } }, }); const { getByText } = render(); expect(getByText('foo')).toHaveCompiledCss('color', 'red'); }); it('should type error when given a chained pseudo element and none are allowed', () => { function CSSPropComponent({ xcss }: { xcss: XCSSProp }) { return
foo
; } const styles = cssMap({ redColor: { color: 'red', '&:hover::after': { backgroundColor: 'green' } }, }); expectTypeOf( ).toBeObject(); }); });