mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-04 17:09:46 +08:00
6759887c44
* chore: migrate to vitest * chore: update ci * fix: test correctly * test: support puppeteer * chore: update coverage * chore: update include/exclude * chore: update config * test: update incorrect tests * chore: update script * chore: update * fix: should close browser at the ended * chore: improve * fix: test cause tsc error * fix: eslint error * chore: exclude correctly * test: update snap and fix some tests * chore: update test config * fix: countup.js * fix: incorrect test * chore: update reference * test: update * fix: countup.js * fix: timeout * chore: update site test * fix: fixed countup version * chore: remove unsed code * test: update * test: update demo timeout * test: update timeout * chore: update image test * chore: update threads * fix: image/svg+xml test failed * chore: limits threads * test: update test coverage include * chore: remove jest files * chore: rename jest to vi * chore: update document * chore: fix missing @types/jsdom * chore: update coverage * chore: update snap * fix:watermark test cases are incorrect * feat: update ignore comment * test: fix test case * test: reset body scrollTop * test: clean up * test: use vi * test: update snapshot * test: update snapshot * test: fix dropdown test failed * fix: toHaveStyle cause test fail * test: improve test case * test: fix * fix: color failed, refer to https://github.com/jsdom/jsdom/pull/3560 * test: fix * test: fix * test: fix circular import * test: revert * ci: coverage failed * test: fix c8 ignore comment * chore: incorrect config * chore: fix ignore ci * test: revert svg+xml * test: fix realTimers * feat: rc-trigger should be remove * test: fix some failed test * chore: remove unused deps and configure eslint-plugin-vitest * test: update snap * chore: remove jest * test: fix lint error --------- Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: afc163 <afc163@gmail.com>
189 lines
6.2 KiB
TypeScript
189 lines
6.2 KiB
TypeScript
import dayjs from 'dayjs';
|
|
import MockDate from 'mockdate';
|
|
import React from 'react';
|
|
import Statistic from '..';
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
|
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
|
import { formatTimeStr } from '../utils';
|
|
|
|
describe('Statistic', () => {
|
|
mountTest(Statistic);
|
|
mountTest(Statistic.Countdown);
|
|
rtlTest(Statistic);
|
|
|
|
beforeAll(() => {
|
|
MockDate.set(dayjs('2018-11-28 00:00:00').valueOf());
|
|
});
|
|
|
|
afterAll(() => {
|
|
MockDate.reset();
|
|
});
|
|
|
|
it('`-` is not a number', () => {
|
|
const { container } = render(<Statistic value="-" />);
|
|
expect(container.querySelector('.ant-statistic-content')!.textContent).toEqual('-');
|
|
});
|
|
|
|
it('customize formatter', () => {
|
|
const formatter = vi.fn(() => 93);
|
|
const { container } = render(<Statistic value={1128} formatter={formatter} />);
|
|
expect(formatter).toHaveBeenCalledWith(1128);
|
|
expect(container.querySelector('.ant-statistic-content-value')!.textContent).toEqual('93');
|
|
});
|
|
|
|
it('groupSeparator', () => {
|
|
const { container } = render(<Statistic value={1128} groupSeparator="__TEST__" />);
|
|
expect(container.querySelector('.ant-statistic-content-value')!.textContent).toEqual(
|
|
'1__TEST__128',
|
|
);
|
|
});
|
|
|
|
it('not a number', () => {
|
|
const { container } = render(<Statistic value="bamboo" />);
|
|
expect(container.querySelector('.ant-statistic-content-value')!.textContent).toEqual('bamboo');
|
|
});
|
|
|
|
it('support negative number', () => {
|
|
const { asFragment } = render(
|
|
<Statistic title="Account Balance (CNY)" value={-112893.12345} precision={2} />,
|
|
);
|
|
expect(asFragment().firstChild).toMatchSnapshot();
|
|
});
|
|
|
|
it('allow negative precision', () => {
|
|
[
|
|
[-1, -1112893.1212, '-1,112,893'],
|
|
[-2, -1112893.1212, '-1,112,893'],
|
|
[-3, -1112893.1212, '-1,112,893'],
|
|
[-1, -1112893, '-1,112,893'],
|
|
[-1, 1112893, '1,112,893'],
|
|
].forEach(([precision, value, expectValue]: [number, number, string]) => {
|
|
const { container } = render(<Statistic precision={precision} value={value} />);
|
|
expect(container.querySelector('.ant-statistic-content-value-int')!.textContent).toEqual(
|
|
expectValue,
|
|
);
|
|
expect(container.querySelectorAll('.ant-statistic-content-value-decimal').length).toBe(0);
|
|
});
|
|
});
|
|
|
|
it('loading with skeleton', async () => {
|
|
let loading = false;
|
|
const { container, rerender } = render(
|
|
<Statistic title="Active Users" value={112112} loading={loading} />,
|
|
);
|
|
expect(container.querySelectorAll('.ant-skeleton')).toHaveLength(0);
|
|
expect(container.querySelectorAll('.ant-statistic-content')).toHaveLength(1);
|
|
|
|
loading = true;
|
|
rerender(<Statistic title="Active Users" value={112112} loading={loading} />);
|
|
expect(container.querySelectorAll('.ant-skeleton')).toHaveLength(1);
|
|
expect(container.querySelectorAll('.ant-statistic-content')).toHaveLength(0);
|
|
});
|
|
|
|
describe('Countdown', () => {
|
|
it('render correctly', () => {
|
|
const now = dayjs()
|
|
.add(2, 'd')
|
|
.add(11, 'h')
|
|
.add(28, 'm')
|
|
.add(9, 's')
|
|
.add(3, 'ms')
|
|
.toISOString();
|
|
[
|
|
['H:m:s', '59:28:9'],
|
|
['HH:mm:ss', '59:28:09'],
|
|
['HH:mm:ss:SSS', '59:28:09:003'],
|
|
['DD-HH:mm:ss', '02-11:28:09'],
|
|
].forEach(([format, value]) => {
|
|
const { container } = render(<Statistic.Countdown format={format} value={now} />);
|
|
expect(container.querySelector('.ant-statistic-content-value')!.textContent).toEqual(value);
|
|
});
|
|
});
|
|
|
|
it('time going', async () => {
|
|
vi.useFakeTimers();
|
|
const now = Date.now() + 1000;
|
|
const onFinish = vi.fn();
|
|
|
|
const { unmount } = render(<Statistic.Countdown value={now} onFinish={onFinish} />);
|
|
|
|
await waitFakeTimer(10);
|
|
|
|
unmount();
|
|
expect(onFinish).not.toHaveBeenCalled();
|
|
vi.clearAllTimers();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('responses hover events', () => {
|
|
const onMouseEnter = vi.fn();
|
|
const onMouseLeave = vi.fn();
|
|
const { container } = render(
|
|
<Statistic onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} />,
|
|
);
|
|
fireEvent.mouseEnter(container.firstChild!);
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
fireEvent.mouseLeave(container.firstChild!);
|
|
expect(onMouseLeave).toHaveBeenCalled();
|
|
});
|
|
|
|
it('responses hover events for Countdown', () => {
|
|
const onMouseEnter = vi.fn();
|
|
const onMouseLeave = vi.fn();
|
|
const { container } = render(
|
|
<Statistic.Countdown onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} />,
|
|
);
|
|
fireEvent.mouseEnter(container.firstChild!);
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
fireEvent.mouseLeave(container.firstChild!);
|
|
expect(onMouseLeave).toHaveBeenCalled();
|
|
});
|
|
|
|
describe('time onchange', () => {
|
|
it("called if time has't passed", async () => {
|
|
vi.useFakeTimers();
|
|
const deadline = Date.now() + 10 * 1000;
|
|
let remainingTime;
|
|
|
|
const onChange = (value: number) => {
|
|
remainingTime = value;
|
|
};
|
|
render(<Statistic.Countdown value={deadline} onChange={onChange} />);
|
|
// container.update();
|
|
await waitFakeTimer(100);
|
|
expect(remainingTime).toBeGreaterThan(0);
|
|
vi.clearAllTimers();
|
|
vi.useRealTimers();
|
|
});
|
|
});
|
|
|
|
describe('time finished', () => {
|
|
it('not call if time already passed', () => {
|
|
const now = Date.now() - 1000;
|
|
const onFinish = vi.fn();
|
|
render(<Statistic.Countdown value={now} onFinish={onFinish} />);
|
|
|
|
expect(onFinish).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('called if finished', async () => {
|
|
vi.useFakeTimers();
|
|
const now = Date.now() + 10;
|
|
const onFinish = vi.fn();
|
|
render(<Statistic.Countdown value={now} onFinish={onFinish} />);
|
|
await waitFakeTimer();
|
|
expect(onFinish).toHaveBeenCalled();
|
|
vi.clearAllTimers();
|
|
vi.useRealTimers();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('utils', () => {
|
|
it('format should support escape', () => {
|
|
expect(formatTimeStr(1000 * 60 * 60 * 24, 'D [Day]')).toBe('1 Day');
|
|
});
|
|
});
|
|
});
|