mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-11 19:42:54 +08:00
chore: Replace domhook with jest.spyon (#23469)
* chore: Replace domhook with jest.spyon * use mockreturn value when possible * Update tests/shared/focusTest.js
This commit is contained in:
parent
677168b007
commit
55265ac4ef
@ -1,71 +0,0 @@
|
|||||||
const __NULL__ = { notExist: true };
|
|
||||||
|
|
||||||
type ElementType<P> = {
|
|
||||||
prototype: P;
|
|
||||||
};
|
|
||||||
|
|
||||||
export function spyElementPrototypes<P extends {}>(Element: ElementType<P>, properties: P) {
|
|
||||||
const propNames = Object.keys(properties);
|
|
||||||
const originDescriptors = {};
|
|
||||||
|
|
||||||
propNames.forEach(propName => {
|
|
||||||
const originDescriptor = Object.getOwnPropertyDescriptor(Element.prototype, propName);
|
|
||||||
originDescriptors[propName] = originDescriptor || __NULL__;
|
|
||||||
|
|
||||||
const spyProp = properties[propName];
|
|
||||||
|
|
||||||
if (typeof spyProp === 'function') {
|
|
||||||
// If is a function
|
|
||||||
Element.prototype[propName] = function spyFunc(...args) {
|
|
||||||
return spyProp.call(this, originDescriptor, ...args);
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
// Otherwise tread as a property
|
|
||||||
Object.defineProperty(Element.prototype, propName, {
|
|
||||||
...spyProp,
|
|
||||||
set(value) {
|
|
||||||
if (spyProp.set) {
|
|
||||||
return spyProp.set.call(this, originDescriptor, value);
|
|
||||||
}
|
|
||||||
return originDescriptor.set(value);
|
|
||||||
},
|
|
||||||
get() {
|
|
||||||
if (spyProp.get) {
|
|
||||||
return spyProp.get.call(this, originDescriptor);
|
|
||||||
}
|
|
||||||
return originDescriptor.get();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
mockRestore() {
|
|
||||||
propNames.forEach(propName => {
|
|
||||||
const originDescriptor = originDescriptors[propName];
|
|
||||||
if (originDescriptor === __NULL__) {
|
|
||||||
delete Element.prototype[propName];
|
|
||||||
} else if (typeof originDescriptor === 'function') {
|
|
||||||
Element.prototype[propName] = originDescriptor;
|
|
||||||
} else {
|
|
||||||
Object.defineProperty(Element.prototype, propName, originDescriptor);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
type FunctionPropertyNames<T> = {
|
|
||||||
[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;
|
|
||||||
}[keyof T] &
|
|
||||||
string;
|
|
||||||
|
|
||||||
export function spyElementPrototype<P extends {}, K extends FunctionPropertyNames<P>>(
|
|
||||||
Element: ElementType<P>,
|
|
||||||
propName: K,
|
|
||||||
property: P[K],
|
|
||||||
) {
|
|
||||||
return spyElementPrototypes(Element, {
|
|
||||||
[propName]: property,
|
|
||||||
});
|
|
||||||
}
|
|
@ -3,7 +3,6 @@ import { mount } from 'enzyme';
|
|||||||
import Affix from '..';
|
import Affix from '..';
|
||||||
import { getObserverEntities } from '../utils';
|
import { getObserverEntities } from '../utils';
|
||||||
import Button from '../../button';
|
import Button from '../../button';
|
||||||
import { spyElementPrototype } from '../../__tests__/util/domHook';
|
|
||||||
import rtlTest from '../../../tests/shared/rtlTest';
|
import rtlTest from '../../../tests/shared/rtlTest';
|
||||||
import { sleep } from '../../../tests/utils';
|
import { sleep } from '../../../tests/utils';
|
||||||
|
|
||||||
@ -53,7 +52,7 @@ describe('Affix Render', () => {
|
|||||||
rtlTest(Affix);
|
rtlTest(Affix);
|
||||||
|
|
||||||
let wrapper;
|
let wrapper;
|
||||||
let domMock;
|
const domMock = jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
|
||||||
|
|
||||||
const classRect: any = {
|
const classRect: any = {
|
||||||
container: {
|
container: {
|
||||||
@ -63,7 +62,7 @@ describe('Affix Render', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
domMock = spyElementPrototype(HTMLElement, 'getBoundingClientRect', function mockBounding() {
|
domMock.mockImplementation(function fn(this: HTMLElement) {
|
||||||
return (
|
return (
|
||||||
classRect[this.className] || {
|
classRect[this.className] || {
|
||||||
top: 0,
|
top: 0,
|
||||||
|
@ -1,27 +1,29 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { mount } from 'enzyme';
|
import { mount } from 'enzyme';
|
||||||
import Anchor from '..';
|
import Anchor from '..';
|
||||||
import { spyElementPrototypes } from '../../__tests__/util/domHook';
|
|
||||||
import { sleep } from '../../../tests/utils';
|
import { sleep } from '../../../tests/utils';
|
||||||
|
|
||||||
const { Link } = Anchor;
|
const { Link } = Anchor;
|
||||||
|
|
||||||
describe('Anchor Render', () => {
|
describe('Anchor Render', () => {
|
||||||
const getBoundingClientRectMock = jest.fn(() => ({
|
const getBoundingClientRectMock = jest.spyOn(
|
||||||
width: 100,
|
HTMLHeadingElement.prototype,
|
||||||
height: 100,
|
'getBoundingClientRect',
|
||||||
top: 1000,
|
);
|
||||||
}));
|
const getClientRectsMock = jest.spyOn(HTMLHeadingElement.prototype, 'getClientRects');
|
||||||
const getClientRectsMock = jest.fn(() => ({
|
|
||||||
length: 1,
|
beforeAll(() => {
|
||||||
}));
|
getBoundingClientRectMock.mockReturnValue({
|
||||||
const headingSpy = spyElementPrototypes(HTMLHeadingElement, {
|
width: 100,
|
||||||
getBoundingClientRect: getBoundingClientRectMock,
|
height: 100,
|
||||||
getClientRects: getClientRectsMock,
|
top: 1000,
|
||||||
|
});
|
||||||
|
getClientRectsMock.mockReturnValue({ length: 1 });
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
headingSpy.mockRestore();
|
getBoundingClientRectMock.mockRestore();
|
||||||
|
getClientRectsMock.mockRestore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Anchor render perfectly', () => {
|
it('Anchor render perfectly', () => {
|
||||||
|
@ -4,24 +4,19 @@ import PageHeader from '..';
|
|||||||
import ConfigProvider from '../../config-provider';
|
import ConfigProvider from '../../config-provider';
|
||||||
import mountTest from '../../../tests/shared/mountTest';
|
import mountTest from '../../../tests/shared/mountTest';
|
||||||
import rtlTest from '../../../tests/shared/rtlTest';
|
import rtlTest from '../../../tests/shared/rtlTest';
|
||||||
import { spyElementPrototypes } from '../../__tests__/util/domHook';
|
|
||||||
|
|
||||||
describe('PageHeader', () => {
|
describe('PageHeader', () => {
|
||||||
mountTest(PageHeader);
|
mountTest(PageHeader);
|
||||||
rtlTest(PageHeader);
|
rtlTest(PageHeader);
|
||||||
|
|
||||||
let spy;
|
const mockGetBoundingClientRect = jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
spy = spyElementPrototypes(HTMLElement, {
|
mockGetBoundingClientRect.mockReturnValue({ width: 100 });
|
||||||
getBoundingClientRect: () => ({
|
|
||||||
width: 100,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
spy.mockRestore();
|
mockGetBoundingClientRect.mockRestore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('pageHeader should not contain back it back', () => {
|
it('pageHeader should not contain back it back', () => {
|
||||||
|
@ -3,7 +3,6 @@ import { mount } from 'enzyme';
|
|||||||
import Upload from '..';
|
import Upload from '..';
|
||||||
import UploadList from '../UploadList';
|
import UploadList from '../UploadList';
|
||||||
import Form from '../../form';
|
import Form from '../../form';
|
||||||
import { spyElementPrototypes } from '../../__tests__/util/domHook';
|
|
||||||
import { errorRequest, successRequest } from './requests';
|
import { errorRequest, successRequest } from './requests';
|
||||||
import { setup, teardown } from './mock';
|
import { setup, teardown } from './mock';
|
||||||
import { sleep } from '../../../tests/utils';
|
import { sleep } from '../../../tests/utils';
|
||||||
@ -35,35 +34,16 @@ describe('Upload List', () => {
|
|||||||
function setSize(width, height) {
|
function setSize(width, height) {
|
||||||
size = { width, height };
|
size = { width, height };
|
||||||
}
|
}
|
||||||
const imageSpy = spyElementPrototypes(Image, {
|
const mockWidthGet = jest.spyOn(Image.prototype, 'width', 'get');
|
||||||
src: {
|
const mockHeightGet = jest.spyOn(Image.prototype, 'height', 'get');
|
||||||
set() {
|
const mockSrcSet = jest.spyOn(Image.prototype, 'src', 'set');
|
||||||
if (this.onload) {
|
|
||||||
this.onload();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
width: {
|
|
||||||
get: () => size.width,
|
|
||||||
},
|
|
||||||
height: {
|
|
||||||
get: () => size.height,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
let drawImageCallback = null;
|
let drawImageCallback = null;
|
||||||
function hookDrawImageCall(callback) {
|
function hookDrawImageCall(callback) {
|
||||||
drawImageCallback = callback;
|
drawImageCallback = callback;
|
||||||
}
|
}
|
||||||
const canvasSpy = spyElementPrototypes(HTMLCanvasElement, {
|
const mockGetCanvasContext = jest.spyOn(HTMLCanvasElement.prototype, 'getContext');
|
||||||
getContext: () => ({
|
const mockToDataURL = jest.spyOn(HTMLCanvasElement.prototype, 'toDataURL');
|
||||||
drawImage: (...args) => {
|
|
||||||
if (drawImageCallback) drawImageCallback(...args);
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
|
|
||||||
toDataURL: () => 'data:image/png;base64,',
|
|
||||||
});
|
|
||||||
|
|
||||||
// HTMLCanvasElement.prototype
|
// HTMLCanvasElement.prototype
|
||||||
|
|
||||||
@ -76,12 +56,29 @@ describe('Upload List', () => {
|
|||||||
let open;
|
let open;
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
open = jest.spyOn(window, 'open').mockImplementation(() => {});
|
open = jest.spyOn(window, 'open').mockImplementation(() => {});
|
||||||
|
mockWidthGet.mockImplementation(() => size.width);
|
||||||
|
mockHeightGet.mockImplementation(() => size.height);
|
||||||
|
mockSrcSet.mockImplementation(function fn() {
|
||||||
|
if (this.onload) {
|
||||||
|
this.onload();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
mockGetCanvasContext.mockReturnValue({
|
||||||
|
drawImage: (...args) => {
|
||||||
|
if (drawImageCallback) drawImageCallback(...args);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
mockToDataURL.mockReturnValue('data:image/png;base64,');
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
window.URL.createObjectURL = originCreateObjectURL;
|
window.URL.createObjectURL = originCreateObjectURL;
|
||||||
imageSpy.mockRestore();
|
mockWidthGet.mockRestore();
|
||||||
canvasSpy.mockRestore();
|
mockHeightGet.mockRestore();
|
||||||
|
mockSrcSet.mockRestore();
|
||||||
|
mockGetCanvasContext.mockRestore();
|
||||||
|
mockToDataURL.mockRestore();
|
||||||
open.mockRestore();
|
open.mockRestore();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,24 +1,22 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { mount } from 'enzyme';
|
import { mount } from 'enzyme';
|
||||||
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
|
|
||||||
import { sleep } from '../utils';
|
import { sleep } from '../utils';
|
||||||
|
|
||||||
// eslint-disable-next-line jest/no-export
|
// eslint-disable-next-line jest/no-export
|
||||||
export default function focusTest(Component, refFocus = false) {
|
export default function focusTest(Component, refFocus = false) {
|
||||||
describe('focus and blur', () => {
|
describe('focus and blur', () => {
|
||||||
let domSpy;
|
|
||||||
let focused = false;
|
let focused = false;
|
||||||
let blurred = false;
|
let blurred = false;
|
||||||
|
const mockFocus = jest.spyOn(HTMLElement.prototype, 'focus');
|
||||||
|
const mockBlur = jest.spyOn(HTMLElement.prototype, 'blur');
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
if (refFocus) {
|
if (refFocus) {
|
||||||
domSpy = spyElementPrototypes(HTMLElement, {
|
mockFocus.mockImplementation(() => {
|
||||||
focus() {
|
focused = true;
|
||||||
focused = true;
|
});
|
||||||
},
|
mockBlur.mockImplementation(() => {
|
||||||
blur() {
|
blurred = true;
|
||||||
blurred = true;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -32,9 +30,8 @@ export default function focusTest(Component, refFocus = false) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
if (domSpy) {
|
mockFocus.mockRestore();
|
||||||
domSpy.mockRestore();
|
mockBlur.mockRestore();
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@ -53,10 +50,7 @@ export default function focusTest(Component, refFocus = false) {
|
|||||||
ref.current.focus();
|
ref.current.focus();
|
||||||
expect(focused).toBeTruthy();
|
expect(focused).toBeTruthy();
|
||||||
|
|
||||||
wrapper
|
wrapper.find('input').first().simulate('focus');
|
||||||
.find('input')
|
|
||||||
.first()
|
|
||||||
.simulate('focus');
|
|
||||||
expect(onFocus).toHaveBeenCalled();
|
expect(onFocus).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -71,10 +65,7 @@ export default function focusTest(Component, refFocus = false) {
|
|||||||
ref.current.blur();
|
ref.current.blur();
|
||||||
expect(blurred).toBeTruthy();
|
expect(blurred).toBeTruthy();
|
||||||
|
|
||||||
wrapper
|
wrapper.find('input').first().simulate('blur');
|
||||||
.find('input')
|
|
||||||
.first()
|
|
||||||
.simulate('blur');
|
|
||||||
expect(onBlur).toHaveBeenCalled();
|
expect(onBlur).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -84,10 +75,7 @@ export default function focusTest(Component, refFocus = false) {
|
|||||||
|
|
||||||
expect(focused).toBeTruthy();
|
expect(focused).toBeTruthy();
|
||||||
|
|
||||||
wrapper
|
wrapper.find('input').first().simulate('focus');
|
||||||
.find('input')
|
|
||||||
.first()
|
|
||||||
.simulate('focus');
|
|
||||||
expect(onFocus).toHaveBeenCalled();
|
expect(onFocus).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user