-
-
Notifications
You must be signed in to change notification settings - Fork 158
Added contentWillUnmount prop to call before the Contents unmounts. #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
60a04d6
d7bceae
08a9e14
b15f3c8
7d4418a
b5099c7
e1fa1b4
9bd4b98
58d01e4
84443a4
208e7a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import ReactDOM from 'react-dom'; | ||
import ReactDOM, { unmountComponentAtNode } from 'react-dom'; | ||
import ReactTestUtils from 'react-dom/test-utils'; | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon/pkg/sinon'; | ||
|
@@ -313,6 +313,55 @@ describe('The Frame Component', () => { | |
); | ||
}); | ||
|
||
it('should call contentWillUnmount on unmount', done => { | ||
div = document.body.appendChild(document.createElement('div')); | ||
const willUnmount = sinon.spy(); | ||
|
||
const Thing = () => ( | ||
<Frame contentWillUnmount={willUnmount}> | ||
<div className="test-class-1" /> | ||
</Frame> | ||
); | ||
|
||
ReactDOM.render(<Thing />, div); | ||
|
||
// TODO: act() doesn'tt seem to solve this something weird??? | ||
setTimeout(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Reflex-Gravity not sure why |
||
unmountComponentAtNode(div); | ||
expect(willUnmount.callCount).to.equal(1, 'expected 1 willUnmount'); | ||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('should error when null is passed in contentWillUnmount', () => { | ||
div = document.body.appendChild(document.createElement('div')); | ||
ReactDOM.render(<Frame contentWillUnmount={null} />, div); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also these tests should assert something? |
||
}); | ||
|
||
it('should not error when contentWillUnmount is not passed', done => { | ||
div = document.body.appendChild(document.createElement('div')); | ||
const didUpdate = sinon.spy(); | ||
const didMount = sinon.spy(); | ||
const frame = ReactDOM.render( | ||
<Frame | ||
contentDidUpdate={didUpdate} | ||
contentDidMount={() => { | ||
didMount(); | ||
frame.setState({ foo: 'bar' }, () => { | ||
expect(didMount.callCount).to.equal(1, 'expected 1 didMount'); | ||
expect(didUpdate.callCount).to.equal(1, 'expected 1 didUpdate'); | ||
frame.setState({ foo: 'gah' }, () => { | ||
expect(didMount.callCount).to.equal(1, 'expected 1 didMount'); | ||
expect(didUpdate.callCount).to.equal(2, 'expected 2 didUpdate'); | ||
done(); | ||
}); | ||
}); | ||
}} | ||
/>, | ||
div | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here nothing is asserted? |
||
}); | ||
|
||
it('should return first child element of the `body` on call to `this.getMountTarget()` if `props.mountTarget` was not passed in', () => { | ||
div = document.body.appendChild(document.createElement('div')); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this is called but this test doesn't check the
callCount
either?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I just need to check the
callCount
to be 1 ryt?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated this test. But, the test is failing. The
contentWillUnmount
isn't getting called, maybe because the unmounting of the component in the test isn't happening properly.I don't exactly get the reason for this. The implementation of the feature seems to be correct and working. But the way I wrote the test is what I guess is wrong. Can you help me here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so i think what we're doing here is actually the wrong thing, the issue is about having a callback when the
<Frame>
component unmounts and not when the contents within the iframe unmount.So I think what we need is a bit simpler instead of passing the
contentWillUnmount
prop down through to<Content>
we just need to call it inside<Frame>
'scomponentWillUnmount
callback and we should probably rename it toframeWillUnmount
to differentiate it from the other content lifecycle events.Then you can just test it like this:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. But still
contentWillUnmount
should be called as Content gets unmounted too.I tried it in an example, the
contentWillUnmount
was called when I unmounted the Frame. The test however doesn't trigger the callback.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah ok having though on this more I think what you have is useful in two instances of changing the iframe contents as well as unmounting the entire Frame too.
I honestly have no idea why this isn't firing in test env, if you run
npm run karma:dev
that will allow you to debug the tests within Chrome dev tools.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried with
npm run karma:dev
same issue. How do I proceed? Skip this test?