Browse Source

test: remove useless branch (#40367)

pull/40445/head
lijianan 2 years ago
committed by GitHub
parent
commit
758a28c93e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      components/slider/__tests__/index.test.tsx
  2. 64
      tests/shared/focusTest.tsx

6
components/slider/__tests__/index.test.tsx

@ -3,11 +3,11 @@ import Slider from '..';
import focusTest from '../../../tests/shared/focusTest';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { render, fireEvent, act } from '../../../tests/utils';
import { act, fireEvent, render } from '../../../tests/utils';
import ConfigProvider from '../../config-provider';
import SliderTooltip from '../SliderTooltip';
import type { TooltipProps } from '../../tooltip';
import { resetWarned } from '../../_util/warning';
import SliderTooltip from '../SliderTooltip';
function tooltipProps(): TooltipProps {
return (global as any).tooltipProps;
@ -26,7 +26,7 @@ jest.mock('../../tooltip', () => {
describe('Slider', () => {
mountTest(Slider);
rtlTest(Slider);
focusTest(Slider, { testLib: true });
focusTest(Slider);
beforeEach(() => {
jest.useFakeTimers();

64
tests/shared/focusTest.tsx

@ -1,11 +1,10 @@
import React from 'react';
import { sleep, render, fireEvent } from '../utils';
import { fireEvent, render, sleep } from '../utils';
// eslint-disable-next-line jest/no-export
export default function focusTest(
const focusTest = (
Component: React.ComponentType<any>,
{ refFocus = false, testLib = false, blurDelay = 0 } = {},
) {
{ refFocus = false, blurDelay = 0 } = {},
) => {
describe('focus and blur', () => {
let focused = false;
let blurred = false;
@ -23,41 +22,7 @@ export default function focusTest(
}
});
// ==================== React Test Lib ====================
if (testLib) {
it('Test: focus() and onFocus', () => {
const handleFocus = jest.fn();
const ref = React.createRef<any>();
const { unmount } = render(<Component onFocus={handleFocus} ref={ref} />);
ref.current.focus();
expect(handleFocus).toHaveBeenCalled();
unmount();
});
it('Test: blur() and onBlur', async () => {
const handleBlur = jest.fn();
const ref = React.createRef<any>();
const { unmount } = render(<Component ref={ref} onBlur={handleBlur} />);
ref.current.focus();
ref.current.blur();
expect(handleBlur).toHaveBeenCalled();
unmount();
});
it('Test: autoFocus', () => {
const handleFocus = jest.fn();
const { unmount } = render(<Component autoFocus onFocus={handleFocus} />);
expect(handleFocus).toHaveBeenCalled();
unmount();
});
return;
}
let containerHtml: HTMLElement;
let containerHtml: HTMLDivElement;
beforeEach(() => {
containerHtml = document.createElement('div');
document.body.appendChild(containerHtml);
@ -74,7 +39,7 @@ export default function focusTest(
document.body.removeChild(containerHtml);
});
const getElement = (container: { querySelector: Function }) =>
const getElement = (container: HTMLElement) =>
container.querySelector('input') ||
container.querySelector('button') ||
container.querySelector('textarea') ||
@ -92,7 +57,7 @@ export default function focusTest(
ref.current.focus();
expect(focused).toBeTruthy();
fireEvent.focus(getElement(container));
fireEvent.focus(getElement(container)!);
expect(onFocus).toHaveBeenCalled();
});
@ -109,7 +74,7 @@ export default function focusTest(
ref.current.blur();
expect(blurred).toBeTruthy();
fireEvent.blur(getElement(container));
fireEvent.blur(getElement(container)!);
await sleep(blurDelay);
expect(onBlur).toHaveBeenCalled();
});
@ -120,14 +85,14 @@ export default function focusTest(
expect(focused).toBeTruthy();
fireEvent.focus(getElement(container));
fireEvent.focus(getElement(container)!);
expect(onFocus).toHaveBeenCalled();
});
} else {
it('focus() and onFocus', () => {
const handleFocus = jest.fn();
const { container } = render(<Component onFocus={handleFocus} />);
fireEvent.focus(getElement(container));
fireEvent.focus(getElement(container)!);
expect(handleFocus).toHaveBeenCalled();
});
@ -135,9 +100,9 @@ export default function focusTest(
jest.useRealTimers();
const handleBlur = jest.fn();
const { container } = render(<Component onBlur={handleBlur} />);
fireEvent.focus(getElement(container));
fireEvent.focus(getElement(container)!);
await sleep(0);
fireEvent.blur(getElement(container));
fireEvent.blur(getElement(container)!);
await sleep(0);
expect(handleBlur).toHaveBeenCalled();
});
@ -149,4 +114,7 @@ export default function focusTest(
});
}
});
}
};
// eslint-disable-next-line jest/no-export
export default focusTest;

Loading…
Cancel
Save