fix(CountDown): should clear timer when deactivated (#4919)

This commit is contained in:
neverland 2019-11-04 20:32:39 +08:00 committed by GitHub
parent e0e597d164
commit b630b6b035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 7 deletions

View File

@ -44,6 +44,21 @@ export default createComponent({
} }
}, },
activated() {
if (this.keepAlivePaused) {
this.counting = true;
this.keepAlivePaused = false;
this.tick();
}
},
deactivated() {
if (this.counting) {
this.pause();
this.keepAlivePaused = true;
}
},
beforeDestroy() { beforeDestroy() {
this.pause(); this.pause();
}, },

View File

@ -1,6 +1,9 @@
import Vue from 'vue';
import CountDown from '..'; import CountDown from '..';
import { mount, later } from '../../../test/utils'; import { mount, later } from '../../../test/utils';
Vue.use(CountDown);
test('macro task finish event', async () => { test('macro task finish event', async () => {
const wrapper = mount(CountDown, { const wrapper = mount(CountDown, {
propsData: { propsData: {
@ -150,15 +153,42 @@ test('incomplate format prop', () => {
expect(wrapper).toMatchSnapshot(); expect(wrapper).toMatchSnapshot();
}); });
test('pause when destroyed', async () => { test('pause when destroyed', () => {
const pause = jest.fn(); const wrapper = mount(CountDown);
const wrapper = mount(CountDown, { expect(wrapper.vm.counting).toBeTruthy();
mocks: { wrapper.destroy();
pause expect(wrapper.vm.counting).toBeFalsy();
});
test('pause when deactivated', async () => {
const wrapper = mount({
template: `
<keep-alive>
<van-count-down v-if="render" ref="countDown" time="100" />
</keep-alive>
`,
data() {
return {
render: true
};
},
methods: {
getCountDown() {
return this.$refs.countDown;
}
} }
}); });
wrapper.destroy(); const countDown = wrapper.vm.getCountDown();
expect(countDown.counting).toBeTruthy();
expect(wrapper.vm.counting).toBeFalsy(); wrapper.setData({ render: false });
expect(countDown.counting).toBeFalsy();
wrapper.setData({ render: true });
expect(countDown.counting).toBeTruthy();
countDown.pause();
wrapper.setData({ render: false });
wrapper.setData({ render: true });
expect(countDown.counting).toBeFalsy();
}); });