Skip to content

Commit 1edc1d8

Browse files
committed
feat: Enable disable port in test env
1 parent 17e1817 commit 1edc1d8

File tree

7 files changed

+88
-6
lines changed

7 files changed

+88
-6
lines changed

src/Portal.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
import * as React from 'react';
22
import { createPortal } from 'react-dom';
3+
import canUseDom from 'rc-util/lib/Dom/canUseDom';
34
import OrderContext from './Context';
45
import useDom from './useDom';
56
import useScrollLocker from './useScrollLocker';
7+
import { inlineMock } from './mock';
68

7-
// ZombieJ: Since React 18 strict mode logic change.
8-
// We should rewrite for compatible.
9+
export type ContainerType = Element | DocumentFragment;
10+
11+
export type GetContainer = string | ContainerType | (() => ContainerType);
912

1013
export interface PortalProps {
1114
/** Customize container element. Default will create a div in document.body when `open` */
12-
getContainer?: () => Element | DocumentFragment;
15+
getContainer?: GetContainer;
1316
children?: React.ReactNode;
1417
/** Show the portal children */
1518
open?: boolean;
1619
/** Lock screen scroll when open */
1720
autoLock?: boolean;
1821
}
1922

23+
const getPortalContainer = (getContainer: GetContainer) => {
24+
if (!canUseDom()) {
25+
return null;
26+
}
27+
28+
if (typeof getContainer === 'string') {
29+
return document.querySelector(getContainer);
30+
}
31+
if (typeof getContainer === 'function') {
32+
return getContainer();
33+
}
34+
return getContainer;
35+
};
36+
2037
export default function Portal(props: PortalProps) {
2138
const { open, autoLock, getContainer, children } = props;
2239

@@ -30,7 +47,7 @@ export default function Portal(props: PortalProps) {
3047
}, [open]);
3148

3249
// ======================== Container ========================
33-
const customizeContainer = getContainer?.();
50+
const customizeContainer = getPortalContainer(getContainer);
3451

3552
const [defaultContainer, queueCreate] = useDom(
3653
mergedRender && !customizeContainer,
@@ -39,13 +56,14 @@ export default function Portal(props: PortalProps) {
3956

4057
// ========================= Render ==========================
4158
// Do not render when nothing need render
42-
if (!mergedRender) {
59+
if (!mergedRender || !canUseDom()) {
4360
return null;
4461
}
4562

63+
console.log(inlineMock());
4664
return (
4765
<OrderContext.Provider value={queueCreate}>
48-
{createPortal(children, mergedContainer)}
66+
{inlineMock() ? children : createPortal(children, mergedContainer)}
4967
</OrderContext.Provider>
5068
);
5169
}

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Portal from './Portal';
22
import type { PortalProps } from './Portal';
3+
import { inlineMock } from './mock';
34

45
export type { PortalProps };
6+
export { inlineMock };
57

68
export default Portal;

src/mock.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export let inline = false;
2+
3+
export function inlineMock(nextInline?: boolean) {
4+
if (typeof nextInline === 'boolean') {
5+
inline = nextInline;
6+
}
7+
return inline;
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test Env inlineMock 1`] = `
4+
<div>
5+
Start
6+
<div
7+
class="bamboo"
8+
>
9+
Hello World
10+
</div>
11+
End
12+
</div>
13+
`;

tests/setupAfterEnv.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom';

tests/ssr.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import Portal from '../src';
4+
5+
jest.mock('rc-util/lib/Dom/canUseDom', () => () => false);
6+
7+
describe('SSR', () => {
8+
it('No Render in SSR', () => {
9+
const { unmount } = render(
10+
<Portal open>
11+
<div className="bamboo">Hello World</div>
12+
</Portal>,
13+
);
14+
15+
expect(document.querySelector('.bamboo')).toBeFalsy();
16+
17+
unmount();
18+
});
19+
});

tests/testEnv.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import Portal, { inlineMock } from '../src';
4+
5+
describe('Test Env', () => {
6+
it('inlineMock', () => {
7+
inlineMock(true);
8+
9+
const { container } = render(
10+
<>
11+
Start
12+
<Portal open>
13+
<div className="bamboo">Hello World</div>
14+
</Portal>
15+
End
16+
</>,
17+
);
18+
19+
expect(container).toMatchSnapshot();
20+
});
21+
});

0 commit comments

Comments
 (0)