Browse Source

test: migrate part of Affix tests (#35860)

pull/35927/head
Shang Song 3 years ago
committed by GitHub
parent
commit
22c8bc4c7a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      components/affix/__tests__/Affix.test.tsx
  2. 2
      components/affix/index.tsx

40
components/affix/__tests__/Affix.test.tsx

@ -1,7 +1,7 @@
import React from 'react';
import type { ReactWrapper } from 'enzyme';
import { mount } from 'enzyme';
import type { AffixProps, AffixState } from '..';
import type { AffixProps, AffixState, InternalAffixClass } from '..';
import Affix from '..';
import { getObserverEntities } from '../utils';
import Button from '../../button';
@ -60,7 +60,6 @@ describe('Affix Render', () => {
const domMock = jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
let affixMounterWrapper: ReactWrapper<unknown, unknown, AffixMounter>;
let affixWrapper: ReactWrapper<AffixProps, AffixState, React.Component<AffixProps, AffixState>>;
const classRect: Record<string, DOMRect> = {
container: {
@ -143,15 +142,35 @@ describe('Affix Render', () => {
});
describe('updatePosition when target changed', () => {
it('function change', () => {
it('function change', async () => {
document.body.innerHTML = '<div id="mounter" />';
const container = document.querySelector('#id') as HTMLDivElement;
const getTarget = () => container;
affixWrapper = mount(<Affix target={getTarget}>{null}</Affix>);
affixWrapper.setProps({ target: () => null });
expect(affixWrapper.find('Affix').last().state().status).toBe(0);
expect(affixWrapper.find('Affix').last().state().affixStyle).toBe(undefined);
expect(affixWrapper.find('Affix').last().state().placeholderStyle).toBe(undefined);
let affixInstance: InternalAffixClass;
const { rerender } = render(
<Affix
ref={node => {
affixInstance = node as InternalAffixClass;
}}
target={getTarget}
>
{null}
</Affix>,
);
rerender(
<Affix
ref={node => {
affixInstance = node as InternalAffixClass;
}}
target={() => null}
>
{null}
</Affix>,
);
expect(affixInstance!.state.status).toBe(0);
expect(affixInstance!.state.affixStyle).toBe(undefined);
expect(affixInstance!.state.placeholderStyle).toBe(undefined);
await sleep(100);
});
it('instance change', async () => {
@ -163,13 +182,12 @@ describe('Affix Render', () => {
const originLength = getObserverLength();
const getTarget = () => target;
affixWrapper = mount(<Affix target={getTarget}>{null}</Affix>);
const { rerender } = render(<Affix target={getTarget}>{null}</Affix>);
await sleep(100);
expect(getObserverLength()).toBe(originLength + 1);
target = null;
affixWrapper.setProps({});
affixWrapper.update();
rerender(<Affix>{null}</Affix>);
await sleep(100);
expect(getObserverLength()).toBe(originLength);
});

2
components/affix/index.tsx

@ -296,6 +296,8 @@ class Affix extends React.Component<InternalAffixProps, AffixState> {
);
}
}
// just use in test
export type InternalAffixClass = Affix;
const AffixFC = React.forwardRef<Affix, AffixProps>((props, ref) => {
const { prefixCls: customizePrefixCls } = props;

Loading…
Cancel
Save