Skip to content

Commit

Permalink
test(useProgress): add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nemo-shen committed Jan 14, 2024
1 parent 5225c5c commit 69a21ba
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
80 changes: 79 additions & 1 deletion packages/core/useProgress/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,86 @@
import { describe, expect, test } from 'vitest'
import { vi, describe, expect, test } from 'vitest'
import { useProgress } from '.'

const wait = (time) =>
new Promise((resolve) => {
setTimeout(resolve, time)
})

describe('useEllipsis', () => {
test('should be defined', () => {
expect(useProgress).toBeDefined()
})

test('should increment correctly', () => {
const { progress, increment } = useProgress()
increment()
expect(progress.value).toBe(1)

increment(101)
expect(progress.value).toBe(100)
})

test('should decrement correctly', () => {
const { progress, decrement } = useProgress({ initialValue: 10 })

decrement()
expect(progress.value).toBe(9)

decrement(10)
expect(progress.value).toBe(0)
})

test('should progress correctly with maxValue', () => {
const { progress, increment } = useProgress({ maxValue: 1000 })

increment()
expect(progress.value).toBe(0.1)
})

test('should onChange called after change progress', async () => {
const onChange = vi.fn()

const { increment } = useProgress({ onChange })

await increment()
expect(onChange).toHaveBeenCalledTimes(1)

await increment(100)
expect(onChange).toHaveBeenCalledTimes(2)

await increment(100)
expect(onChange).toHaveBeenCalledTimes(2)
})

test('should auto increment progress', async () => {
const { progress, startAutoIncrement } = useProgress({
autoIncrementRules: [{ before: 100, delay: 1, increment: 1 }],
})

expect(progress.value).toBe(0)

startAutoIncrement()

await wait(200)
expect(progress.value).toBe(99)
})

test('should reset progress', async () => {
const { progress, increment, reset } = useProgress()

increment()
expect(progress.value).toBe(1)

reset()
expect(progress.value).toBe(0)
})

test('should isComplete correctly', async () => {
const { isComplete, increment } = useProgress()

expect(isComplete.value).toBeFalsy()

increment(100)
expect(isComplete.value).toBeTruthy()
})
})
6 changes: 3 additions & 3 deletions packages/core/useProgress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const useProgress = (options: UseProgressOptions = {}) => {
autoIncrementRules = [],
onChange,
} = options
const currentValue = ref(0)
const currentValue = ref(initialValue)
const progress = computed(() =>
parseFloat(((currentValue.value / maxValue) * 100).toFixed(2))
)
Expand All @@ -37,9 +37,9 @@ export const useProgress = (options: UseProgressOptions = {}) => {
currentValue.value = Math.min(maxValue, currentValue.value + value)
}

const decrement = (value: number) => {
const decrement = (value: number = 1) => {
if (currentValue.value <= 0) return
currentValue.value = Math.min(0, currentValue.value - value)
currentValue.value = Math.max(0, currentValue.value - value)
}

const reset = () => {
Expand Down

0 comments on commit 69a21ba

Please sign in to comment.