mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-11 11:32:52 +08:00
test: fix image test (#53817)
This commit is contained in:
parent
fb891769b1
commit
116a2c09bd
@ -2,7 +2,6 @@ import * as React from 'react';
|
|||||||
import { useEvent } from 'rc-util';
|
import { useEvent } from 'rc-util';
|
||||||
import raf from 'rc-util/lib/raf';
|
import raf from 'rc-util/lib/raf';
|
||||||
|
|
||||||
import useForceUpdate from '../_util/hooks/useForceUpdate';
|
|
||||||
import { cloneElement } from '../_util/reactNode';
|
import { cloneElement } from '../_util/reactNode';
|
||||||
import type { StatisticProps } from './Statistic';
|
import type { StatisticProps } from './Statistic';
|
||||||
import Statistic from './Statistic';
|
import Statistic from './Statistic';
|
||||||
@ -29,14 +28,15 @@ const StatisticTimer: React.FC<StatisticTimerProps> = (props) => {
|
|||||||
const { value, format = 'HH:mm:ss', onChange, onFinish, type, ...rest } = props;
|
const { value, format = 'HH:mm:ss', onChange, onFinish, type, ...rest } = props;
|
||||||
const down = type === 'countdown';
|
const down = type === 'countdown';
|
||||||
|
|
||||||
const forceUpdate = useForceUpdate();
|
// We reuse state here to do same as `forceUpdate`
|
||||||
|
const [showTime, setShowTime] = React.useState<null | object>(null);
|
||||||
|
|
||||||
// ======================== Update ========================
|
// ======================== Update ========================
|
||||||
const update = useEvent(() => {
|
const update = useEvent(() => {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const timestamp = getTime(value);
|
const timestamp = getTime(value);
|
||||||
|
|
||||||
forceUpdate();
|
setShowTime({});
|
||||||
const timeDiff = !down ? now - timestamp : timestamp - now;
|
const timeDiff = !down ? now - timestamp : timestamp - now;
|
||||||
|
|
||||||
onChange?.(timeDiff);
|
onChange?.(timeDiff);
|
||||||
@ -68,9 +68,13 @@ const StatisticTimer: React.FC<StatisticTimerProps> = (props) => {
|
|||||||
return clear;
|
return clear;
|
||||||
}, [value, down]);
|
}, [value, down]);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
setShowTime({});
|
||||||
|
}, []);
|
||||||
|
|
||||||
// ======================== Format ========================
|
// ======================== Format ========================
|
||||||
const formatter: StatisticProps['formatter'] = (formatValue, config) =>
|
const formatter: StatisticProps['formatter'] = (formatValue, config) =>
|
||||||
formatCounter(formatValue, { ...config, format }, down);
|
showTime ? formatCounter(formatValue, { ...config, format }, down) : '-';
|
||||||
|
|
||||||
const valueRender: StatisticProps['valueRender'] = (node) =>
|
const valueRender: StatisticProps['valueRender'] = (node) =>
|
||||||
cloneElement(node, { title: undefined });
|
cloneElement(node, { title: undefined });
|
||||||
|
@ -420,7 +420,7 @@ exports[`renders components/statistic/demo/timer.tsx correctly 1`] = `
|
|||||||
<span
|
<span
|
||||||
class="ant-statistic-content-value"
|
class="ant-statistic-content-value"
|
||||||
>
|
>
|
||||||
48:00:30
|
-
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -443,7 +443,7 @@ exports[`renders components/statistic/demo/timer.tsx correctly 1`] = `
|
|||||||
<span
|
<span
|
||||||
class="ant-statistic-content-value"
|
class="ant-statistic-content-value"
|
||||||
>
|
>
|
||||||
48:00:30:000
|
-
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -466,7 +466,7 @@ exports[`renders components/statistic/demo/timer.tsx correctly 1`] = `
|
|||||||
<span
|
<span
|
||||||
class="ant-statistic-content-value"
|
class="ant-statistic-content-value"
|
||||||
>
|
>
|
||||||
00:00:10
|
-
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -489,7 +489,7 @@ exports[`renders components/statistic/demo/timer.tsx correctly 1`] = `
|
|||||||
<span
|
<span
|
||||||
class="ant-statistic-content-value"
|
class="ant-statistic-content-value"
|
||||||
>
|
>
|
||||||
47:59:30
|
-
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -512,7 +512,7 @@ exports[`renders components/statistic/demo/timer.tsx correctly 1`] = `
|
|||||||
<span
|
<span
|
||||||
class="ant-statistic-content-value"
|
class="ant-statistic-content-value"
|
||||||
>
|
>
|
||||||
2 天 0 时 0 分 30 秒
|
-
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -535,7 +535,7 @@ exports[`renders components/statistic/demo/timer.tsx correctly 1`] = `
|
|||||||
<span
|
<span
|
||||||
class="ant-statistic-content-value"
|
class="ant-statistic-content-value"
|
||||||
>
|
>
|
||||||
1 天 23 时 59 分 30 秒
|
-
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import { imageDemoTest } from '../../../tests/shared/imageTest';
|
import { imageDemoTest } from '../../../tests/shared/imageTest';
|
||||||
|
|
||||||
describe('Statistic image', () => {
|
describe('Statistic image', () => {
|
||||||
imageDemoTest('statistic', { skip: ['countdown.tsx'] });
|
imageDemoTest('statistic', {
|
||||||
|
ssr: ['timer.tsx'],
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
|
import { renderToString } from 'react-dom/server';
|
||||||
|
|
||||||
import type { CountdownProps } from '..';
|
import type { CountdownProps } from '..';
|
||||||
import Statistic from '..';
|
import Statistic from '..';
|
||||||
@ -200,6 +201,24 @@ describe('Statistic', () => {
|
|||||||
'00:30:01',
|
'00:30:01',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('ssr', async () => {
|
||||||
|
const onChange = jest.fn();
|
||||||
|
const onFinish = jest.fn();
|
||||||
|
|
||||||
|
const html = renderToString(
|
||||||
|
<Statistic.Timer
|
||||||
|
type="countdown"
|
||||||
|
value={Date.now() + 2300}
|
||||||
|
onChange={onChange}
|
||||||
|
onFinish={onFinish}
|
||||||
|
/>,
|
||||||
|
);
|
||||||
|
|
||||||
|
document.body.innerHTML = html;
|
||||||
|
|
||||||
|
expect(document.querySelector('.ant-statistic-content-value')!.textContent).toEqual('-');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Deprecated Countdown', () => {
|
describe('Deprecated Countdown', () => {
|
||||||
|
@ -30,7 +30,7 @@ const themes = {
|
|||||||
|
|
||||||
interface ImageTestOptions {
|
interface ImageTestOptions {
|
||||||
onlyViewport?: boolean;
|
onlyViewport?: boolean;
|
||||||
ssr?: boolean;
|
ssr?: boolean | string[];
|
||||||
openTriggerClassName?: string;
|
openTriggerClassName?: string;
|
||||||
mobile?: boolean;
|
mobile?: boolean;
|
||||||
}
|
}
|
||||||
@ -39,6 +39,7 @@ interface ImageTestOptions {
|
|||||||
export default function imageTest(
|
export default function imageTest(
|
||||||
component: React.ReactElement,
|
component: React.ReactElement,
|
||||||
identifier: string,
|
identifier: string,
|
||||||
|
filename: string,
|
||||||
options: ImageTestOptions,
|
options: ImageTestOptions,
|
||||||
) {
|
) {
|
||||||
let doc: Document;
|
let doc: Document;
|
||||||
@ -159,7 +160,10 @@ export default function imageTest(
|
|||||||
let html: string;
|
let html: string;
|
||||||
let styleStr: string;
|
let styleStr: string;
|
||||||
|
|
||||||
if (options.ssr) {
|
if (
|
||||||
|
options.ssr &&
|
||||||
|
(options.ssr === true || options.ssr.some((demoName) => filename.includes(demoName)))
|
||||||
|
) {
|
||||||
html = ReactDOMServer.renderToString(element);
|
html = ReactDOMServer.renderToString(element);
|
||||||
styleStr = extractStyle(cache) + extractStaticStyle(html).map((item) => item.tag);
|
styleStr = extractStyle(cache) + extractStaticStyle(html).map((item) => item.tag);
|
||||||
} else {
|
} else {
|
||||||
@ -277,7 +281,7 @@ type Options = {
|
|||||||
skip?: boolean | string[];
|
skip?: boolean | string[];
|
||||||
onlyViewport?: boolean | string[];
|
onlyViewport?: boolean | string[];
|
||||||
/** Use SSR render instead. Only used when the third part deps component */
|
/** Use SSR render instead. Only used when the third part deps component */
|
||||||
ssr?: boolean;
|
ssr?: boolean | string[];
|
||||||
/** Open Trigger to check the popup render */
|
/** Open Trigger to check the popup render */
|
||||||
openTriggerClassName?: string;
|
openTriggerClassName?: string;
|
||||||
mobile?: string[];
|
mobile?: string[];
|
||||||
@ -312,7 +316,7 @@ export function imageDemoTest(component: string, options: Options = {}) {
|
|||||||
if (typeof Demo === 'function') {
|
if (typeof Demo === 'function') {
|
||||||
Demo = <Demo />;
|
Demo = <Demo />;
|
||||||
}
|
}
|
||||||
imageTest(Demo, `${component}-${path.basename(file, '.tsx')}`, getTestOption(file));
|
imageTest(Demo, `${component}-${path.basename(file, '.tsx')}`, file, getTestOption(file));
|
||||||
|
|
||||||
// Check if need mobile test
|
// Check if need mobile test
|
||||||
if ((options.mobile || []).some((c) => file.endsWith(c))) {
|
if ((options.mobile || []).some((c) => file.endsWith(c))) {
|
||||||
@ -328,7 +332,7 @@ export function imageDemoTest(component: string, options: Options = {}) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
mobileDemos.forEach(([file, Demo]) => {
|
mobileDemos.forEach(([file, Demo]) => {
|
||||||
imageTest(Demo, `${component}-${path.basename(file, '.tsx')}`, {
|
imageTest(Demo, `${component}-${path.basename(file, '.tsx')}`, file, {
|
||||||
...getTestOption(file),
|
...getTestOption(file),
|
||||||
mobile: true,
|
mobile: true,
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user