Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Don't setState on an unmounted ad.
Browse files Browse the repository at this point in the history
  • Loading branch information
ctbarna committed Jul 31, 2018
1 parent 60f6ce0 commit 9425436
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Bling.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,20 @@ class Bling extends Component {
}
}

isAdMounted() {
return Bling._adManager.getMountedInstances().indexOf(this) !== -1;
}

onScriptLoaded() {
const { onScriptLoaded } = this.props;

if (this.getRenderWhenViewable()) {
this.foldCheck();
}
this.setState({ scriptLoaded: true }, onScriptLoaded); // eslint-disable-line react/no-did-mount-set-state

if (this.isAdMounted()) {
this.setState({ scriptLoaded: true }, onScriptLoaded); // eslint-disable-line react/no-did-mount-set-state
}
}

onScriptError(err) {
Expand Down
15 changes: 15 additions & 0 deletions test/Bling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,21 @@ describe("Bling", () => {
ReactTestUtils.renderIntoDocument(<Wrapper />);
});

it("does not call props.onSlotLoaded if it has been unmounted", () => {
const onScriptLoaded = sinon.stub();
const instance = ReactTestUtils.renderIntoDocument(
<Bling
adUnitPath="/4595/nfl.test.open"
slotSize={[300, 250]}
onScriptLoaded={onScriptLoaded}
/>
);
onScriptLoaded.reset();
instance.componentWillUnmount();
instance.onScriptLoaded();
expect(onScriptLoaded.called).to.be.false;
});

it("removes itself from registry when unmounted", () => {
const instance = ReactTestUtils.renderIntoDocument(
<Bling adUnitPath="/4595/nfl.test.open" slotSize={[300, 250]} />
Expand Down

0 comments on commit 9425436

Please sign in to comment.