|
1 | | -import { createGlobalStyle, styled } from '../index' |
| 1 | +import { createGlobalStyle, isStyledComponent, styled } from '../index' |
2 | 2 | import { afterEach, describe, expect, it } from 'vitest' |
3 | | -import { mount } from '@vue/test-utils' |
| 3 | +import { render, cleanup } from '@testing-library/vue' |
| 4 | +import { getStyle } from './utils' |
4 | 5 | import { ref } from 'vue' |
5 | 6 |
|
6 | 7 | describe('styled', () => { |
7 | 8 | afterEach(() => { |
8 | 9 | // Reset env |
9 | | - const styleTags = document.querySelectorAll('style') |
10 | | - styleTags.forEach((styleTag) => { |
11 | | - styleTag.innerHTML = '' |
12 | | - }) |
| 10 | + cleanup() |
13 | 11 | }) |
14 | 12 |
|
15 | 13 | it('should create a styled component', async () => { |
16 | 14 | const MyComponent = { |
17 | 15 | template: '<div>Hello World</div>', |
18 | 16 | } |
19 | 17 |
|
20 | | - const StyledComponent = styled(MyComponent)` |
21 | | - color: blue; |
| 18 | + const StyledComponent = styled(MyComponent).attrs({ 'data-testid': 'test' })` |
| 19 | + color: rgb(0, 0, 255); |
22 | 20 | ` |
23 | | - expect(StyledComponent).toBeDefined() |
24 | | - expect(StyledComponent.name).toMatch(/^styled-/) |
25 | 21 |
|
26 | | - const wrapper = mount(StyledComponent) |
27 | | - const className = wrapper.find('div').element.className |
| 22 | + // Is styled component |
| 23 | + expect(isStyledComponent(StyledComponent)).toBeTruthy() |
| 24 | + |
| 25 | + const instance = render(StyledComponent) |
| 26 | + const element = instance.getByTestId('test') |
| 27 | + |
| 28 | + // Is element exist |
| 29 | + expect(element).toBeDefined() |
| 30 | + |
| 31 | + // Is applied style correctly |
| 32 | + const style = getStyle(element) |
| 33 | + expect(style).toBeDefined() |
| 34 | + expect(style?.color).eq('rgb(0, 0, 255)') |
28 | 35 |
|
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') |
| 36 | + // Is element text content correct |
| 37 | + expect(element.textContent).eq('Hello World') |
32 | 38 | }) |
33 | 39 |
|
34 | 40 | it('should throw error if the element is invalid', () => { |
35 | | - // 模拟一个无效的元素类型 |
| 41 | + // Mock a invalid element |
36 | 42 | const invalidElement = 'invalid-element' |
37 | 43 |
|
38 | | - // 断言当传入无效的元素类型时,应该抛出错误 |
| 44 | + // should throw error |
39 | 45 | expect(() => { |
40 | 46 | styled(invalidElement) |
41 | 47 | }).toThrowError('The element is invalid.') |
42 | 48 | }) |
43 | 49 |
|
44 | 50 | it('should style styled component', async () => { |
45 | 51 | const StyledComponent = styled.div` |
| 52 | + width: 80px; |
46 | 53 | height: 36px; |
47 | 54 | ` |
48 | 55 |
|
49 | | - const StyledComponent2 = styled(StyledComponent).attrs({ style: 'color: blue' })` |
| 56 | + const StyledComponent2 = styled(StyledComponent).attrs({ 'data-testid': 'test' })` |
| 57 | + color: rgb(0, 0, 255); |
50 | 58 | height: 44px; |
51 | 59 | ` |
52 | 60 |
|
53 | | - const wrapper = mount(StyledComponent2, { slots: { default: () => 'Hello World' } }) |
54 | | - const className = wrapper.find('div').element.className |
55 | | - expect(className).contain((document.styleSheets[0].cssRules[0] as CSSStyleRule).selectorText.replace(/\./, '')) |
56 | | - expect(className).contain((document.styleSheets[0].cssRules[1] as CSSStyleRule).selectorText.replace(/\./, '')) |
57 | | - expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style.height).toBe('36px') |
58 | | - expect((document.styleSheets[0].cssRules[1] as CSSStyleRule).style.height).toBe('44px') |
59 | | - expect(wrapper.find('div').element.className).toMatch(/^styled-/) |
60 | | - expect(wrapper.text()).toBe('Hello World') |
61 | | - expect(wrapper.find('div').element.style.color).toBe('blue') |
| 61 | + const instance = render(StyledComponent2) |
| 62 | + const element = instance.getByTestId('test') |
| 63 | + |
| 64 | + // Is applied style correctly |
| 65 | + const style = getStyle(element) |
| 66 | + expect(style).toBeDefined() |
| 67 | + expect(style?.color).eq('rgb(0, 0, 255)') |
| 68 | + expect(style?.height).eq('44px') |
| 69 | + expect(style?.width).eq('80px') |
62 | 70 | }) |
63 | 71 |
|
64 | 72 | it('should inject attrs', async () => { |
65 | 73 | const StyledComponent = styled.div.attrs({ |
66 | | - style: 'color: red', |
| 74 | + 'data-testid': 'test', |
67 | 75 | })` |
68 | 76 | height: 36px; |
69 | 77 | ` |
70 | | - const wrapper = mount(StyledComponent) |
| 78 | + const instance = render(StyledComponent) |
| 79 | + const element = instance.getByTestId('test') |
| 80 | + |
| 81 | + expect(element).toBeDefined() |
| 82 | + expect(element.dataset['testid']).eq('test') |
71 | 83 |
|
72 | | - expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style.height).toBe('36px') |
73 | | - expect(wrapper.find('div').element.style.color).toBe('red') |
| 84 | + const style = getStyle(element) |
| 85 | + expect(style?.height).eq('36px') |
74 | 86 | }) |
75 | 87 |
|
76 | 88 | it('should react to props change', async () => { |
77 | | - const StyledComponent = styled('div', { color: String })` |
| 89 | + const StyledComponent = styled('div', { color: String }).attrs({ 'data-testid': 'test' })` |
78 | 90 | color: ${(props) => props.color}; |
79 | 91 | ` |
80 | | - const color = ref('red') |
81 | | - const wrapper = mount(StyledComponent, { |
82 | | - props: { |
83 | | - color: color.value, |
84 | | - }, |
| 92 | + const color = ref('rgb(255, 0, 0)') |
| 93 | + const instance = render(StyledComponent, { |
| 94 | + props: { color: color.value }, |
85 | 95 | }) |
86 | 96 |
|
87 | | - expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style['color']).toBe('red') |
| 97 | + const element = instance.getByTestId('test') |
| 98 | + const style = getStyle(element) |
| 99 | + expect(style?.color).eq('rgb(255, 0, 0)') |
88 | 100 |
|
89 | | - await wrapper.setProps({ color: 'blue' }) |
90 | | - expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style['color']).toBe('blue') |
| 101 | + color.value = 'rgb(0, 0, 255)' |
| 102 | + await instance.rerender({ color: color.value }) |
| 103 | + const newStyle = getStyle(element) |
| 104 | + expect(newStyle?.color).eq('rgb(0, 0, 255)') |
91 | 105 | }) |
92 | 106 |
|
93 | 107 | it('should create a global style component', async () => { |
94 | 108 | const GlobalStyle = createGlobalStyle` |
95 | 109 | body { |
96 | | - background: red; |
| 110 | + background: rgb(255, 0, 0); |
97 | 111 | } |
98 | 112 | ` |
99 | | - mount(GlobalStyle) |
| 113 | + const instance = render(GlobalStyle) |
100 | 114 |
|
101 | 115 | expect(GlobalStyle).toBeDefined() |
102 | 116 | expect(GlobalStyle.name).toMatch(/^styled-global.+/) |
103 | | - expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).selectorText).toBe('body') |
104 | | - expect((document.styleSheets[0].cssRules[0] as CSSStyleRule).style.background).toBe('red') |
| 117 | + |
| 118 | + const style = getStyle(instance.baseElement) |
| 119 | + expect(style?.background).toBe('rgb(255, 0, 0)') |
105 | 120 | }) |
106 | 121 | }) |
0 commit comments