2021-10-15 11:19:14 +08:00
|
|
|
/* eslint-disable class-methods-use-this */
|
2019-05-27 18:53:44 +08:00
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2023-05-28 21:32:45 +08:00
|
|
|
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import { isStyleSupport } from '../styleChecker';
|
2022-11-19 13:45:56 +08:00
|
|
|
import throttleByAnimationFrame from '../throttleByAnimationFrame';
|
2019-05-27 18:53:44 +08:00
|
|
|
import TransButton from '../transButton';
|
2017-02-08 14:44:43 +08:00
|
|
|
|
|
|
|
describe('Test utils function', () => {
|
2020-09-21 18:32:24 +08:00
|
|
|
describe('throttle', () => {
|
2022-10-12 22:46:16 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllTimers();
|
|
|
|
});
|
|
|
|
|
2020-09-21 18:32:24 +08:00
|
|
|
it('throttle function should work', async () => {
|
|
|
|
const callback = jest.fn();
|
|
|
|
const throttled = throttleByAnimationFrame(callback);
|
|
|
|
expect(callback).not.toHaveBeenCalled();
|
2017-02-08 14:44:43 +08:00
|
|
|
|
2020-09-21 18:32:24 +08:00
|
|
|
throttled();
|
|
|
|
throttled();
|
2022-10-12 22:46:16 +08:00
|
|
|
await waitFakeTimer();
|
2017-02-08 14:44:43 +08:00
|
|
|
|
2020-09-21 18:32:24 +08:00
|
|
|
expect(callback).toHaveBeenCalled();
|
|
|
|
expect(callback.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throttle function should be canceled', async () => {
|
|
|
|
const callback = jest.fn();
|
|
|
|
const throttled = throttleByAnimationFrame(callback);
|
2017-02-08 14:44:43 +08:00
|
|
|
|
2020-09-21 18:32:24 +08:00
|
|
|
throttled();
|
|
|
|
throttled.cancel();
|
2022-10-12 22:46:16 +08:00
|
|
|
await waitFakeTimer();
|
2017-02-08 14:44:43 +08:00
|
|
|
|
2020-09-21 18:32:24 +08:00
|
|
|
expect(callback).not.toHaveBeenCalled();
|
|
|
|
});
|
2017-02-08 14:44:43 +08:00
|
|
|
});
|
2018-06-12 03:18:41 +08:00
|
|
|
|
2019-05-27 18:53:44 +08:00
|
|
|
describe('TransButton', () => {
|
|
|
|
it('can be focus/blur', () => {
|
2022-11-11 18:15:02 +08:00
|
|
|
const ref = React.createRef<HTMLDivElement>();
|
2022-08-21 23:25:00 +08:00
|
|
|
render(<TransButton ref={ref}>TransButton</TransButton>);
|
|
|
|
expect(typeof ref.current?.focus).toBe('function');
|
|
|
|
expect(typeof ref.current?.blur).toBe('function');
|
2019-05-27 18:53:44 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should trigger onClick when press enter', () => {
|
|
|
|
const onClick = jest.fn();
|
2022-08-21 23:25:00 +08:00
|
|
|
|
|
|
|
const { container } = render(<TransButton onClick={onClick}>TransButton</TransButton>);
|
|
|
|
|
|
|
|
// callback should trigger
|
|
|
|
fireEvent.keyUp(container.querySelector('div')!, { keyCode: KeyCode.ENTER });
|
|
|
|
expect(onClick).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
// callback should not trigger
|
|
|
|
fireEvent.keyDown(container.querySelector('div')!, { keyCode: KeyCode.ENTER });
|
|
|
|
expect(onClick).toHaveBeenCalledTimes(1);
|
2019-05-27 18:53:44 +08:00
|
|
|
});
|
|
|
|
});
|
2020-09-21 18:32:24 +08:00
|
|
|
|
|
|
|
describe('style', () => {
|
|
|
|
it('isStyleSupport', () => {
|
|
|
|
expect(isStyleSupport('color')).toBe(true);
|
|
|
|
expect(isStyleSupport('not-existed')).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('isStyleSupport return false in service side', () => {
|
|
|
|
const spy = jest
|
|
|
|
.spyOn(window.document, 'documentElement', 'get')
|
2022-08-21 23:25:00 +08:00
|
|
|
.mockImplementation(() => undefined as unknown as HTMLElement);
|
2020-09-21 18:32:24 +08:00
|
|
|
expect(isStyleSupport('color')).toBe(false);
|
|
|
|
expect(isStyleSupport('not-existed')).toBe(false);
|
|
|
|
spy.mockRestore();
|
|
|
|
});
|
|
|
|
});
|
2017-02-08 14:44:43 +08:00
|
|
|
});
|