Skip to content

Commit 4fb08b9

Browse files
committed
Add destroyed method to Clock
1 parent 9c6c675 commit 4fb08b9

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Clock.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,26 @@ function getHour() {
2424
return padZero(getDate().getHours());
2525
}
2626
27-
const clock = {
27+
export default{
2828
name: 'clock',
29+
2930
data() {
3031
return {
32+
ticker: null,
3133
minutes: getMinutes(),
3234
hours: getHour(),
3335
};
3436
},
37+
3538
created() {
36-
setInterval(() => {
39+
this.ticker = setInterval(() => {
3740
this.minutes = getMinutes();
3841
this.hours = getHour();
3942
}, 1000);
4043
},
41-
};
4244
43-
export default clock;
45+
destroyed() {
46+
clearInterval(this.ticker);
47+
},
48+
};
4449
</script>

test/unit/specs/Clock.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ describe('Clock.vue', () => {
5959
clock.tick(4 * minutes);
6060
setTimeout(() => expect(wrapper.find('.clock__minutes')[0].text()).to.equal('04'), 1000);
6161
});
62+
it('Calls clear input with vm.ticker when component is destroyed', () => {
63+
const stub = sinon.stub();
64+
window.clearInterval = stub;
65+
const wrapper = mount(Clock);
66+
const ticker = wrapper.vm.ticker;
67+
wrapper.destroy();
68+
expect(stub.args[0][0]).to.equal(ticker);
69+
});
6270
});

0 commit comments

Comments
 (0)