2019-01-09 20:38:09 +08:00
|
|
|
import React from 'react';
|
|
|
|
import MockDate from 'mockdate';
|
|
|
|
import moment from 'moment';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Statistic from '..';
|
2019-07-24 15:07:10 +08:00
|
|
|
import { formatTimeStr } from '../utils';
|
2019-07-31 18:06:31 +08:00
|
|
|
import { sleep } from '../../../tests/utils';
|
2019-08-26 22:53:20 +08:00
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2019-01-09 20:38:09 +08:00
|
|
|
|
|
|
|
describe('Statistic', () => {
|
2019-08-26 22:53:20 +08:00
|
|
|
mountTest(Statistic);
|
|
|
|
mountTest(Statistic.Countdown);
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Statistic);
|
2019-08-26 22:53:20 +08:00
|
|
|
|
2019-01-09 20:38:09 +08:00
|
|
|
beforeAll(() => {
|
2020-05-21 23:27:02 +08:00
|
|
|
MockDate.set(moment('2018-11-28 00:00:00').valueOf());
|
2019-01-09 20:38:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
MockDate.reset();
|
|
|
|
});
|
|
|
|
|
2020-04-06 11:19:44 +08:00
|
|
|
it('`-` is not a number', () => {
|
|
|
|
const wrapper = mount(<Statistic value="-" />);
|
|
|
|
expect(wrapper.find('.ant-statistic-content').text()).toEqual('-');
|
|
|
|
});
|
|
|
|
|
2019-01-09 20:38:09 +08:00
|
|
|
it('customize formatter', () => {
|
|
|
|
const formatter = jest.fn(() => 93);
|
|
|
|
const wrapper = mount(<Statistic value={1128} formatter={formatter} />);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(formatter).toHaveBeenCalledWith(1128);
|
2019-01-09 20:38:09 +08:00
|
|
|
expect(wrapper.find('.ant-statistic-content-value').text()).toEqual('93');
|
|
|
|
});
|
|
|
|
|
2019-01-10 17:04:14 +08:00
|
|
|
it('groupSeparator', () => {
|
|
|
|
const wrapper = mount(<Statistic value={1128} groupSeparator="__TEST__" />);
|
|
|
|
expect(wrapper.find('.ant-statistic-content-value').text()).toEqual('1__TEST__128');
|
|
|
|
});
|
|
|
|
|
2019-01-09 20:38:09 +08:00
|
|
|
it('not a number', () => {
|
|
|
|
const wrapper = mount(<Statistic value="bamboo" />);
|
|
|
|
expect(wrapper.find('.ant-statistic-content-value').text()).toEqual('bamboo');
|
|
|
|
});
|
|
|
|
|
2019-02-02 21:55:46 +08:00
|
|
|
it('support negetive number', () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<Statistic title="Account Balance (CNY)" value={-112893.12345} precision={2} />,
|
|
|
|
);
|
|
|
|
expect(wrapper.render()).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2020-10-23 10:09:31 +08:00
|
|
|
it('loading with skeleton', async () => {
|
|
|
|
let loading = false;
|
|
|
|
const wrapper = mount(<Statistic title="Active Users" value={112112} loading={loading} />);
|
|
|
|
expect(wrapper.find('.ant-skeleton')).toHaveLength(0);
|
|
|
|
expect(wrapper.find('.ant-statistic-content')).toHaveLength(1);
|
|
|
|
|
|
|
|
loading = true;
|
|
|
|
wrapper.setProps({ loading });
|
|
|
|
expect(wrapper.find('.ant-skeleton')).toHaveLength(1);
|
|
|
|
expect(wrapper.find('.ant-statistic-content')).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
2019-01-09 20:38:09 +08:00
|
|
|
describe('Countdown', () => {
|
|
|
|
it('render correctly', () => {
|
2020-04-06 11:19:44 +08:00
|
|
|
const now = moment().add(2, 'd').add(11, 'h').add(28, 'm').add(9, 's').add(3, 'ms');
|
2019-01-09 20:38:09 +08:00
|
|
|
|
|
|
|
[
|
|
|
|
['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 wrapper = mount(<Statistic.Countdown format={format} value={now} />);
|
|
|
|
expect(wrapper.find('.ant-statistic-content-value').text()).toEqual(value);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('time going', async () => {
|
|
|
|
const now = Date.now() + 1000;
|
2019-02-12 17:16:49 +08:00
|
|
|
const onFinish = jest.fn();
|
|
|
|
const wrapper = mount(<Statistic.Countdown value={now} onFinish={onFinish} />);
|
2019-01-09 20:38:09 +08:00
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
// setInterval should work
|
|
|
|
const instance = wrapper.instance();
|
|
|
|
expect(instance.countdownId).not.toBe(undefined);
|
|
|
|
|
2019-07-31 18:06:31 +08:00
|
|
|
await sleep(10);
|
2019-01-09 20:38:09 +08:00
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
expect(instance.countdownId).toBe(undefined);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onFinish).not.toHaveBeenCalled();
|
2019-02-12 17:16:49 +08:00
|
|
|
});
|
|
|
|
|
2020-06-06 15:25:47 +08:00
|
|
|
it('responses hover events', () => {
|
|
|
|
const onMouseEnter = jest.fn();
|
|
|
|
const onMouseLeave = jest.fn();
|
|
|
|
const wrapper = mount(<Statistic onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} />);
|
|
|
|
wrapper.simulate('mouseenter');
|
|
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
|
|
wrapper.simulate('mouseleave');
|
|
|
|
expect(onMouseLeave).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('responses hover events for Countdown', () => {
|
|
|
|
const onMouseEnter = jest.fn();
|
|
|
|
const onMouseLeave = jest.fn();
|
2020-10-21 10:35:06 +08:00
|
|
|
const wrapper = mount(
|
|
|
|
<Statistic.Countdown onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} />,
|
|
|
|
);
|
2020-06-06 15:25:47 +08:00
|
|
|
wrapper.simulate('mouseenter');
|
|
|
|
expect(onMouseEnter).toHaveBeenCalled();
|
|
|
|
wrapper.simulate('mouseleave');
|
|
|
|
expect(onMouseLeave).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2019-02-12 17:16:49 +08:00
|
|
|
describe('time finished', () => {
|
|
|
|
it('not call if time already passed', () => {
|
|
|
|
const now = Date.now() - 1000;
|
|
|
|
|
|
|
|
const onFinish = jest.fn();
|
|
|
|
const wrapper = mount(<Statistic.Countdown value={now} onFinish={onFinish} />);
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const instance = wrapper.instance();
|
|
|
|
expect(instance.countdownId).toBe(undefined);
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onFinish).not.toHaveBeenCalled();
|
2019-02-12 17:16:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('called if finished', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const now = Date.now() + 10;
|
|
|
|
const onFinish = jest.fn();
|
|
|
|
const wrapper = mount(<Statistic.Countdown value={now} onFinish={onFinish} />);
|
|
|
|
wrapper.update();
|
|
|
|
|
2020-05-21 23:27:02 +08:00
|
|
|
MockDate.set(moment('2019-11-28 00:00:00').valueOf());
|
2019-02-12 17:16:49 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
|
2019-04-03 15:54:26 +08:00
|
|
|
expect(onFinish).toHaveBeenCalled();
|
2019-02-12 17:16:49 +08:00
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
2019-01-09 20:38:09 +08:00
|
|
|
});
|
|
|
|
});
|
2019-07-24 15:07:10 +08:00
|
|
|
|
|
|
|
describe('utils', () => {
|
|
|
|
it('format should support escape', () => {
|
|
|
|
expect(formatTimeStr(1000 * 60 * 60 * 24, 'D [Day]')).toBe('1 Day');
|
|
|
|
});
|
|
|
|
});
|
2019-01-09 20:38:09 +08:00
|
|
|
});
|