Skip to content

Commit

Permalink
Use createRoot in ReactART-test (#28060)
Browse files Browse the repository at this point in the history
Stacked on #28059

---------
  • Loading branch information
jackpope authored Jan 23, 2024
1 parent 9e13800 commit 6e03d0d
Showing 1 changed file with 53 additions and 36 deletions.
89 changes: 53 additions & 36 deletions packages/react-art/src/__tests__/ReactART-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Wedge from 'react-art/Wedge';

// Isolate DOM renderer.
jest.resetModules();
const ReactDOM = require('react-dom');

const ReactDOMClient = require('react-dom/client');
const act = require('internal-test-utils').act;

Expand Down Expand Up @@ -178,11 +178,11 @@ describe('ReactART', () => {
testDOMNodeStructure(realNode, expectedStructure);
});

it('should be able to reorder components', () => {
const instance = ReactDOM.render(
<TestComponent flipped={false} />,
container,
);
it('should be able to reorder components', async () => {
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<TestComponent flipped={false} />);
});

const expectedStructure = {
nodeName: 'svg',
Expand All @@ -200,10 +200,12 @@ describe('ReactART', () => {
],
};

const realNode = ReactDOM.findDOMNode(instance);
const realNode = container.firstChild;
testDOMNodeStructure(realNode, expectedStructure);

ReactDOM.render(<TestComponent flipped={true} />, container);
await act(() => {
root.render(<TestComponent flipped={true} />);
});

const expectedNewStructure = {
nodeName: 'svg',
Expand All @@ -224,7 +226,7 @@ describe('ReactART', () => {
testDOMNodeStructure(realNode, expectedNewStructure);
});

it('should be able to reorder many components', () => {
it('should be able to reorder many components', async () => {
class Component extends React.Component {
render() {
const chars = this.props.chars.split('');
Expand All @@ -242,14 +244,17 @@ describe('ReactART', () => {
const before = 'abcdefghijklmnopqrst';
const after = 'mxhpgwfralkeoivcstzy';

let instance = ReactDOM.render(<Component chars={before} />, container);
const realNode = ReactDOM.findDOMNode(instance);
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Component chars={before} />);
});
const realNode = container.firstChild;
expect(realNode.textContent).toBe(before);

instance = ReactDOM.render(<Component chars={after} />, container);
await act(() => {
root.render(<Component chars={after} />);
});
expect(realNode.textContent).toBe(after);

ReactDOM.unmountComponentAtNode(container);
});

it('renders composite with lifecycle inside group', async () => {
Expand Down Expand Up @@ -311,7 +316,7 @@ describe('ReactART', () => {
expect(ref.constructor).toBe(CustomShape);
});

it('resolves refs before componentDidUpdate', () => {
it('resolves refs before componentDidUpdate', async () => {
class CustomShape extends React.Component {
render() {
return <Shape />;
Expand Down Expand Up @@ -341,24 +346,34 @@ describe('ReactART', () => {
);
}
}
ReactDOM.render(<Outer />, container);

const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(<Outer />);
});
expect(ref).toBe(null);
ReactDOM.render(<Outer mountCustomShape={true} />, container);

await act(() => {
root.render(<Outer mountCustomShape={true} />);
});
expect(ref.constructor).toBe(CustomShape);
});

it('adds and updates event handlers', () => {
function render(onClick) {
return ReactDOM.render(
<Surface>
<Shape onClick={onClick} />
</Surface>,
container,
);
it('adds and updates event handlers', async () => {
const root = ReactDOMClient.createRoot(container);

async function render(onClick) {
await act(() => {
root.render(
<Surface>
<Shape onClick={onClick} />
</Surface>,
);
});
}

function doClick(instance) {
const path = ReactDOM.findDOMNode(instance).querySelector('path');
const path = container.firstChild.querySelector('path');

path.dispatchEvent(
new MouseEvent('click', {
Expand All @@ -368,12 +383,12 @@ describe('ReactART', () => {
}

const onClick1 = jest.fn();
let instance = render(onClick1);
let instance = await render(onClick1);
doClick(instance);
expect(onClick1).toBeCalled();

const onClick2 = jest.fn();
instance = render(onClick2);
instance = await render(onClick2);
doClick(instance);
expect(onClick2).toBeCalled();
});
Expand Down Expand Up @@ -412,15 +427,17 @@ describe('ReactART', () => {

await waitFor(['A']);

ReactDOM.render(
<Surface>
<LogCurrentRenderer />
<CurrentRendererContext.Provider value="ART">
const root = ReactDOMClient.createRoot(container);
await act(() => {
root.render(
<Surface>
<LogCurrentRenderer />
</CurrentRendererContext.Provider>
</Surface>,
container,
);
<CurrentRendererContext.Provider value="ART">
<LogCurrentRenderer />
</CurrentRendererContext.Provider>
</Surface>,
);
});

expect(ops).toEqual([null, 'ART']);

Expand Down

0 comments on commit 6e03d0d

Please sign in to comment.