Skip to content
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

Added contentWillUnmount prop to call before the Contents unmounts. #185

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

Reflex-Gravity
Copy link

This PR contains a new feature which adds a new prop contentWillUnmount that calls before the Content unmounts.
This PR is linked to #178 .

I have also added tests for this feature.

@paztis
Copy link

paztis commented Apr 30, 2021

The componentWillUnmount poperty mught be optional. Can you add a null test on this.props.componentWillUnmount before execution ?

Copy link
Author

@Reflex-Gravity Reflex-Gravity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed contentWillUnmount from the required props, and set the default value of contentWillUnmount to null. I have also added tests to support this.

@Reflex-Gravity
Copy link
Author

@paztis Anything else I need to change? Is there something that is blocking from merging?

@paztis
Copy link

paztis commented May 4, 2021 via email

Copy link
Owner

@ryanseddon ryanseddon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good just some feedback

src/Content.jsx Outdated Show resolved Hide resolved
src/Content.jsx Outdated Show resolved Hide resolved
test/Content.spec.jsx Outdated Show resolved Hide resolved
test/Content.spec.jsx Outdated Show resolved Hide resolved
test/Content.spec.jsx Outdated Show resolved Hide resolved
test/Content.spec.jsx Outdated Show resolved Hide resolved
test/Frame.spec.jsx Outdated Show resolved Hide resolved
@ryanseddon
Copy link
Owner

@Reflex-Gravity some feedback for you before we can merge

@Reflex-Gravity Reflex-Gravity requested a review from ryanseddon May 29, 2021 06:07
src/Content.jsx Outdated Show resolved Hide resolved
test/Content.spec.jsx Outdated Show resolved Hide resolved
@@ -313,6 +313,63 @@ describe('The Frame Component', () => {
);
});

it('should call contentWillUnmount on unmount', done => {
div = document.body.appendChild(document.createElement('div'));
const willUnmount = sinon.spy();
Copy link
Owner

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?

Copy link
Author

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?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep

Copy link
Author

@Reflex-Gravity Reflex-Gravity Jun 1, 2021

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?

Copy link
Owner

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>'s componentWillUnmount callback and we should probably rename it to frameWillUnmount to differentiate it from the other content lifecycle events.

Then you can just test it like this:

  it('should call contentWillUnmount on unmount', done => {
    div = document.body.appendChild(document.createElement('div'));
    const willUnmount = sinon.spy();
    const frame = ReactDOM.render(
      <Frame frameWillUnmount={willUnmount} />,
      div
    );
    ReactDOM.unmountComponentAtNode(ReactDOM.findDOMNode(frame).parentNode);
    expect(willUnmount.callCount).to.equal(1, 'expected 1 willUnmount');
    done();
  });

Copy link
Author

@Reflex-Gravity Reflex-Gravity Jun 3, 2021

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.

Copy link
Owner

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.

Copy link
Author

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?

@Reflex-Gravity Reflex-Gravity requested a review from ryanseddon June 1, 2021 21:23

wrapper.setState({ isClosed: true }, () => {
// TODO: act() doesn'tt seem to solve this something weird???
setTimeout(() => {
Copy link
Owner

@ryanseddon ryanseddon Jun 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Reflex-Gravity not sure why act() didn't work but this might give you a clue as to what's happening here, srcdoc is async and act doesn't seem to wait long enough for the iframe to actually mount but doing the old setTimeout next tick trick does...


it('should error when null is passed in contentWillUnmount', () => {
div = document.body.appendChild(document.createElement('div'));
ReactDOM.render(<Frame contentWillUnmount={null} />, div);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also these tests should assert something?

}}
/>,
div
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here nothing is asserted?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants