2023-03-27 23:00:56 +08:00
|
|
|
import { createCache, StyleProvider } from '@ant-design/cssinjs';
|
2021-03-19 16:23:25 +08:00
|
|
|
import { SmileOutlined } from '@ant-design/icons';
|
|
|
|
import IconContext from '@ant-design/icons/lib/components/Context';
|
2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2021-03-19 16:23:25 +08:00
|
|
|
import ConfigProvider from '..';
|
2023-03-27 23:00:56 +08:00
|
|
|
import { render } from '../../../tests/utils';
|
|
|
|
import Button from '../../button';
|
2021-03-19 16:23:25 +08:00
|
|
|
|
|
|
|
describe('ConfigProvider.Icon', () => {
|
|
|
|
beforeEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
// eslint-disable-next-line jest/no-standalone-expect
|
2021-03-19 16:23:25 +08:00
|
|
|
expect(document.querySelectorAll('style')).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-11-19 13:47:33 +08:00
|
|
|
document.querySelectorAll('style').forEach((style) => {
|
2022-08-22 22:54:38 +08:00
|
|
|
style.parentNode?.removeChild(style);
|
2021-03-19 16:23:25 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-08 14:01:53 +08:00
|
|
|
describe('csp', () => {
|
|
|
|
it('raw', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
render(
|
2022-03-08 14:01:53 +08:00
|
|
|
<ConfigProvider csp={{ nonce: 'little' }}>
|
|
|
|
<SmileOutlined />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
const styleNode = document.querySelector('style');
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(styleNode?.nonce).toEqual('little');
|
2022-03-08 14:01:53 +08:00
|
|
|
});
|
2021-03-19 16:23:25 +08:00
|
|
|
|
2022-03-08 14:01:53 +08:00
|
|
|
it('mix with iconPrefixCls', () => {
|
2022-08-22 22:54:38 +08:00
|
|
|
const { container } = render(
|
2022-03-08 14:01:53 +08:00
|
|
|
<ConfigProvider iconPrefixCls="bamboo" csp={{ nonce: 'light' }}>
|
|
|
|
<SmileOutlined />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
const styleNode = document.querySelector('style');
|
|
|
|
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(container.querySelector('.bamboo-smile')).toBeTruthy();
|
|
|
|
expect(styleNode?.nonce).toEqual('light');
|
2022-03-08 14:01:53 +08:00
|
|
|
});
|
2021-03-19 16:23:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('nest', () => {
|
|
|
|
const Checker = () => {
|
|
|
|
const { csp } = React.useContext(IconContext);
|
2022-08-22 22:54:38 +08:00
|
|
|
return <div id="csp">{csp?.nonce}</div>;
|
2021-03-19 16:23:25 +08:00
|
|
|
};
|
|
|
|
|
2022-08-22 22:54:38 +08:00
|
|
|
const { container } = render(
|
2021-03-19 16:23:25 +08:00
|
|
|
<ConfigProvider iconPrefixCls="bamboo" csp={{ nonce: 'light' }}>
|
|
|
|
<ConfigProvider>
|
|
|
|
<SmileOutlined />
|
|
|
|
<Checker />
|
|
|
|
</ConfigProvider>
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
const styleNode = document.querySelector('style');
|
|
|
|
|
2022-08-22 22:54:38 +08:00
|
|
|
expect(container.querySelector('.bamboo-smile')).toBeTruthy();
|
|
|
|
expect(styleNode?.nonce).toEqual('light');
|
|
|
|
expect(container.querySelector('#csp')?.innerHTML).toEqual('light');
|
2021-03-19 16:23:25 +08:00
|
|
|
});
|
2023-03-27 23:00:56 +08:00
|
|
|
|
|
|
|
it('cssinjs should support nonce', () => {
|
|
|
|
render(
|
|
|
|
<StyleProvider cache={createCache()}>
|
|
|
|
<ConfigProvider csp={{ nonce: 'bamboo' }}>
|
|
|
|
<Button />
|
|
|
|
</ConfigProvider>
|
|
|
|
</StyleProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
const styleList = Array.from(document.querySelectorAll('style'));
|
|
|
|
|
|
|
|
expect(styleList.length).toBeTruthy();
|
|
|
|
styleList.forEach((style) => {
|
|
|
|
expect(style.nonce).toEqual('bamboo');
|
|
|
|
});
|
|
|
|
});
|
2023-10-13 23:14:49 +08:00
|
|
|
|
|
|
|
it('nonce applies to all style tags', () => {
|
|
|
|
render(
|
|
|
|
<ConfigProvider csp={{ nonce: 'bamboo' }} theme={{ token: { borderRadius: 2 } }}>
|
|
|
|
<Button />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
const styleNodes = document.querySelectorAll('style');
|
|
|
|
|
|
|
|
styleNodes.forEach((node) => {
|
|
|
|
expect(node?.nonce).toEqual('bamboo');
|
|
|
|
});
|
|
|
|
});
|
2021-03-19 16:23:25 +08:00
|
|
|
});
|