File size: 2,778 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as React from 'react';
import { createCache, StyleProvider } from '@ant-design/cssinjs';
import { SmileOutlined } from '@ant-design/icons';

import ConfigProvider from '..';
import { render } from '../../../tests/utils';
import Button from '../../button';
import Divider from '../../divider';

describe('ConfigProvider.DynamicTheme', () => {
  beforeEach(() => {
    Array.from(document.querySelectorAll('style')).forEach((style) => {
      style.parentNode?.removeChild(style);
    });
  });

  it('customize primary color', () => {
    render(
      <ConfigProvider
        theme={{
          token: {
            colorPrimary: '#f00000',
          },
        }}
      >
        <Button />
      </ConfigProvider>,
    );

    const dynamicStyles = Array.from(document.querySelectorAll('style[data-css-hash]'));

    expect(
      dynamicStyles.some((style) => {
        const { innerHTML } = style;
        return innerHTML.includes('.ant-btn-primary') && innerHTML.includes('background:#f00000');
      }),
    ).toBeTruthy();
  });

  it('not crash on null token', () => {
    expect(() => {
      render(
        <ConfigProvider
          theme={{
            token: null as any,
          }}
        />,
      );
    }).not.toThrow();
  });

  it('should support overriding aliasToken', () => {
    render(
      <ConfigProvider
        theme={{
          token: {
            colorSplit: 'blue',
          },
        }}
      >
        <Divider />
      </ConfigProvider>,
    );

    const dynamicStyles = Array.from(document.querySelectorAll('style[data-css-hash]'));

    expect(
      dynamicStyles.some((style) => {
        const { innerHTML } = style;
        return (
          innerHTML.includes('.ant-divider') && innerHTML.includes('border-block-start:0 blue')
        );
      }),
    ).toBeTruthy();
  });

  it('should support iconPrefixCls', () => {
    const { container } = render(
      <ConfigProvider iconPrefixCls="test-icon">
        <SmileOutlined />
      </ConfigProvider>,
    );

    expect(container.querySelector('.test-icon')).toBeTruthy();

    const dynamicStyles = Array.from(document.querySelectorAll('style[data-css-hash]'));
    expect(
      dynamicStyles.some((style) => {
        const { innerHTML } = style;
        return innerHTML.includes('.test-icon');
      }),
    ).toBeTruthy();
  });

  it('layer should affect icon', () => {
    render(
      <StyleProvider layer cache={createCache()}>
        <ConfigProvider>
          <SmileOutlined />
        </ConfigProvider>
      </StyleProvider>,
    );

    const styles = Array.from(document.querySelectorAll('style'));

    expect(styles.length).toBeTruthy();
    styles.forEach((style) => {
      expect(style.innerHTML).toContain('@layer antd');
    });
  });
});