File tree Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Original file line number Diff line number Diff 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 >
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments