2022-06-28 15:38:30 +08:00
|
|
|
import { Theme } from '@ant-design/cssinjs';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-04-23 15:42:36 +08:00
|
|
|
import theme from '..';
|
2022-06-28 15:38:30 +08:00
|
|
|
import { render, renderHook } from '../../../tests/utils';
|
|
|
|
import ConfigProvider from '../../config-provider';
|
2023-06-05 17:45:33 +08:00
|
|
|
import type { ThemeConfig } from '../../config-provider/context';
|
2022-12-22 10:11:08 +08:00
|
|
|
import Row from '../../row';
|
2023-04-23 15:42:36 +08:00
|
|
|
import genRadius from '../themes/shared/genRadius';
|
2022-06-29 20:34:00 +08:00
|
|
|
|
|
|
|
const { useToken } = theme;
|
2022-06-28 15:38:30 +08:00
|
|
|
|
|
|
|
describe('Theme', () => {
|
2023-06-26 11:23:50 +08:00
|
|
|
const getHookToken = (config?: ThemeConfig) => {
|
|
|
|
let token: any;
|
|
|
|
const Demo = () => {
|
|
|
|
const { token: hookToken } = useToken();
|
|
|
|
token = hookToken;
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
render(
|
|
|
|
<ConfigProvider theme={config}>
|
|
|
|
<Demo />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
delete token._hashId;
|
|
|
|
delete token._tokenKey;
|
|
|
|
return token;
|
|
|
|
};
|
|
|
|
|
2022-06-28 15:38:30 +08:00
|
|
|
it('useTheme', () => {
|
2022-08-08 19:08:43 +08:00
|
|
|
const { result } = renderHook(() => useToken());
|
2022-06-28 15:38:30 +08:00
|
|
|
|
2022-08-08 19:08:43 +08:00
|
|
|
expect(result.current!.theme instanceof Theme).toBeTruthy();
|
|
|
|
expect(result.current!.hashId).toBeFalsy();
|
|
|
|
expect(result.current!.token).toEqual(
|
2022-06-28 15:38:30 +08:00
|
|
|
expect.objectContaining({
|
2022-08-26 16:05:16 +08:00
|
|
|
colorPrimary: '#1677ff',
|
2022-06-28 15:38:30 +08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ConfigProvider with seed', () => {
|
|
|
|
const Demo = React.forwardRef((_, ref: any) => {
|
2022-06-29 20:34:00 +08:00
|
|
|
const themeObj = useToken();
|
2022-06-28 15:38:30 +08:00
|
|
|
ref.current = themeObj;
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2022-06-29 20:34:00 +08:00
|
|
|
const themeRef = React.createRef<ReturnType<typeof useToken>>();
|
2022-06-28 15:38:30 +08:00
|
|
|
render(
|
|
|
|
<ConfigProvider
|
|
|
|
theme={{
|
|
|
|
token: {
|
2022-07-20 18:35:09 +08:00
|
|
|
colorPrimary: '#ff0000',
|
2022-06-28 15:38:30 +08:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Demo ref={themeRef} />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(themeRef.current!.token).toEqual(
|
|
|
|
expect.objectContaining({
|
2022-07-15 19:27:29 +08:00
|
|
|
colorPrimary: '#ff0000',
|
2022-06-28 15:38:30 +08:00
|
|
|
colorPrimaryHover: '#ff3029', // It's safe to modify if theme logic changed
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2022-09-01 20:09:32 +08:00
|
|
|
|
|
|
|
describe('radius should be computed as expected', () => {
|
|
|
|
const radiusGroup = {
|
2022-11-01 15:06:38 +08:00
|
|
|
0: {
|
|
|
|
borderRadius: 0,
|
|
|
|
borderRadiusLG: 0,
|
|
|
|
borderRadiusSM: 0,
|
|
|
|
borderRadiusXS: 0,
|
|
|
|
borderRadiusOuter: 0,
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
borderRadius: 2,
|
|
|
|
borderRadiusLG: 2,
|
|
|
|
borderRadiusSM: 2,
|
|
|
|
borderRadiusXS: 1,
|
|
|
|
borderRadiusOuter: 2,
|
|
|
|
},
|
|
|
|
4: {
|
|
|
|
borderRadius: 4,
|
|
|
|
borderRadiusLG: 4,
|
|
|
|
borderRadiusSM: 4,
|
|
|
|
borderRadiusXS: 1,
|
|
|
|
borderRadiusOuter: 4,
|
|
|
|
},
|
|
|
|
5: {
|
|
|
|
borderRadius: 5,
|
|
|
|
borderRadiusLG: 6,
|
|
|
|
borderRadiusSM: 4,
|
|
|
|
borderRadiusXS: 1,
|
|
|
|
borderRadiusOuter: 4,
|
|
|
|
},
|
|
|
|
6: {
|
|
|
|
borderRadius: 6,
|
|
|
|
borderRadiusLG: 8,
|
|
|
|
borderRadiusSM: 4,
|
|
|
|
borderRadiusXS: 2,
|
|
|
|
borderRadiusOuter: 4,
|
|
|
|
},
|
|
|
|
7: {
|
|
|
|
borderRadius: 7,
|
|
|
|
borderRadiusLG: 9,
|
|
|
|
borderRadiusSM: 5,
|
|
|
|
borderRadiusXS: 2,
|
|
|
|
borderRadiusOuter: 4,
|
|
|
|
},
|
|
|
|
8: {
|
|
|
|
borderRadius: 8,
|
|
|
|
borderRadiusLG: 10,
|
|
|
|
borderRadiusSM: 6,
|
|
|
|
borderRadiusXS: 2,
|
|
|
|
borderRadiusOuter: 6,
|
|
|
|
},
|
|
|
|
10: {
|
|
|
|
borderRadius: 10,
|
|
|
|
borderRadiusLG: 12,
|
|
|
|
borderRadiusSM: 6,
|
|
|
|
borderRadiusXS: 2,
|
|
|
|
borderRadiusOuter: 6,
|
|
|
|
},
|
|
|
|
12: {
|
|
|
|
borderRadius: 12,
|
|
|
|
borderRadiusLG: 14,
|
|
|
|
borderRadiusSM: 6,
|
|
|
|
borderRadiusXS: 2,
|
|
|
|
borderRadiusOuter: 6,
|
|
|
|
},
|
|
|
|
14: {
|
|
|
|
borderRadius: 14,
|
|
|
|
borderRadiusLG: 16,
|
|
|
|
borderRadiusSM: 7,
|
|
|
|
borderRadiusXS: 2,
|
|
|
|
borderRadiusOuter: 6,
|
|
|
|
},
|
|
|
|
16: {
|
|
|
|
borderRadius: 16,
|
|
|
|
borderRadiusLG: 16,
|
|
|
|
borderRadiusSM: 8,
|
|
|
|
borderRadiusXS: 2,
|
|
|
|
borderRadiusOuter: 6,
|
|
|
|
},
|
|
|
|
20: {
|
|
|
|
borderRadius: 16,
|
|
|
|
borderRadiusLG: 16,
|
|
|
|
borderRadiusSM: 8,
|
|
|
|
borderRadiusXS: 2,
|
|
|
|
borderRadiusOuter: 6,
|
|
|
|
},
|
2022-09-01 20:09:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.entries(radiusGroup).forEach(([base, result]) => {
|
|
|
|
it(`base ${base}`, () => {
|
|
|
|
expect(genRadius(Number(base))).toMatchObject(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-12-22 10:11:08 +08:00
|
|
|
|
|
|
|
const rowShouldThrow = (screens: any, error: string) => {
|
|
|
|
const demoRender = () =>
|
|
|
|
render(
|
|
|
|
<ConfigProvider theme={{ token: screens }}>
|
|
|
|
<Row />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
expect(demoRender).toThrow(error);
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('invalid breakpoints order values should raise an error', () => {
|
|
|
|
const tests: Array<[string, number, string]> = [
|
|
|
|
['screenXS', 1000, 'screenXSMax<=screenSMMin fails : !(1010<=576)'],
|
|
|
|
['screenSM', 1000, 'screenSMMax<=screenMDMin fails : !(1010<=768)'],
|
|
|
|
['screenMD', 1000, 'screenMDMax<=screenLGMin fails : !(1010<=992)'],
|
|
|
|
['screenLG', 2000, 'screenLGMax<=screenXLMin fails : !(2010<=1200)'],
|
|
|
|
['screenXL', 2000, 'screenXLMax<=screenXXLMin fails : !(2010<=1600)'],
|
|
|
|
];
|
|
|
|
|
|
|
|
tests.forEach(([screen, value, error]: any) => {
|
|
|
|
it(`Screen ${screen} is too big`, () => {
|
|
|
|
rowShouldThrow(
|
|
|
|
{ [screen]: value, [`${screen}Min`]: value, [`${screen}Max`]: value + 10 },
|
|
|
|
error,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('invalid breakpoints MIN<=BP values should raise an error', () => {
|
|
|
|
const tests: Array<[string, number, string]> = [
|
|
|
|
['screenXSMin', 1000, 'screenXSMin<=screenXS fails : !(1000<=480)'],
|
|
|
|
['screenSMMin', 1000, 'screenSMMin<=screenSM fails : !(1000<=576)'],
|
|
|
|
['screenMDMin', 1000, 'screenMDMin<=screenMD fails : !(1000<=768)'],
|
|
|
|
['screenLGMin', 2000, 'screenLGMin<=screenLG fails : !(2000<=992)'],
|
|
|
|
['screenXLMin', 2000, 'screenXLMin<=screenXL fails : !(2000<=1200)'],
|
|
|
|
['screenXXLMin', 2000, 'screenXXLMin<=screenXXL fails : !(2000<=1600)'],
|
|
|
|
];
|
|
|
|
|
|
|
|
tests.forEach(([screen, value, error]: any) => {
|
|
|
|
it(`Screen ${screen}Min is too big regarding ${screen}`, () => {
|
|
|
|
rowShouldThrow({ [screen]: value }, error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('invalid breakpoints BP<=MAX values should raise an error', () => {
|
|
|
|
const tests: Array<[string, number, string]> = [
|
|
|
|
['screenXS', 1000, 'screenXS<=screenXSMax fails : !(1000<=575)'],
|
|
|
|
['screenSM', 1000, 'screenSM<=screenSMMax fails : !(1000<=767)'],
|
|
|
|
['screenMD', 1000, 'screenMD<=screenMDMax fails : !(1000<=991)'],
|
|
|
|
['screenLG', 2000, 'screenLG<=screenLGMax fails : !(2000<=1199)'],
|
|
|
|
['screenXL', 2000, 'screenXL<=screenXLMax fails : !(2000<=1599)'],
|
|
|
|
];
|
|
|
|
|
|
|
|
tests.forEach(([screen, value, error]: any) => {
|
|
|
|
it(`Screen ${screen} is too big regarding ${screen}Max`, () => {
|
|
|
|
rowShouldThrow({ [screen]: value }, error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-04-23 15:42:36 +08:00
|
|
|
|
|
|
|
it('motion false token', () => {
|
|
|
|
const Shower = () => {
|
|
|
|
const { token } = useToken();
|
|
|
|
|
|
|
|
return <div className="duration">{token.motionDurationSlow}</div>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const { container } = render(
|
|
|
|
<ConfigProvider theme={{ token: { motion: false } }}>
|
|
|
|
<Shower />
|
|
|
|
</ConfigProvider>,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(container.querySelector('.duration')?.textContent).toEqual('0s');
|
|
|
|
});
|
2023-06-05 17:45:33 +08:00
|
|
|
|
|
|
|
describe('getDesignToken', () => {
|
|
|
|
it('default', () => {
|
|
|
|
const token = theme.getDesignToken();
|
|
|
|
const hookToken = getHookToken();
|
|
|
|
expect(token).toEqual(hookToken);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with custom token', () => {
|
|
|
|
const config: ThemeConfig = {
|
|
|
|
token: {
|
|
|
|
colorPrimary: '#189cff',
|
|
|
|
borderRadius: 8,
|
|
|
|
fontSizeLG: 20,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const token = theme.getDesignToken(config);
|
|
|
|
const hookToken = getHookToken(config);
|
|
|
|
expect(token).toEqual(hookToken);
|
|
|
|
expect(token.colorPrimary).toEqual('#189cff');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('with custom algorithm', () => {
|
|
|
|
const config: ThemeConfig = {
|
|
|
|
token: {
|
|
|
|
colorPrimary: '#1677ff',
|
|
|
|
borderRadius: 8,
|
|
|
|
fontSizeLG: 20,
|
|
|
|
},
|
|
|
|
algorithm: [theme.darkAlgorithm, theme.compactAlgorithm],
|
|
|
|
};
|
|
|
|
const token = theme.getDesignToken(config);
|
|
|
|
const hookToken = getHookToken(config);
|
|
|
|
expect(token).toEqual(hookToken);
|
|
|
|
expect(token.colorPrimary).toEqual('#1668dc');
|
|
|
|
});
|
|
|
|
});
|
2023-06-26 11:23:50 +08:00
|
|
|
|
|
|
|
describe('colorLink', () => {
|
|
|
|
it('should follow colorPrimary by default', () => {
|
|
|
|
const token = getHookToken();
|
|
|
|
expect(token.colorLink).toEqual(token.colorInfo);
|
|
|
|
expect(token.colorLinkHover).toEqual(token.colorInfoHover);
|
|
|
|
expect(token.colorLinkActive).toEqual(token.colorInfoActive);
|
|
|
|
|
|
|
|
const token2 = getHookToken({ token: { colorPrimary: '#189cff' } });
|
|
|
|
expect(token2.colorLink).toEqual(token2.colorInfo);
|
|
|
|
expect(token2.colorLinkHover).toEqual(token2.colorInfoHover);
|
|
|
|
expect(token2.colorLinkActive).toEqual(token2.colorInfoActive);
|
|
|
|
|
|
|
|
const token3 = getHookToken({ algorithm: [theme.darkAlgorithm] });
|
|
|
|
expect(token3.colorLink).toEqual(token3.colorInfo);
|
|
|
|
expect(token3.colorLinkHover).toEqual(token3.colorInfoHover);
|
|
|
|
expect(token3.colorLinkActive).toEqual(token3.colorInfoActive);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be calculated correctly', () => {
|
|
|
|
const token = getHookToken({ token: { colorLink: '#189cff' } });
|
|
|
|
expect(token.colorLink).toEqual('#189cff');
|
|
|
|
expect(token.colorLinkHover).toEqual('#69c8ff');
|
|
|
|
expect(token.colorLinkActive).toEqual('#0978d9');
|
|
|
|
});
|
|
|
|
});
|
2022-06-28 15:38:30 +08:00
|
|
|
});
|