|
| 1 | +import React from 'react'; |
| 2 | +import { render, renderHook } from '@testing-library/react'; |
| 3 | + |
| 4 | +import { genStyleUtils } from '../src' |
| 5 | +import type { CSSVarRegisterProps, SubStyleComponentProps } from '../src/util/genStyleUtils'; |
| 6 | + |
| 7 | +describe('genStyleUtils', () => { |
| 8 | + const mockConfig = { |
| 9 | + usePrefix: jest.fn().mockReturnValue({ |
| 10 | + rootPrefixCls: 'ant', |
| 11 | + iconPrefixCls: 'anticon', |
| 12 | + }), |
| 13 | + useToken: jest.fn().mockReturnValue({ |
| 14 | + theme: {}, |
| 15 | + realToken: {}, |
| 16 | + hashId: 'hash', |
| 17 | + token: {}, |
| 18 | + cssVar: {}, |
| 19 | + }), |
| 20 | + useCSP: jest.fn().mockReturnValue({ nonce: 'nonce' }), |
| 21 | + getResetStyles: jest.fn().mockReturnValue([]), |
| 22 | + }; |
| 23 | + |
| 24 | + const { genStyleHooks, genSubStyleComponent, genComponentStyleHook } = genStyleUtils(mockConfig); |
| 25 | + |
| 26 | + describe('genStyleHooks', () => { |
| 27 | + it('should generate style hooks', () => { |
| 28 | + const component = 'TestComponent'; |
| 29 | + const styleFn = jest.fn(); |
| 30 | + const getDefaultToken = jest.fn(); |
| 31 | + const hooks = genStyleHooks(component, styleFn, getDefaultToken); |
| 32 | + |
| 33 | + expect(hooks).toBeInstanceOf(Function); |
| 34 | + |
| 35 | + const { |
| 36 | + result: { current } |
| 37 | + } = renderHook(() => hooks('test-prefix')); |
| 38 | + expect(current).toBeInstanceOf(Array); |
| 39 | + expect(current).toHaveLength(3); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('genSubStyleComponent', () => { |
| 44 | + it('should generate sub style component', () => { |
| 45 | + const component = 'TestComponent'; |
| 46 | + const styleFn = jest.fn(); |
| 47 | + const getDefaultToken = jest.fn(); |
| 48 | + const StyledComponent = genSubStyleComponent(component, styleFn, getDefaultToken); |
| 49 | + |
| 50 | + const { container } = render(<StyledComponent prefixCls="test-prefix" rootCls="test-root" />); |
| 51 | + expect(container).toBeEmptyDOMElement(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('genComponentStyleHook', () => { |
| 56 | + it('should generate component style hook', () => { |
| 57 | + const component = 'TestComponent'; |
| 58 | + const styleFn = jest.fn(); |
| 59 | + const getDefaultToken = jest.fn(); |
| 60 | + const hook = genComponentStyleHook(component, styleFn, getDefaultToken); |
| 61 | + |
| 62 | + const TestComponent: React.FC<SubStyleComponentProps> = ({ prefixCls, rootCls }) => { |
| 63 | + hook(prefixCls, rootCls); |
| 64 | + return <div data-testid="test-component">Test</div>; |
| 65 | + }; |
| 66 | + |
| 67 | + const { getByTestId } = render(<TestComponent prefixCls="test-prefix" rootCls="test-root" />); |
| 68 | + expect(getByTestId('test-component')).toHaveTextContent('Test'); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('CSSVarRegister', () => { |
| 73 | + it('should render CSSVarRegister component', () => { |
| 74 | + const CSSVarRegister: React.FC<CSSVarRegisterProps> = ({ |
| 75 | + rootCls, |
| 76 | + cssVar = {}, |
| 77 | + }) => { |
| 78 | + return <div data-testid={rootCls}>{cssVar.prefix}</div>; |
| 79 | + }; |
| 80 | + |
| 81 | + const { getByTestId } = render(<CSSVarRegister rootCls="test-root" cssVar={{ prefix: 'test-prefix' }} component="test" />); |
| 82 | + expect(getByTestId('test-root')).toHaveTextContent('test-prefix'); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments