|
| 1 | +import { test, expect } from 'vitest' |
| 2 | +import { styled2panda } from '../src/styled2panda' |
| 3 | +import { Project } from 'ts-morph' |
| 4 | +import outdent from 'outdent' |
| 5 | + |
| 6 | +const project = new Project() |
| 7 | +const createSourceFile = (content: string) => project.createSourceFile('app.tsx', content, { overwrite: true }) |
| 8 | + |
| 9 | +test('import transform', () => { |
| 10 | + const sourceFile = createSourceFile(outdent` |
| 11 | + import styled from 'styled-components' |
| 12 | + `) |
| 13 | + |
| 14 | + expect(styled2panda({ sourceFile }).code).toMatchInlineSnapshot(`"import { styled } from '../styled-system/jsx'"`) |
| 15 | +}) |
| 16 | + |
| 17 | +test('component auto className references', () => { |
| 18 | + const sourceFile = createSourceFile(outdent` |
| 19 | + import styled from 'styled-components' |
| 20 | +
|
| 21 | + const Link = styled.a\` |
| 22 | + background: papayawhip; |
| 23 | + color: #bf4f74; |
| 24 | + \` |
| 25 | +
|
| 26 | + const Icon = styled.svg\` |
| 27 | + width: 48px; |
| 28 | + height: 48px; |
| 29 | +
|
| 30 | + \${Link}:hover & { |
| 31 | + fill: rebeccapurple; |
| 32 | + } |
| 33 | + \` |
| 34 | + `) |
| 35 | + |
| 36 | + expect(styled2panda({ sourceFile }).code).toMatchInlineSnapshot(` |
| 37 | + "import { styled } from '../styled-system/jsx' |
| 38 | +
|
| 39 | + const Link = styled('a', { base: { |
| 40 | + "background": "papayawhip", |
| 41 | + "color": "#bf4f74" |
| 42 | + } }, { defaultProps: { className: 'Link' } }) |
| 43 | +
|
| 44 | + const Icon = styled('svg', { base: { |
| 45 | + "width": "48px", |
| 46 | + "height": "48px", |
| 47 | + ".Link:hover &": { |
| 48 | + "fill": "rebeccapurple" |
| 49 | + } |
| 50 | + } }, { defaultProps: { className: 'Icon' } })" |
| 51 | + `) |
| 52 | +}) |
| 53 | + |
| 54 | +test('composition', () => { |
| 55 | + const sourceFile = createSourceFile(outdent` |
| 56 | + "import styled from 'styled-components' |
| 57 | +
|
| 58 | + // The Button from the last section without the interpolations |
| 59 | + const Button = styled.button\` |
| 60 | + color: #BF4F74; |
| 61 | + font-size: 1em; |
| 62 | + margin: 1em; |
| 63 | + padding: 0.25em 1em; |
| 64 | + border: 2px solid #BF4F74; |
| 65 | + border-radius: 3px; |
| 66 | + \`; |
| 67 | +
|
| 68 | + // A new component based on Button, but with some override styles |
| 69 | + const TomatoButton = styled(Button)\` |
| 70 | + color: tomato; |
| 71 | + border-color: tomato; |
| 72 | + \`; |
| 73 | + `) |
| 74 | + |
| 75 | + expect(styled2panda({ sourceFile }).code).toMatchInlineSnapshot(` |
| 76 | + ""import styled from 'styled-components' |
| 77 | +
|
| 78 | + // The Button from the last section without the interpolations |
| 79 | + const Button = styled('button', { base: { |
| 80 | + "color": "#BF4F74", |
| 81 | + "fontSize": "1em", |
| 82 | + "margin": "1em", |
| 83 | + "padding": "0.25em 1em", |
| 84 | + "border": "2px solid #BF4F74", |
| 85 | + "borderRadius": "3px" |
| 86 | + } }, { defaultProps: { className: 'Button' } }); |
| 87 | +
|
| 88 | + // A new component based on Button, but with some override styles |
| 89 | + const TomatoButton = styled(Button, { base: { |
| 90 | + "color": "tomato", |
| 91 | + "borderColor": "tomato" |
| 92 | + } }, { defaultProps: { className: 'TomatoButton' } });" |
| 93 | + `) |
| 94 | +}) |
0 commit comments