|
| 1 | +const rule = require('../use-styled-react-import') |
| 2 | +const {RuleTester} = require('eslint') |
| 3 | + |
| 4 | +const ruleTester = new RuleTester({ |
| 5 | + parserOptions: { |
| 6 | + ecmaVersion: 'latest', |
| 7 | + sourceType: 'module', |
| 8 | + ecmaFeatures: { |
| 9 | + jsx: true, |
| 10 | + }, |
| 11 | + }, |
| 12 | +}) |
| 13 | + |
| 14 | +ruleTester.run('use-styled-react-import', rule, { |
| 15 | + valid: [ |
| 16 | + // Valid: Component used without sx prop |
| 17 | + `import { Button } from '@primer/react' |
| 18 | + const Component = () => <Button>Click me</Button>`, |
| 19 | + |
| 20 | + // Valid: Component with sx prop imported from styled-react |
| 21 | + `import { Button } from '@primer/styled-react' |
| 22 | + const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`, |
| 23 | + |
| 24 | + // Valid: Utilities imported from styled-react |
| 25 | + `import { sx } from '@primer/styled-react'`, |
| 26 | + |
| 27 | + // Valid: Component not in the styled list |
| 28 | + `import { Avatar } from '@primer/react' |
| 29 | + const Component = () => <Avatar sx={{ color: 'red' }} />`, |
| 30 | + |
| 31 | + // Valid: Mixed imports - component without sx prop |
| 32 | + `import { Button, Text } from '@primer/react' |
| 33 | + const Component = () => <Button>Click me</Button>`, |
| 34 | + ], |
| 35 | + invalid: [ |
| 36 | + // Invalid: Box with sx prop imported from @primer/react |
| 37 | + { |
| 38 | + code: `import { Box } from '@primer/react' |
| 39 | + const Component = () => <Box sx={{ color: 'red' }}>Content</Box>`, |
| 40 | + output: `import { Box } from '@primer/styled-react' |
| 41 | + const Component = () => <Box sx={{ color: 'red' }}>Content</Box>`, |
| 42 | + errors: [ |
| 43 | + { |
| 44 | + messageId: 'useStyledReactImport', |
| 45 | + data: {componentName: 'Box'}, |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + |
| 50 | + // Invalid: Button with sx prop imported from @primer/react |
| 51 | + { |
| 52 | + code: `import { Button } from '@primer/react' |
| 53 | + const Component = () => <Button sx={{ margin: 2 }}>Click me</Button>`, |
| 54 | + output: `import { Button } from '@primer/styled-react' |
| 55 | + const Component = () => <Button sx={{ margin: 2 }}>Click me</Button>`, |
| 56 | + errors: [ |
| 57 | + { |
| 58 | + messageId: 'useStyledReactImport', |
| 59 | + data: {componentName: 'Button'}, |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + |
| 64 | + // Invalid: Multiple components, one with sx prop |
| 65 | + { |
| 66 | + code: `import { Button, Box, Avatar } from '@primer/react' |
| 67 | + const Component = () => ( |
| 68 | + <div> |
| 69 | + <Button>Regular button</Button> |
| 70 | + <Box sx={{ padding: 2 }}>Styled box</Box> |
| 71 | + <Avatar /> |
| 72 | + </div> |
| 73 | + )`, |
| 74 | + output: `import { Button, Avatar } from '@primer/react' |
| 75 | +import { Box } from '@primer/styled-react' |
| 76 | + const Component = () => ( |
| 77 | + <div> |
| 78 | + <Button>Regular button</Button> |
| 79 | + <Box sx={{ padding: 2 }}>Styled box</Box> |
| 80 | + <Avatar /> |
| 81 | + </div> |
| 82 | + )`, |
| 83 | + errors: [ |
| 84 | + { |
| 85 | + messageId: 'useStyledReactImport', |
| 86 | + data: {componentName: 'Box'}, |
| 87 | + }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + |
| 91 | + // Invalid: Utility import from @primer/react that should be from styled-react |
| 92 | + { |
| 93 | + code: `import { sx } from '@primer/react'`, |
| 94 | + output: `import { sx } from '@primer/styled-react'`, |
| 95 | + errors: [ |
| 96 | + { |
| 97 | + messageId: 'moveToStyledReact', |
| 98 | + data: {importName: 'sx'}, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + |
| 103 | + // Invalid: Button and Link, only Button uses sx |
| 104 | + { |
| 105 | + code: `import { Button, Link } from '@primer/react' |
| 106 | + const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`, |
| 107 | + output: `import { Link } from '@primer/react' |
| 108 | +import { Button } from '@primer/styled-react' |
| 109 | + const Component = () => <Button sx={{ color: 'red' }}>Click me</Button>`, |
| 110 | + errors: [ |
| 111 | + { |
| 112 | + messageId: 'useStyledReactImport', |
| 113 | + data: {componentName: 'Button'}, |
| 114 | + }, |
| 115 | + ], |
| 116 | + }, |
| 117 | + ], |
| 118 | +}) |
0 commit comments