ice/packages/miniapp-react-dom/tests/findDOMNode.spec.jsx

96 lines
2.5 KiB
React
Raw Permalink Normal View History

feat: miniapp (#333) * feat(miniapp): use platform in cli to generate miniapp code * fix: lint error * feat(miniapp): add miniapp project * chore: revert example code * chore: optimize code * feat(miniapp): migrate some packages to ice * feat(miniapp): update swcOptions for miniapp * chore(example): add more pages * feat(miniapp): add miniapp runClientApp * feat(miniapp): update compile config for miniapp * feat(miniapp): support get app and routes config in compile * fix(miniapp): lint warning * feat(miniapp): support routeData/routeConfig and reduce size * fix: lint warning * fix: lint warning * feat(miniapp): support global css and page css * feat(miniapp): support css modules * feat(miniapp): support copying assets in public dir * feat(miniapp): support Link and getSearchParams * feat(miniapp): use index.ts.ejs as template * feat(miniapp): support passing params in Link * feat(miniapp): support history in miniapp * fix(miniapp): build without compiling server bundle * fix(miniapp): use getRoutesConfig * feat(miniapp): support build options like outputDir * chore(miniapp): update miniapp examples * feat(miniapp): support html tags * feat(miniapp): remove taro related code * fix(miniapp): some cases * fix(miniapp): use require.resolve to get page loader * chore(miniapp): update lock * fix(miniapp): page loader check this name * chore(miniapp): add devDependencies * chore: update index ejs template * refactor(miniapp): update multiple platforms logic in ice * chore: lock regenerator-runtime version * chore(miniapp): add ali miniapp build command * fix(ice): set default value of platform to web * fix(runtime): remove duplicated denpdencies * chore: set default value of platform to web * feat(miniapp): registerSerialization when plugin activated * test(miniapp): suit for vitest * chore(miniapp): add webpack-sources to bundles * chore: clean code * chore(miniapp): remove jest config file * fix: lint error * fix: add default value of outputDir * refactor(miniapp): remove openBrowser from logMessage * chore(miniapp): remove unnecessary ts-ignore * fix: type error * chore(miniapp): rename Adapter to adapter * chore(miniapp): update interface name * refactor: use plugins to support miniapp * chore(miniapp): update example name * fix: update dependencies * feat(miniapp): add platform check in cli * chore: update lock * fix: remove useless platform option * fix: lint warning * fix: lint warning * test: use src code in tests * chore(miniapp): remove ||= operators * fix(miniapp): exclude miniapp-runtime esm code * fix(ice-cli): update platform for build command
2022-09-23 11:11:38 +08:00
import { expect, describe, test } from 'vitest';
import * as React from 'react';
import { document } from '@ice/miniapp-runtime';
import { render, unmountComponentAtNode, findDOMNode } from '../src/index';
describe('findDOMNode', () => {
function renderIntoDetachedNode(children) {
const div = document.createElement('div');
return render(children, div);
}
test('findDOMNode should return null if passed null', () => {
expect(findDOMNode(null)).toBe(null);
});
test('findDOMNode should find dom element', () => {
class MyNode extends React.Component {
render() {
return (
<div>
<span>Noise</span>
</div>
);
}
}
const myNode = renderIntoDetachedNode(<MyNode />);
const myDiv = findDOMNode(myNode);
const mySameDiv = findDOMNode(myDiv);
expect(myDiv.tagName).toBe('DIV');
expect(mySameDiv).toBe(myDiv);
});
test('findDOMNode should find dom element after an update from null', () => {
function Bar({ flag }) {
if (flag) {
return <span>A</span>;
}
return null;
}
class MyNode extends React.Component {
render() {
return <Bar flag={this.props.flag} />;
}
}
const container = document.createElement('div');
const myNodeA = render(<MyNode />, container);
const a = findDOMNode(myNodeA);
expect(a).toBe(null);
const myNodeB = render(<MyNode flag />, container);
expect(myNodeA === myNodeB).toBe(true);
const b = findDOMNode(myNodeB);
expect(b.tagName).toBe('SPAN');
});
test('findDOMNode should reject random objects', () => {
expect(() => {
findDOMNode({ foo: 'bar' });
}).toThrowError('Argument appears to not be a ReactComponent. Keys: foo');
});
test('findDOMNode should reject unmounted objects with render func', () => {
class Foo extends React.Component {
render() {
return <div />;
}
}
const container = document.createElement('div');
const inst = render(<Foo />, container);
unmountComponentAtNode(container);
expect(() => findDOMNode(inst)).toThrowError(
'Unable to find node on an unmounted component.',
);
});
test('findDOMNode should not throw an error when called within a component that is not mounted', () => {
class Bar extends React.Component {
UNSAFE_componentWillMount() {
expect(findDOMNode(this)).toBeNull();
}
render() {
return <div />;
}
}
expect(() => renderIntoDetachedNode(<Bar />)).not.toThrow();
});
});