Skip to content

Commit

Permalink
feat: more purposeful test
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk authored and yyx990803 committed Apr 6, 2023
1 parent eb0f8fa commit f3a0071
Showing 1 changed file with 14 additions and 43 deletions.
57 changes: 14 additions & 43 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,54 +292,25 @@ describe('reactivity/computed', () => {
})

it('chained computed value on-demand trigger', () => {
const c1Spy = vi.fn()
const c2Spy = vi.fn()
const minSpy = vi.fn()
const hourSpy = vi.fn()

const src = ref(0)
const c1 = computed(() => {
c1Spy()
return src.value < 5
const sec = ref(0)
const min = computed(() => {
minSpy()
return Math.floor(sec.value / 60)
})
const c2 = computed(() => {
c2Spy()
return c1.value ? '< 5' : '>= 5'
const hour = computed(() => {
hourSpy()
return Math.floor(min.value / 60)
})

expect(c1Spy).toHaveBeenCalledTimes(0)
expect(c2Spy).toHaveBeenCalledTimes(0)

expect(src.value).toBe(0)
expect(c2.value).toBe('< 5')
expect(c1Spy).toHaveBeenCalledTimes(1)
expect(c2Spy).toHaveBeenCalledTimes(1)

src.value++
expect(c2.value).toBe('< 5')
expect(c1Spy).toHaveBeenCalledTimes(2)
expect(c2Spy).toHaveBeenCalledTimes(1)

for (let i = 0; i < 10; i++) {
src.value++
for (sec.value = 0; sec.value < 1000; sec.value++) {
hour.value
}
expect(src.value).toBe(11)
expect(c2.value).toBe('>= 5')
expect(c1Spy).toHaveBeenCalledTimes(3)
expect(c2Spy).toHaveBeenCalledTimes(2)

src.value++
expect(src.value).toBe(12)
expect(c2.value).toBe('>= 5')
expect(c1Spy).toHaveBeenCalledTimes(4)
expect(c2Spy).toHaveBeenCalledTimes(2)

for (let i = 0; i < 100; i++) {
src.value++
c2.value
}
expect(src.value).toBe(112)
expect(c2.value).toBe('>= 5')
expect(c1Spy).toHaveBeenCalledTimes(104)
expect(c2Spy).toHaveBeenCalledTimes(2)

expect(minSpy).toHaveBeenCalledTimes(1000)
expect(hourSpy).toHaveBeenCalledTimes(17)
})

it('chained computed value urgent assessment edge case', () => {
Expand Down

0 comments on commit f3a0071

Please sign in to comment.