Skip to content

Commit e6d5314

Browse files
authored
fix: React 18 use createRoot instead (#203)
* chore: replace with react compitable render * test: part test case * test: more test case * test: hooks test
1 parent 792b0f1 commit e6d5314

File tree

6 files changed

+109
-99
lines changed

6 files changed

+109
-99
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
2+
setupFilesAfterEnv: ['<rootDir>/tests/setup.js'],
23
snapshotSerializers: [require.resolve("enzyme-to-json/serializer")]
34
};

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@
4949
"react-dom": ">=16.9.0"
5050
},
5151
"devDependencies": {
52+
"@testing-library/jest-dom": "^5.16.4",
53+
"@testing-library/react": "^13.0.1",
5254
"@types/classnames": "^2.2.10",
5355
"@types/enzyme": "^3.10.7",
5456
"@types/jest": "^26.0.4",
55-
"@types/react": "^16.9.34",
56-
"@types/react-dom": "^16.9.7",
57+
"@types/react": "^18.0.0",
58+
"@types/react-dom": "^18.0.0",
5759
"cross-env": "^7.0.0",
5860
"dumi": "^1.1.7",
5961
"enzyme": "^3.3.0",
@@ -63,14 +65,14 @@
6365
"gh-pages": "^3.1.0",
6466
"less": "^3.10.3",
6567
"np": "^6.0.0",
66-
"react": "^16.9.0",
67-
"react-dom": "^16.9.0",
68+
"react": "^18.0.0",
69+
"react-dom": "^18.0.0",
6870
"typescript": "^4.0.5"
6971
},
7072
"dependencies": {
7173
"@babel/runtime": "^7.10.1",
7274
"classnames": "2.x",
7375
"rc-motion": "^2.2.0",
74-
"rc-util": "^5.0.1"
76+
"rc-util": "^5.20.1"
7577
}
7678
}

src/Notification.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { Component } from 'react';
33
import type { ReactText } from 'react';
4-
import ReactDOM from 'react-dom';
4+
import { render, unmount } from 'rc-util/lib/React/render';
55
import classNames from 'classnames';
66
import { CSSMotionList } from 'rc-motion';
77
import type { NoticeProps } from './Notice';
@@ -260,7 +260,7 @@ Notification.newInstance = function newNotificationInstance(properties, callback
260260
},
261261
component: notification,
262262
destroy() {
263-
ReactDOM.unmountComponentAtNode(div);
263+
unmount(div);
264264
if (div.parentNode) {
265265
div.parentNode.removeChild(div);
266266
}
@@ -279,7 +279,7 @@ Notification.newInstance = function newNotificationInstance(properties, callback
279279
return;
280280
}
281281

282-
ReactDOM.render(<Notification {...props} ref={ref} />, div);
282+
render(<Notification {...props} ref={ref} />, div);
283283
};
284284

285285
export default Notification;

tests/hooks.test.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
2-
import type { ReactWrapper } from 'enzyme';
3-
import { mount } from 'enzyme';
2+
import { render, fireEvent } from '@testing-library/react';
43
import Notification from '../src';
54
import type { NotificationInstance } from '../src/Notification';
65

@@ -18,11 +17,11 @@ describe('Notification.Hooks', () => {
1817

1918
const Context = React.createContext({ name: 'light' });
2019

21-
let wrapper: ReactWrapper;
20+
let container: HTMLElement;
2221
Notification.newInstance(
2322
{
2423
TEST_RENDER: (node: React.ReactElement) => {
25-
wrapper = mount(<div>{node}</div>);
24+
({ container } = render(<div>{node}</div>));
2625
},
2726
} as any,
2827
(notification) => {
@@ -54,26 +53,27 @@ describe('Notification.Hooks', () => {
5453
);
5554
};
5655

57-
const demo = mount(<Demo />);
58-
demo.find('button').simulate('click');
56+
const { container: demoContainer } = render(<Demo />);
57+
fireEvent.click(demoContainer.querySelector('button'));
5958

6059
await timeout(10);
61-
expect(demo.find('.context-content').text()).toEqual('bamboo');
60+
expect(container.querySelector('.context-content').textContent).toEqual('bamboo');
6261

6362
await timeout(1000);
64-
expect(wrapper.find('Notification').state().notices).toHaveLength(0);
63+
expect(container.querySelectorAll('.rc-notification-notice')).toHaveLength(0);
6564

6665
instance.destroy();
6766
});
6867

6968
it('key replace', async () => {
7069
let instance: NotificationInstance;
7170

72-
let wrapper: ReactWrapper;
71+
let container: HTMLElement;
72+
let unmount: () => void;
7373
Notification.newInstance(
7474
{
7575
TEST_RENDER: (node: React.ReactElement) => {
76-
wrapper = mount(<div>{node}</div>);
76+
({ container, unmount } = render(<div>{node}</div>));
7777
},
7878
} as any,
7979
(notification) => {
@@ -110,17 +110,17 @@ describe('Notification.Hooks', () => {
110110
);
111111
};
112112

113-
const demo = mount(<Demo />);
114-
demo.find('button').simulate('click');
113+
const { container: demoContainer } = render(<Demo />);
114+
fireEvent.click(demoContainer.querySelector('button'));
115115

116116
await timeout(10);
117-
expect(demo.find('.context-content').text()).toEqual('light');
117+
expect(container.querySelector('.context-content').textContent).toEqual('light');
118118

119119
await timeout(600);
120-
expect(demo.find('.context-content').text()).toEqual('bamboo');
120+
expect(container.querySelector('.context-content').textContent).toEqual('bamboo');
121121

122122
instance.destroy();
123123

124-
wrapper.unmount();
124+
unmount();
125125
});
126126
});

0 commit comments

Comments
 (0)