|
| 1 | +/** @jsxImportSource @compiled/react */ |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | + |
| 4 | +import { css, cssMap, XCSSProp } from './__fixtures__/strict-api'; |
| 5 | + |
| 6 | +describe('createStrictAPI()', () => { |
| 7 | + describe('css()', () => { |
| 8 | + it('should type error when circumventing the excess property check', () => { |
| 9 | + const styles = css({ |
| 10 | + color: 'var(--ds-text)', |
| 11 | + accentColor: 'red', |
| 12 | + // @ts-expect-error — Type 'string' is not assignable to type 'undefined'.ts(2322) |
| 13 | + bkgrnd: 'red', |
| 14 | + '&:hover': { |
| 15 | + color: 'var(--ds-text-hover)', |
| 16 | + // @ts-expect-error — Type 'string' is not assignable to type 'undefined'.ts(2322) |
| 17 | + bkgrnd: 'red', |
| 18 | + }, |
| 19 | + }); |
| 20 | + |
| 21 | + const { getByTestId } = render(<div css={styles} data-testid="div" />); |
| 22 | + |
| 23 | + expect(getByTestId('div')).toHaveCompiledCss('color', 'var(--ds-text)'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should constrain declared types for css() func', () => { |
| 27 | + // @ts-expect-error — Type '"red"' is not assignable to type '"var(--ds-surface)" | "var(--ds-surface-sunken" | undefined'.ts(2322) |
| 28 | + const styles = css({ background: 'red' }); |
| 29 | + |
| 30 | + const { getByTestId } = render(<div css={styles} data-testid="div" />); |
| 31 | + |
| 32 | + expect(getByTestId('div')).toHaveCompiledCss('background-color', 'red'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should mark all properties as optional', () => { |
| 36 | + const styles1 = css({}); |
| 37 | + const styles2 = css({ '&:hover': {} }); |
| 38 | + |
| 39 | + const { getByTestId } = render(<div css={[styles1, styles2]} data-testid="div" />); |
| 40 | + |
| 41 | + expect(getByTestId('div')).not.toHaveCompiledCss('color', 'red'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should constrain pseudos', () => { |
| 45 | + const styles = css({ |
| 46 | + // @ts-expect-error — Type '"red"' is not assignable to type '"var(--ds-surface)" | "var(--ds-surface-sunken" | undefined'.ts(2322) |
| 47 | + background: 'red', |
| 48 | + '&:hover': { |
| 49 | + // @ts-expect-error — Type '"red"' is not assignable to type '"var(--ds-surface)" | "var(--ds-surface-sunken" | undefined'.ts(2322) |
| 50 | + background: 'red', |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + const { getByTestId } = render(<div css={styles} data-testid="div" />); |
| 55 | + |
| 56 | + expect(getByTestId('div')).toHaveCompiledCss('background-color', 'red', { target: ':hover' }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should allow valid properties inside pseudos that are different to root', () => { |
| 60 | + const styles = css({ |
| 61 | + background: 'var(--ds-surface)', |
| 62 | + '&:hover': { |
| 63 | + accentColor: 'red', |
| 64 | + background: 'var(--ds-surface-hover)', |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + const { getByTestId } = render(<div css={styles} data-testid="div" />); |
| 69 | + |
| 70 | + expect(getByTestId('div')).toHaveCompiledCss('background', 'var(--ds-surface-hover)', { |
| 71 | + target: ':hover', |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should allow valid properties', () => { |
| 76 | + const styles = css({ |
| 77 | + background: 'var(--ds-surface)', |
| 78 | + accentColor: 'red', |
| 79 | + color: 'var(--ds-text)', |
| 80 | + all: 'inherit', |
| 81 | + '&:hover': { color: 'var(--ds-text-hover)' }, |
| 82 | + '&:invalid': { color: 'orange' }, |
| 83 | + }); |
| 84 | + |
| 85 | + const { getByTestId } = render(<div css={styles} data-testid="div" />); |
| 86 | + |
| 87 | + expect(getByTestId('div')).toHaveCompiledCss('all', 'inherit'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('cssMap()', () => { |
| 92 | + it('should allow valid properties', () => { |
| 93 | + const styles = cssMap({ |
| 94 | + primary: { |
| 95 | + background: 'var(--ds-surface)', |
| 96 | + accentColor: 'red', |
| 97 | + all: 'inherit', |
| 98 | + '&:hover': { color: 'var(--ds-text-hover)' }, |
| 99 | + '&:invalid': { color: 'orange' }, |
| 100 | + }, |
| 101 | + }); |
| 102 | + |
| 103 | + const { getByTestId } = render(<div css={styles.primary} data-testid="div" />); |
| 104 | + |
| 105 | + expect(getByTestId('div')).toHaveCompiledCss('background', 'var(--ds-surface)'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should allow valid properties inside pseudos that are different to root', () => { |
| 109 | + const styles = cssMap({ |
| 110 | + primary: { |
| 111 | + background: 'var(--ds-surface)', |
| 112 | + '&:hover': { |
| 113 | + accentColor: 'red', |
| 114 | + background: 'var(--ds-surface-hover)', |
| 115 | + }, |
| 116 | + }, |
| 117 | + }); |
| 118 | + |
| 119 | + const { getByTestId } = render(<div css={styles.primary} data-testid="div" />); |
| 120 | + |
| 121 | + expect(getByTestId('div')).toHaveCompiledCss('background', 'var(--ds-surface-hover)', { |
| 122 | + target: ':hover', |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should type error invalid vales', () => { |
| 127 | + const styles = cssMap({ |
| 128 | + primary: { |
| 129 | + // @ts-expect-error — Type '{ val: string; }' is not assignable to type 'Readonly<Properties<string | number, string & {}>> & PseudosDeclarations & EnforceSchema<{ background: "var(--ds-surface)" | "var(--ds-surface-sunken"; }>'. |
| 130 | + val: 'ok', |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + const { getByTestId } = render(<div css={styles.primary} data-testid="div" />); |
| 135 | + |
| 136 | + expect(getByTestId('div')).toHaveCompiledCss('val', 'ok'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should type error invalid values in pseudos', () => { |
| 140 | + const styles = cssMap({ |
| 141 | + primary: { |
| 142 | + // @ts-expect-error — Type '"red"' is not assignable to type '"var(--ds-surface)" | "var(--ds-surface-sunken" | undefined'.ts(2322) |
| 143 | + background: 'red', |
| 144 | + '&:hover': { |
| 145 | + // @ts-expect-error — Type 'string' is not assignable to type 'never'.ts(2322) |
| 146 | + val: 'ok', |
| 147 | + }, |
| 148 | + }, |
| 149 | + }); |
| 150 | + |
| 151 | + const { getByTestId } = render(<div css={styles.primary} data-testid="div" />); |
| 152 | + |
| 153 | + expect(getByTestId('div')).toHaveCompiledCss('val', 'ok', { target: ':hover' }); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('XCSSProp', () => { |
| 158 | + it('should allow valid values', () => { |
| 159 | + function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', never>> }) { |
| 160 | + return <button data-testid="button" className={xcss} />; |
| 161 | + } |
| 162 | + |
| 163 | + const { getByTestId } = render(<Button xcss={{ background: 'var(--ds-surface)' }} />); |
| 164 | + |
| 165 | + expect(getByTestId('button')).toHaveCompiledCss('background', 'var(--ds-surface)'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should type error for invalid known values', () => { |
| 169 | + function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', never>> }) { |
| 170 | + return <button data-testid="button" className={xcss} />; |
| 171 | + } |
| 172 | + |
| 173 | + const { getByTestId } = render( |
| 174 | + <Button |
| 175 | + xcss={{ |
| 176 | + // @ts-expect-error — Type '"red"' is not assignable to type '"var(--ds-surface)" | "var(--ds-surface-sunken" | CompiledPropertyDeclarationReference | undefined'.ts(2322) |
| 177 | + background: 'red', |
| 178 | + // @ts-expect-error — Type '{ background: string; }' is not assignable to type 'undefined'.ts(2322) |
| 179 | + '&::after': { |
| 180 | + background: 'red', |
| 181 | + }, |
| 182 | + }} |
| 183 | + /> |
| 184 | + ); |
| 185 | + |
| 186 | + expect(getByTestId('button')).toHaveCompiledCss('background-color', 'red'); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should type error for invalid unknown values', () => { |
| 190 | + function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', never>> }) { |
| 191 | + return <button data-testid="button" className={xcss} />; |
| 192 | + } |
| 193 | + |
| 194 | + const { getByTestId } = render( |
| 195 | + <Button |
| 196 | + xcss={{ |
| 197 | + // @ts-expect-error — Type '{ asd: number; }' is not assignable to type 'Internal$XCSSProp<"background", never, { background: "var(--ds-surface)" | "var(--ds-surface-sunken"; }, PickObjects<{ background: "var(--ds-surface)" | "var(--ds-surface-sunken"; }>, never>'. |
| 198 | + asd: 0, |
| 199 | + }} |
| 200 | + /> |
| 201 | + ); |
| 202 | + |
| 203 | + expect(getByTestId('button')).toHaveCompiledCss('asd', '0'); |
| 204 | + }); |
| 205 | + |
| 206 | + it('should type error for unsupported known pseudos', () => { |
| 207 | + function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', never>> }) { |
| 208 | + return <button data-testid="button" className={xcss} />; |
| 209 | + } |
| 210 | + const { getByTestId } = render( |
| 211 | + <Button |
| 212 | + xcss={{ |
| 213 | + // @ts-expect-error — Object literal may only specify known properties, and '':hover'' does not exist in type |
| 214 | + ':hover': { |
| 215 | + color: 'red', |
| 216 | + }, |
| 217 | + }} |
| 218 | + /> |
| 219 | + ); |
| 220 | + |
| 221 | + expect(getByTestId('button')).toHaveCompiledCss('color', 'red', { target: ':hover' }); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should type error for unsupported unknown pseudos', () => { |
| 225 | + function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', never>> }) { |
| 226 | + return <button data-testid="button" className={xcss} />; |
| 227 | + } |
| 228 | + |
| 229 | + const { getByTestId } = render( |
| 230 | + <Button |
| 231 | + xcss={{ |
| 232 | + // @ts-expect-error — Object literal may only specify known properties, and '':hover'' does not exist in type |
| 233 | + ':asd': { |
| 234 | + color: 'red', |
| 235 | + }, |
| 236 | + }} |
| 237 | + /> |
| 238 | + ); |
| 239 | + |
| 240 | + expect(getByTestId('button')).toHaveCompiledCss('color', 'red', { target: ':asd' }); |
| 241 | + }); |
| 242 | + |
| 243 | + it('should type error for invalid known values in pseudos', () => { |
| 244 | + function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'color', '&:hover'>> }) { |
| 245 | + return <button data-testid="button" className={xcss} />; |
| 246 | + } |
| 247 | + |
| 248 | + const { getByTestId } = render( |
| 249 | + <Button |
| 250 | + xcss={{ |
| 251 | + '&:hover': { |
| 252 | + // @ts-expect-error — Type '"red"' is not assignable to type 'CompiledPropertyDeclarationReference | "var(--ds-text)" | undefined'.ts(2322) |
| 253 | + color: 'red', |
| 254 | + }, |
| 255 | + }} |
| 256 | + /> |
| 257 | + ); |
| 258 | + |
| 259 | + expect(getByTestId('button')).toHaveCompiledCss('color', 'red', { target: ':hover' }); |
| 260 | + }); |
| 261 | + |
| 262 | + it('should type error for invalid unknown values in pseudos', () => { |
| 263 | + function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'color', '&:hover'>> }) { |
| 264 | + return <button data-testid="button" className={xcss} />; |
| 265 | + } |
| 266 | + |
| 267 | + const { getByTestId } = render( |
| 268 | + <Button |
| 269 | + xcss={{ |
| 270 | + '&:hover': { |
| 271 | + // @ts-expect-error — Type '{ asd: string; }' is not assignable to type 'MarkAsRequired<XCSSItem<"color", { color: "var(--ds-text)"; }>, never>'. |
| 272 | + asd: 'red', |
| 273 | + }, |
| 274 | + }} |
| 275 | + /> |
| 276 | + ); |
| 277 | + |
| 278 | + expect(getByTestId('button')).toHaveCompiledCss('asd', 'red', { target: ':hover' }); |
| 279 | + }); |
| 280 | + |
| 281 | + it('should enforce required properties', () => { |
| 282 | + function Button({ |
| 283 | + xcss, |
| 284 | + }: { |
| 285 | + xcss: ReturnType< |
| 286 | + typeof XCSSProp< |
| 287 | + 'background', |
| 288 | + never, |
| 289 | + { requiredProperties: 'background'; requiredPseudos: never } |
| 290 | + > |
| 291 | + >; |
| 292 | + }) { |
| 293 | + return <button data-testid="button" className={xcss} />; |
| 294 | + } |
| 295 | + |
| 296 | + const { getByTestId } = render( |
| 297 | + <Button |
| 298 | + // @ts-expect-error — Type '{}' is not assignable to type 'Internal$XCSSProp<"background", never, EnforceSchema<{ background: "var(--ds-surface)" | "var(--ds-surface-sunken"; }>, object, { requiredProperties: "background"; requiredPseudos: never; }>'.ts(2322) |
| 299 | + xcss={{}} |
| 300 | + /> |
| 301 | + ); |
| 302 | + |
| 303 | + expect(getByTestId('button')).not.toHaveCompiledCss('color', 'red'); |
| 304 | + }); |
| 305 | + }); |
| 306 | + |
| 307 | + it('should throw when calling XCSSProp directly', () => { |
| 308 | + expect(() => { |
| 309 | + XCSSProp(); |
| 310 | + }).toThrow(); |
| 311 | + }); |
| 312 | +}); |
0 commit comments