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';
|
2022-10-12 22:46:16 +08:00
|
|
|
import { waitFakeTimer, render, fireEvent } from '../../../tests/utils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import getDataOrAriaProps from '../getDataOrAriaProps';
|
|
|
|
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
|
|
|
|
2018-06-14 22:33:35 +08:00
|
|
|
describe('getDataOrAriaProps', () => {
|
2018-06-12 03:18:41 +08:00
|
|
|
it('returns all data-* properties from an object', () => {
|
|
|
|
const props = {
|
|
|
|
onClick: () => {},
|
|
|
|
isOpen: true,
|
|
|
|
'data-test': 'test-id',
|
|
|
|
'data-id': 1234,
|
|
|
|
};
|
2018-06-14 22:33:35 +08:00
|
|
|
const results = getDataOrAriaProps(props);
|
2018-06-12 03:18:41 +08:00
|
|
|
expect(results).toEqual({
|
|
|
|
'data-test': 'test-id',
|
|
|
|
'data-id': 1234,
|
|
|
|
});
|
|
|
|
});
|
2018-06-14 22:33:35 +08:00
|
|
|
|
|
|
|
it('does not return data-__ properties from an object', () => {
|
|
|
|
const props = {
|
|
|
|
onClick: () => {},
|
|
|
|
isOpen: true,
|
|
|
|
'data-__test': 'test-id',
|
|
|
|
'data-__id': 1234,
|
|
|
|
};
|
|
|
|
const results = getDataOrAriaProps(props);
|
|
|
|
expect(results).toEqual({});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns all aria-* properties from an object', () => {
|
|
|
|
const props = {
|
|
|
|
onClick: () => {},
|
|
|
|
isOpen: true,
|
|
|
|
'aria-labelledby': 'label-id',
|
|
|
|
'aria-label': 'some-label',
|
|
|
|
};
|
|
|
|
const results = getDataOrAriaProps(props);
|
|
|
|
expect(results).toEqual({
|
|
|
|
'aria-labelledby': 'label-id',
|
|
|
|
'aria-label': 'some-label',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns role property from an object', () => {
|
|
|
|
const props = {
|
|
|
|
onClick: () => {},
|
|
|
|
isOpen: true,
|
|
|
|
role: 'search',
|
|
|
|
};
|
|
|
|
const results = getDataOrAriaProps(props);
|
|
|
|
expect(results).toEqual({ role: 'search' });
|
|
|
|
});
|
2018-06-12 03:18:41 +08:00
|
|
|
});
|
2018-11-12 20:31:58 +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
|
|
|
});
|