fix(components): [countdown] SSR hydration error (#17554)

* fix(components): [countdown] SSR hydration error

* fix: test
This commit is contained in:
qiang
2024-07-19 18:23:04 +08:00
committed by GitHub
parent 0f93f11ad4
commit e99fda889b
2 changed files with 20 additions and 16 deletions

View File

@@ -20,8 +20,8 @@ describe('Countdown.vue', () => {
wrapper.unmount()
})
it('render test', () => {
wrapper = mount(() => (
it('render test', async () => {
wrapper = await mount(() => (
<Countdown title="test" value={Date.now() + 1000 * 60} />
))
@@ -35,7 +35,7 @@ describe('Countdown.vue', () => {
['DD [days] HH [hours] mm:ss', '02 days 02 hours 02:02'],
['HH:mm:ss', '50:02:02'],
['HH:mm:ss:SSS', '50:02:02:002'],
])('should work with %s', (format, expected) => {
])('should work with %s', async (format, expected) => {
const value = dayjs()
.add(2, 'd')
.add(2, 'h')
@@ -43,7 +43,7 @@ describe('Countdown.vue', () => {
.add(2, 's')
.add(2, 'ms')
wrapper = mount(() => <Countdown value={value} format={format} />)
wrapper = await mount(() => <Countdown value={value} format={format} />)
expect(wrapper.find(CONTENT_CLASS).text()).toBe(expected)
})

View File

@@ -13,7 +13,7 @@
</el-statistic>
</template>
<script lang="ts" setup>
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { ElStatistic } from '@element-plus/components/statistic'
import { cAF, rAF } from '@element-plus/utils'
import { countdownEmits, countdownProps } from './countdown'
@@ -27,7 +27,7 @@ const props = defineProps(countdownProps)
const emit = defineEmits(countdownEmits)
let timer: ReturnType<typeof rAF> | undefined
const rawValue = ref(getTime(props.value) - Date.now())
const rawValue = ref<number>(0)
const displayValue = computed(() => formatTime(rawValue.value, props.format))
const formatter = (val: number) => formatTime(val, props.format)
@@ -56,16 +56,20 @@ const startTimer = () => {
timer = rAF(frameFunc)
}
watch(
() => [props.value, props.format],
() => {
stopTimer()
startTimer()
},
{
immediate: true,
}
)
onMounted(() => {
rawValue.value = getTime(props.value) - Date.now()
watch(
() => [props.value, props.format],
() => {
stopTimer()
startTimer()
},
{
immediate: true,
}
)
})
onBeforeUnmount(() => {
stopTimer()