File size: 1,008 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import React from 'react';
import type { PortalProps, PortalRef } from 'rc-util/lib/Portal';
import { TriggerMockContext } from '../../../shared/demoTestContext';

let OriginPortal = jest.requireActual('rc-util/lib/Portal');
OriginPortal = OriginPortal.default ?? OriginPortal;

class MockPortal extends React.Component<React.PropsWithChildren> {
  container: boolean | undefined;

  static contextType = TriggerMockContext;

  componentDidMount() {
    this.createContainer();
  }

  createContainer() {
    this.container = true;
    this.forceUpdate();
  }

  render() {
    const { children } = this.props;
    if (this.container) {
      return children;
    }
    return null;
  }
}

const CustomPortal = React.forwardRef<PortalRef, PortalProps | React.PropsWithChildren>((props, ref) => {
  const context = React.useContext(TriggerMockContext);
  if (context?.mock === false) {
    return <OriginPortal {...props} ref={ref} />;
  }
  return <MockPortal {...props} />;
});

export default CustomPortal;