Skip to content

Commit

Permalink
fix(icons): fix tests for the icons
Browse files Browse the repository at this point in the history
  • Loading branch information
andresin87 committed Mar 27, 2023
1 parent 85f8041 commit 79a83d5
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 54 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 5 additions & 8 deletions packages/components/icons/scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import optimize from './utils/optimize.mjs'
import pathSVG from './utils/pathSVG.mjs'
import prettify from './utils/prettify.mjs'
import readFile from './utils/readFile.mjs'
import titleize from './utils/titleize.mjs'
import writeFile from './utils/writeFile.mjs'

const main = async (pattern = 'assets/**/*.svg') => {
Expand All @@ -27,13 +26,11 @@ const main = async (pattern = 'assets/**/*.svg') => {
const tsxIconCode = prettify(
componentize({
componentName: capitalCase(name),
node: titleize(
optimize(svgData, {
attributes: [{ fill: 'currentColor' }, { stroke: 'currentColor' }],
title: name,
}).trim(),
name
),
node: optimize(svgData, {
attributes: [{ fill: 'currentColor' }, { stroke: 'currentColor' }],
title: name,
}).trim(),
title: name,
})
)

Expand Down
29 changes: 24 additions & 5 deletions packages/components/icons/scripts/utils/componentize.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
const componentize = ({
componentName = 'ComponentName',
node = '<svg />',
}) => `export const ${componentName} = () => (
${node}
import { parseSync, stringify } from 'svgson'

const componentize = ({ componentName = 'ComponentName', node = '<svg />', title }) => {
const {
children,
attributes: { fill, stroke, 'data-title': dataTitle, ...otherAttributes } = {
fill: 'currentColor',
stroke: 'currentColor',
},
...others
} = parseSync(node)

return `import { IconProps } from '../Types'
export const ${componentName} = ({title, fill = '${fill}', stroke = '${stroke}', ...props}: IconProps) => (
<svg ${Object.entries(otherAttributes)
.map(([key, value]) => `${key}="${value}"`)
.join(' ')}
{...{ ...(title && {'data-title': title}), fill, stroke, ...props}}
>
\${title === undefined ? undefined : <title>{title}</title>}
${stringify(children)}
</svg>
)
`
}

export default componentize
31 changes: 0 additions & 31 deletions packages/components/icons/scripts/utils/titleize.mjs

This file was deleted.

5 changes: 5 additions & 0 deletions packages/components/icons/src/Types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface IconProps {
title?: string
fill?: string
stroke?: string
}
15 changes: 10 additions & 5 deletions packages/components/icons/src/icons/Check.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
export const Check = () => (
import { IconProps } from '../Types'

export const Check = ({
title,
fill = 'currentColor',
stroke = 'currentColor',
...props
}: IconProps) => (
<svg
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
data-title="check"
fill="currentColor"
stroke="currentColor"
{...{ ...(title && { 'data-title': title }), fill, stroke, ...props }}
>
<title>check</title>
${title === undefined ? undefined : <title>{title}</title>}
<path d="M8.91958 20.1667C8.73748 20.1667 8.56045 20.1323 8.38847 20.0635C8.21649 19.9947 8.05969 19.8915 7.91806 19.7539L2.42489 14.4176C2.14163 14.1425 2 13.8083 2 13.4152C2 13.0222 2.14163 12.688 2.42489 12.4129C2.70814 12.1377 3.04704 12.0001 3.44158 12.0001C3.83612 12.0001 4.18513 12.1377 4.48862 12.4129L8.91958 16.7173L19.5417 6.42797C19.825 6.1528 20.1639 6.0103 20.5584 6.00048C20.953 5.99065 21.2919 6.13315 21.5751 6.42797C21.8584 6.70313 22 7.03727 22 7.43036C22 7.82346 21.8584 8.15759 21.5751 8.43276L9.92109 19.7539C9.77946 19.8915 9.62266 19.9947 9.45068 20.0635C9.27871 20.1323 9.10167 20.1667 8.91958 20.1667Z" />
</svg>
)
49 changes: 44 additions & 5 deletions packages/components/icons/src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,54 @@ import { describe, expect, it } from 'vitest'
import * as Icons from './index'

describe('Icons', () => {
describe('', () => {
it('should be an svg', () => {
Object.entries(Icons).forEach(([name, Icon]) => {
Object.entries(Icons).forEach(([name, Icon]) => {
describe(`${name}`, () => {
it('should be an svg', () => {
// Given
const props = {}
const props = { 'data-testid': 'testId' }

// When
render(<Icon {...props} />)
const element = screen.getByTitle(name.toLowerCase())
const element = screen.getByTestId(props['data-testid'])

// Then
expect(element).not.toBeNull()
})

it('should not have height and width', () => {
// Given
const props = { 'data-testid': 'testId' }

// When
render(<Icon {...props} />)
const element = screen.getByTestId(props['data-testid'])

// Then
expect(element).not.toHaveAttribute('width')
expect(element).not.toHaveAttribute('height')
})

it('should have viewBox="0 0 24 24"', () => {
// Given
const props = { 'data-testid': 'testId' }

// When
render(<Icon {...props} />)
const element = screen.getByTestId(props['data-testid'])

// Then
expect(element).toHaveAttribute('viewBox', '0 0 24 24')
})

it('should have a title', () => {
// Given
const props = {
title: 'title',
}

// When
render(<Icon {...props} />)
const element = screen.getByTitle(props.title)

// Then
expect(element).not.toBeNull()
Expand Down

0 comments on commit 79a83d5

Please sign in to comment.