Skip to content

Commit 79ba0ef

Browse files
committed
refactor: reorder icon properties and improve test cases for better clarity and functionality
1 parent 087a67b commit 79ba0ef

File tree

5 files changed

+43
-42
lines changed

5 files changed

+43
-42
lines changed

src/components/Icon/Icon.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
}
44

55
.carousel__icon {
6-
width: var(--vc-icn-width);
7-
height: var(--vc-icn-width);
86
fill: currentColor;
7+
height: var(--vc-icn-width);
8+
width: var(--vc-icn-width);
99
}

src/components/Icon/Icon.spec.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mount } from '@vue/test-utils'
2-
import { expect, it, describe, afterEach, vi } from 'vitest'
2+
import { afterEach, describe, expect, it, vi } from 'vitest'
33

44
import { Icon } from './Icon'
55
import { IconName, IconProps } from './Icon.types'
@@ -11,13 +11,6 @@ describe('Icon.ts', () => {
1111
consoleMock.mockReset()
1212
})
1313

14-
it('It should error if no iconName', () => {
15-
const wrapper = mount(Icon, { props: {} as IconProps })
16-
expect(wrapper.html()).toBe('')
17-
expect(consoleMock).toHaveBeenCalledOnce()
18-
expect(consoleMock.mock.calls[0][0]).toBe('[Vue warn]: Missing required prop: "name"')
19-
})
20-
2114
it('It should error if iconName is invalid', () => {
2215
const wrapper = mount(Icon, { props: { name: 'foo' as IconProps['name'] } })
2316
expect(wrapper.html()).toBe('')
@@ -27,14 +20,23 @@ describe('Icon.ts', () => {
2720
)
2821
})
2922

23+
it('It should error if no iconName', () => {
24+
const wrapper = mount(Icon, { props: {} as IconProps })
25+
expect(wrapper.html()).toBe('')
26+
expect(consoleMock).toHaveBeenCalledOnce()
27+
expect(consoleMock.mock.calls[0][0]).toBe('[Vue warn]: Missing required prop: "name"')
28+
})
29+
3030
it('It should render standalone', async () => {
31-
await Promise.all(Object.values(IconName).map(async (name) => {
32-
const wrapper = mount(Icon, { props: { name: name } })
33-
expect(consoleMock).not.toHaveBeenCalled()
34-
expect(wrapper.html()).toMatchSnapshot()
31+
await Promise.all(
32+
Object.values(IconName).map(async (name) => {
33+
const wrapper = mount(Icon, { props: { name: name } })
34+
expect(consoleMock).not.toHaveBeenCalled()
35+
expect(wrapper.html()).toMatchSnapshot()
3536

36-
await wrapper.setProps({ title: 'Test title' })
37-
expect(wrapper.find('svg title').text()).toBe('Test title')
38-
}))
37+
await wrapper.setProps({ title: 'Test title' })
38+
expect(wrapper.find('svg title').text()).toBe('Test title')
39+
})
40+
)
3941
})
4042
})

src/components/Icon/Icon.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ import { DEFAULT_CONFIG, injectCarousel } from '@/shared'
44

55
import { IconName, IconNameValue, IconProps } from './Icon.types'
66

7-
function isIconName(candidate: string): candidate is IconName {
8-
return candidate in IconName
9-
}
10-
117
const iconI18n = <Name extends IconNameValue>(name: Name) =>
128
`icon${name.charAt(0).toUpperCase() + name.slice(1)}` as `icon${Capitalize<Name>}`
139

14-
const validateIconName = (value: IconNameValue) => {
15-
return value && isIconName(value)
16-
}
17-
1810
export const icons = {
19-
arrowUp: 'M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z',
2011
arrowDown: 'M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6 1.41-1.41z',
21-
arrowRight: 'M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z',
2212
arrowLeft: 'M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z',
13+
arrowRight: 'M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z',
14+
arrowUp: 'M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z',
15+
}
16+
17+
function isIconName(candidate: string): candidate is IconName {
18+
return candidate in IconName
19+
}
20+
21+
const validateIconName = (value: IconNameValue) => {
22+
return value && isIconName(value)
2323
}
2424

2525
export const Icon = defineComponent<IconProps>({
@@ -45,8 +45,7 @@ export const Icon = defineComponent<IconProps>({
4545
const path = icons[iconName]
4646
const pathEl = h('path', { d: path })
4747

48-
const iconTitle: string =
49-
carousel?.config.i18n[iconI18n(iconName)] || props.title!
48+
const iconTitle: string = carousel?.config.i18n[iconI18n(iconName)] || props.title!
5049

5150
const titleEl = h('title', iconTitle)
5251

src/components/Icon/Icon.types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
export enum IconName {
2-
arrowUp = 'arrowUp',
32
arrowDown = 'arrowDown',
4-
arrowRight = 'arrowRight',
53
arrowLeft = 'arrowLeft',
4+
arrowRight = 'arrowRight',
5+
arrowUp = 'arrowUp',
66
}
77

88
export type IconNameValue = `${IconName}`
99

10-
export interface IconProps {
11-
title?: string
10+
export type IconProps = {
1211
name: IconNameValue
12+
title?: string
1313
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`Icon.ts > It should render standalone 1`] = `
4-
"<svg class="carousel__icon" viewBox="0 0 24 24" role="img" aria-label="Arrow pointing upwards">
5-
<title>Arrow pointing upwards</title>
6-
<path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"></path>
4+
"<svg class="carousel__icon" viewBox="0 0 24 24" role="img" aria-label="Arrow pointing downwards">
5+
<title>Arrow pointing downwards</title>
6+
<path d="M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6 1.41-1.41z"></path>
77
</svg>"
88
`;
99

1010
exports[`Icon.ts > It should render standalone 2`] = `
11-
"<svg class="carousel__icon" viewBox="0 0 24 24" role="img" aria-label="Arrow pointing downwards">
12-
<title>Arrow pointing downwards</title>
13-
<path d="M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6 1.41-1.41z"></path>
11+
"<svg class="carousel__icon" viewBox="0 0 24 24" role="img" aria-label="Arrow pointing to the left">
12+
<title>Arrow pointing to the left</title>
13+
<path d="M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z"></path>
1414
</svg>"
1515
`;
1616

@@ -22,8 +22,8 @@ exports[`Icon.ts > It should render standalone 3`] = `
2222
`;
2323

2424
exports[`Icon.ts > It should render standalone 4`] = `
25-
"<svg class="carousel__icon" viewBox="0 0 24 24" role="img" aria-label="Arrow pointing to the left">
26-
<title>Arrow pointing to the left</title>
27-
<path d="M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z"></path>
25+
"<svg class="carousel__icon" viewBox="0 0 24 24" role="img" aria-label="Arrow pointing upwards">
26+
<title>Arrow pointing upwards</title>
27+
<path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z"></path>
2828
</svg>"
2929
`;

0 commit comments

Comments
 (0)