Skip to content

Commit dd87092

Browse files
committed
feat: test case
1 parent e3dc8e5 commit dd87092

File tree

13 files changed

+2310
-204
lines changed

13 files changed

+2310
-204
lines changed

.github/workflows/publish.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
cache: 'yarn'
2323
registry-url: https://registry.npmjs.org/
2424
- run: yarn install
25-
# - run: yarn test
25+
- run: yarn test
2626
- run: yarn rebirth
2727
- run: npm publish --access=public
2828
env:

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ yarn build
243243

244244
## TODO
245245

246-
- [ ] test case
247246
- [ ] support `useEffect` deps with reactivity prop value
248247

249248
### Changelog

__tests__/watch.spec.ts

-78
This file was deleted.

jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'jsdom',
5+
}

package.json

+8-9
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,11 @@
2626
"scripts": {
2727
"lint": "eslint --ext .ts src/**",
2828
"format": "prettier --write --parser typescript \"src/**/*.ts\"",
29-
"test": "TODO",
29+
"test": "jest",
3030
"build": "libundler",
31-
"rebirth": "npm run lint && npm run build",
31+
"rebirth": "npm run lint && npm run test && npm run build",
3232
"release": ". ./scripts/release.sh"
3333
},
34-
"standard-version": {
35-
"skip": {
36-
"changelog": true,
37-
"commit": true
38-
}
39-
},
4034
"peerDependencies": {
4135
"react": "^16.8",
4236
"react-dom": "^16.8"
@@ -45,17 +39,22 @@
4539
"@vue/reactivity": "^3.2.0-beta.5"
4640
},
4741
"devDependencies": {
48-
"@surmon-china/libundler": "^1.0.1",
42+
"@surmon-china/libundler": "^1.0.2",
43+
"@testing-library/react": "^12.0.0",
44+
"@testing-library/react-hooks": "^7.0.1",
45+
"@types/jest": "^26.0.24",
4946
"@types/react": "^17.0.15",
5047
"@types/react-dom": "^17.0.9",
5148
"@typescript-eslint/eslint-plugin": "^4.28.4",
5249
"@typescript-eslint/parser": "^4.28.4",
5350
"eslint": "^7.31.0",
5451
"eslint-config-prettier": "^8.3.0",
5552
"eslint-plugin-prettier": "^3.4.0",
53+
"jest": "^27.0.6",
5654
"prettier": "^2.3.2",
5755
"react": "^17.0.2",
5856
"react-dom": "^17.0.2",
57+
"ts-jest": "^27.0.4",
5958
"typescript": "^4.3.5"
6059
}
6160
}

scripts/release.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ set -e
55
PKG_VERSION=$(jq -r '.version' package.json)
66

77
git fetch origin v"$PKG_VERSION" || {
8-
npx standard-version -a --release-as "$PKG_VERSION"
8+
npx standard-version --skip.changelog --skip.commit -a --release-as "$PKG_VERSION"
99
git push --follow-tags origin main
1010
}

src/lifecycle.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@
33
* @author Surmon <https://github.com/surmon-china>
44
*/
55

6-
import { useEffect } from 'react'
6+
import { useRef, useEffect } from 'react'
77

88
export function onMounted(callback: () => any) {
99
useEffect(() => {
1010
callback()
1111
}, [])
1212
}
1313

14-
export function onBeforeUnmount(callback: () => void) {
14+
export function onUpdated(callback: () => void) {
15+
const isMounted = useRef(false)
1516
useEffect(() => {
16-
return () => {
17+
if (isMounted.current) {
1718
callback()
19+
} else {
20+
isMounted.current = true
1821
}
19-
}, [])
22+
})
2023
}
2124

22-
export function onUpdated(callback: () => void) {
25+
export function onBeforeUnmount(callback: () => void) {
2326
useEffect(() => {
24-
callback()
25-
})
27+
return () => {
28+
callback()
29+
}
30+
}, [])
2631
}

src/reactivity.ts

+1-48
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,5 @@
66

77
import type { Ref, UnwrapRef } from '@vue/reactivity'
88

9+
export * from '@vue/reactivity'
910
export type ToRef<T> = [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
10-
11-
export {
12-
computed,
13-
customRef,
14-
effect,
15-
enableTracking,
16-
isProxy,
17-
isReactive,
18-
isReadonly,
19-
isRef,
20-
ITERATE_KEY,
21-
markRaw,
22-
pauseTracking,
23-
reactive,
24-
readonly,
25-
ref,
26-
resetTracking,
27-
shallowReactive,
28-
shallowReadonly,
29-
shallowRef,
30-
stop,
31-
toRaw,
32-
toRef,
33-
toRefs,
34-
track,
35-
trigger,
36-
triggerRef,
37-
unref,
38-
ReactiveEffect,
39-
} from '@vue/reactivity'
40-
41-
export type {
42-
EffectScheduler,
43-
DebuggerOptions,
44-
ComputedGetter,
45-
ComputedRef,
46-
ComputedSetter,
47-
DebuggerEvent,
48-
DeepReadonly,
49-
ReactiveEffectOptions,
50-
Ref,
51-
RefUnwrapBailTypes,
52-
UnwrapNestedRefs,
53-
ToRefs,
54-
UnwrapRef,
55-
WritableComputedOptions,
56-
WritableComputedRef,
57-
} from '@vue/reactivity'

src/watch/watchEffect.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { WATCH_CLEANUP_ERROR, WATCH_CALLBACK_ERROR } from './patch'
1313

1414
export type WatchEffectOptions = DebuggerOptions
1515
export type WatchStopHandle = () => void
16-
export type InvalidateCallbackRegistrator = (cb: () => void) => void
16+
export type InvalidateCallbackRegistrator = (callback: () => void) => void
1717
export type WatchEffect = (onInvalidate: InvalidateCallbackRegistrator) => any
1818

1919
/**

tests/base.spec.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)