Skip to content

Commit

Permalink
refactor(maz-ui): MazAnimateCounter - make it SSR friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisMazel committed Dec 8, 2024
1 parent a708a56 commit 6187cc2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
26 changes: 22 additions & 4 deletions packages/lib/components/MazAnimatedCounter.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts" setup>
import { ref, watch } from 'vue'
import { isClient } from '../modules/helpers/is-client'
const props = withDefaults(
defineProps<{
Expand Down Expand Up @@ -36,13 +37,30 @@ const props = withDefaults(
const currentCount = ref(0)
function getRequestAnimationFrame() {
if (typeof window === 'undefined' || !window.requestAnimationFrame) {
return (callback: FrameRequestCallback): number => {
setTimeout(callback, 1000 / 60)
return 0
}
}
return window.requestAnimationFrame.bind(window)
}
function animate(start: number, end: number, duration: number, delay: number) {
currentCount.value = start
const requestAnim = getRequestAnimationFrame()
if (!isClient()) {
currentCount.value = end
return
}
currentCount.value = start
setTimeout(() => {
const startTime = performance.now()
const updateCount = (currentTime: number) => {
const updateCount = (currentTime: number = performance.now()) => {
const elapsed = currentTime - startTime
const progress = Math.min(elapsed / duration, 1)
Expand All @@ -53,11 +71,11 @@ function animate(start: number, end: number, duration: number, delay: number) {
)
if (progress < 1) {
requestAnimationFrame(updateCount)
requestAnim(updateCount)
}
}
requestAnimationFrame(updateCount)
requestAnim(updateCount)
}, delay)
}
Expand Down
27 changes: 3 additions & 24 deletions packages/lib/tests/specs/components/MazAnimatedCounter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,27 @@ import MazAnimatedCounter from '@components/MazAnimatedCounter.vue'
import { shallowMount } from '@vue/test-utils'

describe('mazAnimatedCounter', () => {
it('renders initial count with prefix and suffix', async () => {
it('renders initial count with prefix and suffix', () => {
const wrapper = shallowMount(MazAnimatedCounter, {
props: { count: 10, prefix: '$' },
})

expect(wrapper.find('.maz-sr-only').text()).toBe('$10')
expect(wrapper.text()).toBe('$10$0')

await new Promise(resolve => setTimeout(resolve, 100))
expect(wrapper.text()).toBe('$10$0')
})

it('updates count and triggers animation', async () => {
it('updates count and triggers animation', () => {
const wrapper = shallowMount(MazAnimatedCounter, {
props: { count: 10, suffix: '%' },
})

expect(wrapper.find('.maz-sr-only').text()).toBe('10%')
expect(wrapper.text()).toBe('10%0%')

await new Promise(resolve => setTimeout(resolve, 100))
expect(wrapper.text()).toBe('10%0%')

await wrapper.setProps({ count: 20, suffix: '%' })
expect(wrapper.find('.maz-sr-only').text()).toBe('20%')
expect(wrapper.text()).toBe('20%10%')

// await new Promise(resolve => setTimeout(resolve, 100))
// expect(wrapper.text()).toBe('20%-52100%')
})

it('respects delay prop', async () => {
it('respects delay prop', () => {
const wrapper = shallowMount(MazAnimatedCounter, {
props: { count: 10, delay: 200 },
})

expect(wrapper.text()).toBe('100')

await new Promise(resolve => setTimeout(resolve, 100))
expect(wrapper.text()).toBe('100')

// await new Promise(resolve => setTimeout(resolve, 200))
// expect(wrapper.html()).toBe('10')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ describe('mazCircularProgressBar', () => {
it('updates percentage and triggers animation', async () => {
await wrapper.setProps({ suffix: '%' })
// Vérifier que le pourcentage initial est correct
expect(wrapper.find('.maz-sr-only').text()).toContain('50 %')
expect(wrapper.find('.maz-sr-only').text()).toContain('50%')

// Mettre à jour le pourcentage
await wrapper.setProps({ percentage: 75 })

// Attendre la fin de l'animation de mise à jour
await new Promise(resolve => setTimeout(resolve, 100))

// Vérifier que le texte mis à jour rendu contient le nouveau pourcentage
expect(wrapper.text()).toContain('75 %')
expect(wrapper.text()).toContain('75%')
})

it('renders with auto color', async () => {
Expand Down

0 comments on commit 6187cc2

Please sign in to comment.