Skip to content

Commit 7715c23

Browse files
committed
test(core): [styled] add unit test
1 parent 92ae096 commit 7715c23

File tree

13 files changed

+15442
-970
lines changed

13 files changed

+15442
-970
lines changed

docs/.vitepress/config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default defineConfig({
66
base: '/vue3-styled-components/',
77
title: 'Vue3 Styled Components',
88
description: 'A tool for css in js.',
9-
head: [['link', { rel: 'icon', href: '/vue3-styled-components/logo.png' }]],
9+
head: [['link', { rel: 'icon', href: '/vue3-styled-components/logo.png ' }]],
1010
themeConfig: {
1111
logo: '/logo.png',
1212
demoblock: {

package/__tests__/styled.spec.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { createGlobalStyle, styled } from '../styled'
2+
import { afterEach, describe, expect, it } from 'vitest'
3+
import { mount } from '@vue/test-utils'
4+
import { ref } from 'vue'
5+
6+
describe('styled', () => {
7+
afterEach(() => {
8+
// Reset env
9+
const styleTags = document.querySelectorAll('style')
10+
styleTags.forEach((styleTag) => {
11+
styleTag.innerHTML = ''
12+
})
13+
})
14+
15+
it('should create a styled component', async () => {
16+
const MyComponent = {
17+
template: '<div>Hello World</div>'
18+
}
19+
20+
const StyledComponent = styled(MyComponent)`
21+
color: blue;
22+
`
23+
expect(StyledComponent).toBeDefined()
24+
expect(StyledComponent.name).toMatch(/^styled-/)
25+
26+
const wrapper = mount(StyledComponent)
27+
const className = wrapper.find('div').element.className
28+
29+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).selectorText).toBe(`.${className}`)
30+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style.color).toBe('blue')
31+
expect(wrapper.text()).toBe('Hello World')
32+
})
33+
34+
it('should throw error if the element is invalid', () => {
35+
// 模拟一个无效的元素类型
36+
const invalidElement = 'invalid-element'
37+
38+
// 断言当传入无效的元素类型时,应该抛出错误
39+
expect(() => {
40+
styled(invalidElement)
41+
}).toThrowError('The element is invalid.')
42+
})
43+
44+
it('should style styled component', async () => {
45+
const StyledComponent = styled.div`
46+
font-size: 14px;
47+
`
48+
49+
const StyledComponent2 = styled(StyledComponent).attrs({ style: 'color: blue' })`
50+
font-size: 16px;
51+
`
52+
53+
const wrapper = mount(StyledComponent2, { slots: { default: () => 'Hello World' } })
54+
const className = wrapper.find('div').element.className
55+
56+
expect(className).contain((document.styleSheets[0].cssRules[0] as CSSStyleRule).selectorText.replace(/\./, ''))
57+
expect(className).contain((document.styleSheets[0].cssRules[1] as CSSStyleRule).selectorText.replace(/\./, ''))
58+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style['font-size']).toBe('16px')
59+
expect((document.styleSheets[0].cssRules[1] as CSSStyleRule).style['font-size']).toBe('14px')
60+
expect(wrapper.find('div').element.className).toMatch(/^styled-/)
61+
expect(wrapper.text()).toBe('Hello World')
62+
expect(wrapper.find('div').element.style.color).toBe('blue')
63+
})
64+
65+
it('should inject attrs', async () => {
66+
const StyledComponent = styled.div.attrs({
67+
style: 'color: red'
68+
})`
69+
font-size: 14px;
70+
`
71+
const wrapper = mount(StyledComponent)
72+
73+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style['font-size']).toBe('14px')
74+
expect(wrapper.find('div').element.style.color).toBe('red')
75+
})
76+
77+
it('should be reactive when props change', async () => {
78+
const StyledComponent = styled('div', { color: String })`
79+
color: ${(props) => props.color};
80+
`
81+
const color = ref('red')
82+
const wrapper = mount(StyledComponent, {
83+
props: {
84+
color: color.value
85+
}
86+
})
87+
88+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style['color']).toBe('red')
89+
90+
await wrapper.setProps({ color: 'blue' })
91+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style['color']).toBe('blue')
92+
})
93+
94+
it('should create a global style component', async () => {
95+
const GlobalStyle = createGlobalStyle`
96+
body {
97+
background-color: red;
98+
}
99+
`
100+
mount(GlobalStyle)
101+
102+
expect(GlobalStyle).toBeDefined()
103+
expect(GlobalStyle.name).toMatch(/^global-style/)
104+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).selectorText).toBe('body')
105+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style['background-color']).toBe('red')
106+
})
107+
})

package/__tests__/types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module 'csstype' {
2+
interface CSSStyleSheet {
3+
cssRules: Array<CSSStyleRule & { selectorText: string }>
4+
}
5+
}

0 commit comments

Comments
 (0)