);
});
it('should not be appearing in normal enter mode', done => {
let count = 0;
+ const nodeRef = React.createRef()
mount(
-
+
).setProps({
in: true,
- onEnter(node, isAppearing){
+ onEnter(isAppearing){
count++;
expect(isAppearing).toEqual(false);
- expect(node.className).toEqual('not-appear-test-enter');
+ expect(nodeRef.current.className).toEqual('not-appear-test-enter');
},
- onEntering(node, isAppearing){
+ onEntering(isAppearing){
count++;
expect(isAppearing).toEqual(false);
- expect(node.className).toEqual('not-appear-test-enter not-appear-test-enter-active');
+ expect(nodeRef.current.className).toEqual('not-appear-test-enter not-appear-test-enter-active');
},
- onEntered(node, isAppearing){
+ onEntered(isAppearing){
expect(isAppearing).toEqual(false);
- expect(node.className).toEqual('not-appear-test-enter-done');
+ expect(nodeRef.current.className).toEqual('not-appear-test-enter-done');
expect(count).toEqual(2);
done();
}
@@ -224,9 +237,11 @@ describe('CSSTransition', () => {
});
it('should not enter the transition states when appear=false', () => {
+ const nodeRef = React.createRef()
mount(
{
throw Error('Entred called!')
}}
>
-
+
);
});
@@ -249,16 +264,18 @@ describe('CSSTransition', () => {
});
describe('exiting', ()=> {
- let instance;
+ let wrapper, nodeRef;
beforeEach(() => {
- instance = mount(
+ nodeRef = React.createRef()
+ wrapper = mount(
-
+
)
});
@@ -266,21 +283,21 @@ describe('CSSTransition', () => {
it('should apply classes at each transition state', done => {
let count = 0;
- instance.setProps({
+ wrapper.setProps({
in: false,
- onExit(node){
+ onExit(){
count++;
- expect(node.className).toEqual('test-exit');
+ expect(nodeRef.current.className).toEqual('test-exit');
},
- onExiting(node){
+ onExiting(){
count++;
- expect(node.className).toEqual('test-exit test-exit-active');
+ expect(nodeRef.current.className).toEqual('test-exit test-exit-active');
},
- onExited(node){
- expect(node.className).toEqual('test-exit-done');
+ onExited(){
+ expect(nodeRef.current.className).toEqual('test-exit-done');
expect(count).toEqual(2);
done();
}
@@ -289,9 +306,11 @@ describe('CSSTransition', () => {
it('should apply custom classNames names', done => {
let count = 0;
- instance = mount(
+ const nodeRef = React.createRef()
+ wrapper = mount(
{
exitDone: 'custom-super-done',
}}
>
-
+
);
- instance.setProps({
+ wrapper.setProps({
in: false,
- onExit(node){
+ onExit() {
count++;
- expect(node.className).toEqual('custom');
+ expect(nodeRef.current.className).toEqual('custom');
},
- onExiting(node){
+ onExiting() {
count++;
- expect(node.className).toEqual('custom custom-super-active');
+ expect(nodeRef.current.className).toEqual('custom custom-super-active');
},
- onExited(node){
- expect(node.className).toEqual('custom-super-done');
+ onExited() {
+ expect(nodeRef.current.className).toEqual('custom-super-done');
expect(count).toEqual(2);
done();
}
@@ -327,30 +346,32 @@ describe('CSSTransition', () => {
it('should support empty prefix', done => {
let count = 0;
- const instance = mount(
+ const nodeRef = React.createRef()
+ const wrapper = mount(
-
+
)
- instance.setProps({
+ wrapper.setProps({
in: false,
- onExit(node){
+ onExit() {
count++;
- expect(node.className).toEqual('exit');
+ expect(nodeRef.current.className).toEqual('exit');
},
- onExiting(node){
+ onExiting() {
count++;
- expect(node.className).toEqual('exit exit-active');
+ expect(nodeRef.current.className).toEqual('exit exit-active');
},
- onExited(node){
- expect(node.className).toEqual('exit-done');
+ onExited() {
+ expect(nodeRef.current.className).toEqual('exit-done');
expect(count).toEqual(2);
done();
}
@@ -359,11 +380,11 @@ describe('CSSTransition', () => {
});
describe('reentering', () => {
- it('should remove dynamically applied classes', done => {
+ it('should remove dynamically applied classes', async () => {
let count = 0;
class Test extends React.Component {
render() {
- const { direction, text, ...props } = this.props;
+ const { direction, text, nodeRef, ...props } = this.props;
return (
{
- {text}
+ {text}
)
}
}
- const instance = mount(
)
+ const nodeRef = {
+ foo: React.createRef(),
+ bar: React.createRef(),
+ }
+
+ const wrapper = mount(
)
const rerender = getProps => new Promise(resolve =>
- instance.setProps({
+ wrapper.setProps({
onEnter: undefined,
onEntering: undefined,
onEntered: undefined,
@@ -400,40 +427,39 @@ describe('CSSTransition', () => {
})
);
- Promise.resolve().then(() =>
- rerender(resolve => ({
- direction: 'up',
- text: 'bar',
+ await rerender(resolve => ({
+ direction: 'up',
+ text: 'bar',
+ nodeRef: nodeRef.bar,
- onEnter(node) {
- count++;
- expect(node.className).toEqual('up-enter');
- },
- onEntering(node) {
- count++;
- expect(node.className).toEqual('up-enter up-enter-active');
- resolve()
- }
- }))
- ).then(() => {
- return rerender(resolve => ({
- direction: 'down',
- text: 'foo',
-
- onEntering(node) {
- count++;
- expect(node.className).toEqual('down-enter down-enter-active');
- },
- onEntered(node) {
- count++;
- expect(node.className).toEqual('down-enter-done');
- resolve();
- }
- }))
- }).then(() => {
- expect(count).toEqual(4);
- done();
- });
+ onEnter() {
+ count++;
+ expect(nodeRef.bar.current.className).toEqual('up-enter');
+ },
+ onEntering() {
+ count++;
+ expect(nodeRef.bar.current.className).toEqual('up-enter up-enter-active');
+ resolve()
+ }
+ }))
+
+ await rerender(resolve => ({
+ direction: 'down',
+ text: 'foo',
+ nodeRef: nodeRef.foo,
+
+ onEntering() {
+ count++;
+ expect(nodeRef.foo.current.className).toEqual('down-enter down-enter-active');
+ },
+ onEntered() {
+ count++;
+ expect(nodeRef.foo.current.className).toEqual('down-enter-done');
+ resolve();
+ }
+ }))
+
+ expect(count).toEqual(4);
});
});
});
diff --git a/test/CSSTransitionGroup-test.js b/test/CSSTransitionGroup-test.js
index 4d0decbf..2776a8e0 100644
--- a/test/CSSTransitionGroup-test.js
+++ b/test/CSSTransitionGroup-test.js
@@ -12,9 +12,10 @@ describe('CSSTransitionGroup', () => {
let consoleErrorSpy;
function YoloTransition({ id, ...props }) {
+ const nodeRef = React.useRef()
return (
-
-
+
+
)
}
diff --git a/test/SwitchTransition-test.js b/test/SwitchTransition-test.js
index 744e2967..76d2a527 100644
--- a/test/SwitchTransition-test.js
+++ b/test/SwitchTransition-test.js
@@ -10,20 +10,21 @@ describe('SwitchTransition', () => {
beforeEach(() => {
log = [];
let events = {
- onEnter: (_, m) => log.push(m ? 'appear' : 'enter'),
- onEntering: (_, m) => log.push(m ? 'appearing' : 'entering'),
- onEntered: (_, m) => log.push(m ? 'appeared' : 'entered'),
+ onEnter: (m) => log.push(m ? 'appear' : 'enter'),
+ onEntering: (m) => log.push(m ? 'appearing' : 'entering'),
+ onEntered: (m) => log.push(m ? 'appeared' : 'entered'),
onExit: () => log.push('exit'),
onExiting: () => log.push('exiting'),
onExited: () => log.push('exited')
};
+ const nodeRef = React.createRef()
Parent = function Parent({ on, rendered = true }) {
return (
{rendered ? (
-
-
+
+
) : null}
@@ -32,10 +33,11 @@ describe('SwitchTransition', () => {
});
it('should have default status ENTERED', () => {
+ const nodeRef = React.createRef()
const wrapper = mount(
-
-
+
+
);
@@ -44,10 +46,11 @@ describe('SwitchTransition', () => {
});
it('should have default mode: out-in', () => {
+ const nodeRef = React.createRef()
const wrapper = mount(
-
-
+
+
);
@@ -56,11 +59,12 @@ describe('SwitchTransition', () => {
});
it('should work without childs', () => {
+ const nodeRef = React.createRef()
expect(() => {
mount(
-
-
+
+
);
diff --git a/test/Transition-test.js b/test/Transition-test.js
index bc31c926..c3690fa3 100644
--- a/test/Transition-test.js
+++ b/test/Transition-test.js
@@ -1,4 +1,5 @@
import React from 'react'
+import ReactDOM from 'react-dom'
import { mount } from 'enzyme'
@@ -25,15 +26,17 @@ expect.extend({
describe('Transition', () => {
it('should not transition on mount', () => {
+ const nodeRef = React.createRef()
let wrapper = mount(
{
throw new Error('should not Enter')
}}
>
-
+
)
@@ -41,21 +44,23 @@ describe('Transition', () => {
})
it('should transition on mount with `appear`', done => {
+ const nodeRef = React.createRef()
mount(
{
throw Error('Animated!')
}}
>
-
+
)
mount(
- done()}>
-
+ done()}>
+
)
})
@@ -63,14 +68,16 @@ describe('Transition', () => {
it('should pass filtered props to children', () => {
class Child extends React.Component {
render() {
- return child
+ return child
}
}
+ const nodeRef = React.createRef()
const child = mount(
{
onExiting={() => {}}
onExited={() => {}}
>
-
+
).find(Child)
- expect(child.props()).toEqual({ foo: 'foo', bar: 'bar' })
+ expect(child.props()).toEqual({ foo: 'foo', bar: 'bar', nodeRef })
})
it('should allow addEndListener instead of timeouts', done => {
- let listener = jest.fn((node, end) => setTimeout(end, 0))
+ let listener = jest.fn(end => setTimeout(end, 0))
- let inst = mount(
+ const nodeRef = React.createRef()
+ let wrapper = mount(
{
expect(listener).toHaveBeenCalledTimes(1)
done()
}}
>
-
+
)
- inst.setProps({ in: true })
+ wrapper.setProps({ in: true })
})
it('should fallback to timeouts with addEndListener', done => {
let calledEnd = false
- let listener = (node, end) =>
+ let listener = (end) =>
setTimeout(() => {
calledEnd = true
end()
}, 100)
- let inst = mount(
+ const nodeRef = React.createRef()
+ let wrapper = mount(
{
expect(calledEnd).toEqual(false)
done()
}}
>
-
+
)
- inst.setProps({ in: true })
+ wrapper.setProps({ in: true })
})
it('should mount/unmount immediately if not have enter/exit timeout', (done) => {
+ const nodeRef = React.createRef()
const wrapper = mount(
-
-
+
+
)
@@ -158,15 +170,45 @@ describe('Transition', () => {
})
})
+ it('should use `React.findDOMNode` when `nodeRef` is not provided', () => {
+ const consoleSpy = jest.spyOn(console, 'error').mockImplementation()
+ const findDOMNodeSpy = jest.spyOn(ReactDOM, 'findDOMNode')
+
+ mount(
+
+
+
+ )
+
+ expect(findDOMNodeSpy).toHaveBeenCalled();
+ findDOMNodeSpy.mockRestore()
+ consoleSpy.mockRestore()
+ })
+
+ it('should not use `React.findDOMNode` when `nodeRef` is provided', () => {
+ const findDOMNodeSpy = jest.spyOn(ReactDOM, 'findDOMNode')
+
+ const nodeRef = React.createRef()
+ mount(
+
+
+
+ )
+
+ expect(findDOMNodeSpy).not.toHaveBeenCalled();
+ findDOMNodeSpy.mockRestore()
+ })
+
describe('appearing timeout', () => {
it('should use enter timeout if appear not set', done => {
let calledBeforeEntered = false
setTimeout(() => {
calledBeforeEntered = true
}, 10)
+ const nodeRef = React.createRef()
const wrapper = mount(
-
-
+
+
)
@@ -182,9 +224,10 @@ describe('Transition', () => {
})
it('should use appear timeout if appear is set', done => {
+ const nodeRef = React.createRef()
const wrapper = mount(
-
-
+
+
)
@@ -206,12 +249,13 @@ describe('Transition', () => {
})
describe('entering', () => {
- let wrapper
+ let wrapper, nodeRef;
beforeEach(() => {
+ nodeRef = React.createRef()
wrapper = mount(
-
-
+
+
)
})
@@ -267,12 +311,13 @@ describe('Transition', () => {
})
describe('exiting', () => {
- let wrapper
+ let wrapper, nodeRef;
beforeEach(() => {
+ nodeRef = React.createRef()
wrapper = mount(
-
-
+
+
)
})
@@ -329,10 +374,8 @@ describe('Transition', () => {
describe('mountOnEnter', () => {
class MountTransition extends React.Component {
- constructor(props) {
- super(props)
- this.state = { in: props.initialIn }
- }
+ nodeRef = React.createRef()
+ state = { in: this.props.initialIn }
render() {
const { ...props } = this.props
@@ -341,12 +384,13 @@ describe('Transition', () => {
return (
this.transition = this.transition || transition}
+ nodeRef={this.nodeRef}
mountOnEnter
in={this.state.in}
timeout={10}
{...props}
>
-
+
)
}
@@ -362,7 +406,7 @@ describe('Transition', () => {
initialIn={false}
onEnter={() => {
expect(wrapper.instance().getStatus()).toEqual(EXITED)
- expect(wrapper.getDOMNode()).toExist()
+ expect(wrapper.instance().nodeRef.current).toExist()
done()
}}
/>
@@ -370,7 +414,7 @@ describe('Transition', () => {
expect(wrapper.instance().getStatus()).toEqual(UNMOUNTED)
- expect(wrapper.getDOMNode()).not.toExist()
+ expect(wrapper.instance().nodeRef.current).not.toExist()
wrapper.setProps({ in: true })
})
@@ -381,27 +425,27 @@ describe('Transition', () => {
initialIn={false}
onEntered={() => {
expect(wrapper.instance().getStatus()).toEqual(ENTERED)
- expect(wrapper.getDOMNode()).toExist()
+ expect(wrapper.instance().nodeRef.current).toExist()
wrapper.setState({ in: false })
}}
onExited={() => {
expect(wrapper.instance().getStatus()).toEqual(EXITED)
- expect(wrapper.getDOMNode()).toExist()
+ expect(wrapper.instance().nodeRef.current).toExist()
done()
}}
/>
)
- expect(wrapper.getDOMNode()).not.toExist()
+ expect(wrapper.instance().nodeRef.current).not.toExist()
wrapper.setState({ in: true })
})
})
describe('unmountOnExit', () => {
class UnmountTransition extends React.Component {
- divRef = React.createRef()
+ nodeRef = React.createRef()
state = { in: this.props.initialIn }
render() {
@@ -411,12 +455,13 @@ describe('Transition', () => {
return (
this.transition = this.transition || transition}
+ nodeRef={this.nodeRef}
unmountOnExit
in={this.state.in}
timeout={10}
{...props}
>
-
+
)
}
@@ -427,42 +472,42 @@ describe('Transition', () => {
}
it('should mount when entering', done => {
- const wrapper = mount(
+ const instance = mount(
{
- expect(wrapper.getStatus()).toEqual(EXITED)
- expect(wrapper.divRef.current).toExist()
+ expect(instance.getStatus()).toEqual(EXITED)
+ expect(instance.nodeRef.current).toExist()
done()
}}
/>
).instance()
- expect(wrapper.getStatus()).toEqual(UNMOUNTED)
- expect(wrapper.divRef.current).toBeNull()
+ expect(instance.getStatus()).toEqual(UNMOUNTED)
+ expect(instance.nodeRef.current).toBeNull()
- wrapper.setState({ in: true })
+ instance.setState({ in: true })
})
it('should unmount after exiting', done => {
- const wrapper = mount(
+ const instance = mount(
{
setTimeout(() => {
- expect(wrapper.getStatus()).toEqual(UNMOUNTED)
- expect(wrapper.divRef.current).not.toExist()
+ expect(instance.getStatus()).toEqual(UNMOUNTED)
+ expect(instance.nodeRef.current).not.toExist()
done()
})
}}
/>
).instance()
- expect(wrapper.getStatus()).toEqual(ENTERED)
- expect(wrapper.divRef.current).toExist()
+ expect(instance.getStatus()).toEqual(ENTERED)
+ expect(instance.nodeRef.current).toExist()
- wrapper.setState({ in: false })
+ instance.setState({ in: false })
})
})
})
diff --git a/test/TransitionGroup-test.js b/test/TransitionGroup-test.js
index 91ed167d..f921ae40 100644
--- a/test/TransitionGroup-test.js
+++ b/test/TransitionGroup-test.js
@@ -20,18 +20,19 @@ describe('TransitionGroup', () => {
log = []
let events = {
- onEnter: (_, m) => log.push(m ? 'appear' : 'enter'),
- onEntering: (_, m) => log.push(m ? 'appearing' : 'entering'),
- onEntered: (_, m) => log.push(m ? 'appeared' : 'entered'),
+ onEnter: (m) => log.push(m ? 'appear' : 'enter'),
+ onEntering: (m) => log.push(m ? 'appearing' : 'entering'),
+ onEntered: (m) => log.push(m ? 'appeared' : 'entered'),
onExit: () => log.push('exit'),
onExiting: () => log.push('exiting'),
onExited: () => log.push('exited'),
}
+ const nodeRef = React.createRef()
Child = function Child(props) {
return (
-
-
+
+
)
}
diff --git a/test/setup.js b/test/setup.js
index 4b7566f9..f053547a 100644
--- a/test/setup.js
+++ b/test/setup.js
@@ -1,3 +1,4 @@
+import React from 'react'
global.requestAnimationFrame = function(callback) {
setTimeout(callback, 0);
@@ -6,4 +7,7 @@ global.requestAnimationFrame = function(callback) {
const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
-Enzyme.configure({ adapter: new Adapter() })
+Enzyme.configure({
+ adapter: new Adapter(),
+ wrappingComponent: props =>
+})