|
| 1 | +import { renderHook, act } from '@testing-library/react-hooks' |
| 2 | +import * as vueReactivity from '@vue/reactivity' |
| 3 | +import * as veact from '../src' |
| 4 | +import { |
| 5 | + onMounted, |
| 6 | + onBeforeUnmount, |
| 7 | + onUpdated, |
| 8 | + useReactive, |
| 9 | + useRef, |
| 10 | + useShallowRef, |
| 11 | + useShallowReactive, |
| 12 | + useComputed, |
| 13 | + watch, |
| 14 | + useWatch, |
| 15 | + watchEffect, |
| 16 | + useWatchEffect, |
| 17 | + useReactivity, |
| 18 | + batchedUpdates, |
| 19 | +} from '../src' |
| 20 | + |
| 21 | +test('<exports> should be export all @vue/reactivity members', () => { |
| 22 | + expect( |
| 23 | + Object.keys(vueReactivity).every((key) => { |
| 24 | + const targetMember = (veact as any)[key] |
| 25 | + return Boolean(targetMember) && (vueReactivity as any)[key] === targetMember |
| 26 | + }) |
| 27 | + ).toBeTruthy() |
| 28 | +}) |
| 29 | + |
| 30 | +test('<type> should be function type', () => { |
| 31 | + const hooksTargetType = 'function' |
| 32 | + expect(typeof onMounted).toBe(hooksTargetType) |
| 33 | + expect(typeof onUpdated).toBe(hooksTargetType) |
| 34 | + expect(typeof onBeforeUnmount).toBe(hooksTargetType) |
| 35 | + expect(typeof useReactive).toBe(hooksTargetType) |
| 36 | + expect(typeof useRef).toBe(hooksTargetType) |
| 37 | + expect(typeof useShallowRef).toBe(hooksTargetType) |
| 38 | + expect(typeof useShallowReactive).toBe(hooksTargetType) |
| 39 | + expect(typeof useComputed).toBe(hooksTargetType) |
| 40 | + expect(typeof watch).toBe(hooksTargetType) |
| 41 | + expect(typeof useWatch).toBe(hooksTargetType) |
| 42 | + expect(typeof watchEffect).toBe(hooksTargetType) |
| 43 | + expect(typeof useWatchEffect).toBe(hooksTargetType) |
| 44 | + expect(typeof useReactivity).toBe(hooksTargetType) |
| 45 | + expect(typeof batchedUpdates).toBe(hooksTargetType) |
| 46 | +}) |
| 47 | + |
| 48 | +test('<lifecycle>', () => { |
| 49 | + const results: string[] = [] |
| 50 | + const hookRenderer = renderHook(() => { |
| 51 | + onMounted(() => { |
| 52 | + results.push('onMounted') |
| 53 | + }) |
| 54 | + onUpdated(() => { |
| 55 | + results.push('onUpdated') |
| 56 | + }) |
| 57 | + onBeforeUnmount(() => { |
| 58 | + results.push('onBeforeUnmount') |
| 59 | + }) |
| 60 | + return useRef(0) |
| 61 | + }) |
| 62 | + |
| 63 | + act(() => { |
| 64 | + hookRenderer.result.current.value++ |
| 65 | + }) |
| 66 | + |
| 67 | + hookRenderer.unmount() |
| 68 | + expect(results.length).toBe(3) |
| 69 | + expect(results[0]).toBe('onMounted') |
| 70 | + expect(results[1]).toBe('onUpdated') |
| 71 | + expect(results[2]).toBe('onBeforeUnmount') |
| 72 | +}) |
0 commit comments