Skip to content

Commit 855f34b

Browse files
committed
test: add unit test for them-provider and keyframes
1 parent 7715c23 commit 855f34b

File tree

18 files changed

+3133
-8648
lines changed

18 files changed

+3133
-8648
lines changed

.idea/codeStyles/Project.xml

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/__tests__/export.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { describe, expectTypeOf, it } from 'vitest'
2+
import { ThemeProvider as TP } from '../providers/theme'
3+
import { styled, keyframes, createGlobalStyle, ThemeProvider } from '../index'
4+
5+
describe('export', () => {
6+
it('should export properly functions', async () => {
7+
expectTypeOf(ThemeProvider).toEqualTypeOf<typeof TP>()
8+
expectTypeOf(styled).toBeFunction()
9+
expectTypeOf(keyframes).toBeFunction()
10+
expectTypeOf(createGlobalStyle).toBeFunction()
11+
})
12+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { mount } from '@vue/test-utils'
3+
import { keyframes, styled } from '../index'
4+
5+
describe('keyframes', () => {
6+
it('should have keyframes', async () => {
7+
const kf = keyframes`
8+
from {
9+
margin-left: 100%;
10+
width: 300%;
11+
}
12+
13+
to {
14+
margin-left: 0%;
15+
width: 100%;
16+
}
17+
`
18+
19+
const StyledComponent = styled.div`
20+
width: 40px;
21+
height: 40px;
22+
background: ${(props: any) => props.theme.error};
23+
animation-duration: 3s;
24+
animation-name: ${kf};
25+
animation-iteration-count: infinite;
26+
`
27+
28+
mount(StyledComponent)
29+
expect((document.styleSheets[0].cssRules[0] as CSSKeyframesRule).name).toBe(kf)
30+
expect((document.styleSheets[0].cssRules[1] as CSSStyleRule).style['animation-name']).toBe(kf)
31+
})
32+
})
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createGlobalStyle, styled } from '../styled'
1+
import { createGlobalStyle, styled } from '../index'
22
import { afterEach, describe, expect, it } from 'vitest'
33
import { mount } from '@vue/test-utils'
44
import { ref } from 'vue'
@@ -43,20 +43,19 @@ describe('styled', () => {
4343

4444
it('should style styled component', async () => {
4545
const StyledComponent = styled.div`
46-
font-size: 14px;
46+
height: 36px;
4747
`
4848

4949
const StyledComponent2 = styled(StyledComponent).attrs({ style: 'color: blue' })`
50-
font-size: 16px;
50+
height: 44px;
5151
`
5252

5353
const wrapper = mount(StyledComponent2, { slots: { default: () => 'Hello World' } })
5454
const className = wrapper.find('div').element.className
55-
5655
expect(className).contain((document.styleSheets[0].cssRules[0] as CSSStyleRule).selectorText.replace(/\./, ''))
5756
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')
57+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style.height).toBe('44px')
58+
expect((document.styleSheets[0].cssRules[1] as CSSStyleRule).style.height).toBe('36px')
6059
expect(wrapper.find('div').element.className).toMatch(/^styled-/)
6160
expect(wrapper.text()).toBe('Hello World')
6261
expect(wrapper.find('div').element.style.color).toBe('blue')
@@ -66,15 +65,15 @@ describe('styled', () => {
6665
const StyledComponent = styled.div.attrs({
6766
style: 'color: red'
6867
})`
69-
font-size: 14px;
68+
height: 36px;
7069
`
7170
const wrapper = mount(StyledComponent)
7271

73-
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style['font-size']).toBe('14px')
72+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style.height).toBe('36px')
7473
expect(wrapper.find('div').element.style.color).toBe('red')
7574
})
7675

77-
it('should be reactive when props change', async () => {
76+
it('should react to props change', async () => {
7877
const StyledComponent = styled('div', { color: String })`
7978
color: ${(props) => props.color};
8079
`
@@ -93,15 +92,15 @@ describe('styled', () => {
9392

9493
it('should create a global style component', async () => {
9594
const GlobalStyle = createGlobalStyle`
96-
body {
97-
background-color: red;
98-
}
95+
body {
96+
background: red;
97+
}
9998
`
10099
mount(GlobalStyle)
101100

102101
expect(GlobalStyle).toBeDefined()
103102
expect(GlobalStyle.name).toMatch(/^global-style/)
104103
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')
104+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style.background).toBe('red')
106105
})
107106
})

package/__tests__/theme.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { mount } from '@vue/test-utils'
3+
import { ThemeProvider, styled } from '../index'
4+
import { h, reactive } from 'vue'
5+
6+
describe('theme-provider', () => {
7+
const StyledComponent = styled.p`
8+
background: ${(props) => props.theme.primary};
9+
`
10+
const theme = reactive({
11+
primary: 'red'
12+
})
13+
const wrapper = mount(ThemeProvider, {
14+
props: {
15+
theme
16+
},
17+
slots: {
18+
default: () => h(StyledComponent)
19+
}
20+
})
21+
22+
it('should use theme', async () => {
23+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style.background).toBe('red')
24+
})
25+
26+
it('should react to theme change', async () => {
27+
theme.primary = 'blue'
28+
await wrapper.setProps({
29+
theme
30+
})
31+
expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style.background).toBe('blue')
32+
})
33+
})

package/__tests__/types.d.ts

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

0 commit comments

Comments
 (0)