Skip to content

Instantly share code, notes, and snippets.

@benjamingr
Created October 22, 2018 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjamingr/3bd6f444d78c69b8931aafe8f159183e to your computer and use it in GitHub Desktop.
Save benjamingr/3bd6f444d78c69b8931aafe8f159183e to your computer and use it in GitHub Desktop.
const React = require('react');
const { expect } = require('chai');
const { shallow, configure } = require('enzyme');
const { createSandbox } = require('sinon');
require('react-dom'); // required by the enzyme adapter internally
const Adapter = require('enzyme-adapter-react-16');
configure({ adapter: new Adapter() });
var called = 0;
class Test extends React.Component {
doSomething() {
called++;
}
componentDidMount() {
this.intervalId = setInterval(this.doSomething, 1000);
console.log("Did Mount");
}
componentWillUnmount() {
debugger;
clearInterval(this.intervalId);
}
render() {
return React.createElement("div", null, null);
}
}
describe('<Test/>', () => {
const sandbox = createSandbox();
const ss = {};
before(() => {
ss.clock = sandbox.useFakeTimers({
loopLimit: 50,
});
});
afterEach(() => {
sandbox.resetHistory();
});
after(() => {
sandbox.restore();
});
it('should stop after unmount', () => {
shallow(React.createElement(Test, null, null))
.unmount();
console.log(ss.clock.timers);
debugger;
ss.clock.tick(10000);
console.log(ss.clock.timers);
expect(called).to.equal(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment