|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import { createGlobalStyle, ThemeProvider } from 'antd-style'; |
| 3 | + |
| 4 | +describe('createGlobalStyle', () => { |
| 5 | + it('全局样式', async () => { |
| 6 | + const Global = createGlobalStyle` |
| 7 | + .some-class { |
| 8 | + color: pink; |
| 9 | + } |
| 10 | + `; |
| 11 | + |
| 12 | + const { findByTestId } = render( |
| 13 | + <div data-testid={'content'}> |
| 14 | + <div className="some-class">pink txt</div> |
| 15 | + <Global /> |
| 16 | + </div>, |
| 17 | + ); |
| 18 | + |
| 19 | + const item = await findByTestId('content'); |
| 20 | + |
| 21 | + expect(item.firstChild).toHaveStyle({ color: 'pink' }); |
| 22 | + }); |
| 23 | + |
| 24 | + it('包裹 ThemeProvider 后可以获取主题样式', async () => { |
| 25 | + const Global = createGlobalStyle` |
| 26 | + .some-class { |
| 27 | + color: ${(p) => p.theme.colorPrimary}; |
| 28 | + } |
| 29 | + `; |
| 30 | + |
| 31 | + const { findByTestId } = render( |
| 32 | + <div data-testid={'content'}> |
| 33 | + <div className="some-class">pink txt</div> |
| 34 | + <Global /> |
| 35 | + </div>, |
| 36 | + { wrapper: ThemeProvider }, |
| 37 | + ); |
| 38 | + |
| 39 | + const item = await findByTestId('content'); |
| 40 | + |
| 41 | + expect(item.firstChild).toHaveStyle({ color: '#1677FF' }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('不包裹 ThemeProvider 没法获得 token', async () => { |
| 45 | + const Global = createGlobalStyle` |
| 46 | + .some-class { |
| 47 | + color: ${(p) => p.theme.colorPrimary}; |
| 48 | + } |
| 49 | + `; |
| 50 | + |
| 51 | + const { findByTestId } = render( |
| 52 | + <div data-testid={'content'}> |
| 53 | + <div className="some-class">pink txt</div> |
| 54 | + <Global /> |
| 55 | + </div>, |
| 56 | + ); |
| 57 | + |
| 58 | + const item = await findByTestId('content'); |
| 59 | + |
| 60 | + expect(item.firstChild).toHaveStyle({ color: '' }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments