Skip to content

Commit 3f08f81

Browse files
committed
test: add unit test for new features
1 parent 1e4cfef commit 3f08f81

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

packages/core/__tests__/styled.test.ts

+38-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createGlobalStyle, isStyledComponent, styled } from '../index'
22
import { afterEach, describe, expect, it } from 'vitest'
33
import { render, cleanup } from '@testing-library/vue'
44
import { getStyle } from './utils'
5-
import { ref } from 'vue'
5+
import { h, ref } from 'vue'
66

77
describe('styled', () => {
88
afterEach(() => {
@@ -72,8 +72,10 @@ describe('styled', () => {
7272
it('should inject attrs', async () => {
7373
const StyledComponent = styled.div.attrs({
7474
'data-testid': 'test',
75+
color: 'rgb(0, 0, 255)',
7576
})`
7677
height: 36px;
78+
color: ${(props) => props.color};
7779
`
7880
const instance = render(StyledComponent)
7981
const element = instance.getByTestId('test')
@@ -83,6 +85,24 @@ describe('styled', () => {
8385

8486
const style = getStyle(element)
8587
expect(style?.height).eq('36px')
88+
expect(style?.color).eq('rgb(0, 0, 255)')
89+
90+
const StyledComponent2 = styled.div.attrs(() => ({
91+
'data-testid': 'test2',
92+
color: 'rgb(255, 0, 0)',
93+
}))`
94+
height: 36px;
95+
color: ${(props) => props.color};
96+
`
97+
const instance2 = render(StyledComponent2)
98+
const element2 = instance2.getByTestId('test2')
99+
100+
expect(element2).toBeDefined()
101+
expect(element2.dataset['testid']).eq('test2')
102+
103+
const style2 = getStyle(element2)
104+
expect(style2?.height).eq('36px')
105+
expect(style2?.color).eq('rgb(255, 0, 0)')
86106
})
87107

88108
it('should react to props change', async () => {
@@ -118,4 +138,21 @@ describe('styled', () => {
118138
const style = getStyle(instance.baseElement)
119139
expect(style?.background).toBe('rgb(255, 0, 0)')
120140
})
141+
142+
it('should take effect of using styled component as selector', async () => {
143+
const StyledComponent = styled('div', { color: String }).attrs({ 'data-testid': 'test' })``
144+
const Container = styled.div`
145+
${StyledComponent} {
146+
height: 20px;
147+
}
148+
`
149+
const instance = render(Container, {
150+
slots: {
151+
default: () => h(StyledComponent),
152+
},
153+
})
154+
const element = instance.getByTestId('test')
155+
const style = getStyle(element)
156+
expect(style?.height).toBe('20px')
157+
})
121158
})

packages/core/__tests__/theme.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,25 @@ describe('theme-provider', () => {
3737
return expect(newStyle?.background).eq('rgb(0, 0, 255)')
3838
})
3939
})
40+
41+
it('should use the nestest theme', async () => {
42+
const StyledComponent = styled.p.attrs({ 'data-testid': 'test' })`
43+
background: ${(props) => props.theme.primary};
44+
`
45+
46+
const instance = render(ThemeProvider, {
47+
props: {
48+
theme: {
49+
primary: 'rgb(255, 0, 0)',
50+
},
51+
},
52+
slots: {
53+
default: () => h(ThemeProvider, { theme: { primary: 'rgb(0, 0, 255)' } }, h(StyledComponent)),
54+
},
55+
})
56+
57+
const element = instance.getByTestId('test')
58+
const style = getStyle(element)
59+
expect(style?.background).toBe('rgb(0, 0, 255)')
60+
})
4061
})

0 commit comments

Comments
 (0)