Skip to content

Commit 187ebe7

Browse files
committed
fix: resetConditions only assign value if property exists
1 parent 0600016 commit 187ebe7

File tree

8 files changed

+75
-23
lines changed

8 files changed

+75
-23
lines changed

_internal/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"exports": "./dist/index.mjs",
88
"private": true,
99
"scripts": {
10+
"watch": "bunchee index.ts --no-sourcemap -w",
1011
"build": "bunchee index.ts --no-sourcemap",
1112
"types:check": "tsc --noEmit",
1213
"clean": "rimraf dist"

_internal/utils/common.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ export function sortObject(unordered) {
173173
}, {})
174174
}
175175

176+
export function pick(obj: Record<string, any>, keys: string[]) {
177+
const res = {}
178+
keys.forEach((key) => {
179+
if (key in obj) {
180+
res[key] = obj[key]
181+
}
182+
})
183+
return res
184+
}
185+
176186
export function serializeFunc(fn) {
177187
//the source code from https://github.com/yahoo/serialize-javascript/blob/main/index.js
178188
const serializedFn = fn.toString()

core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"exports": "./dist/index.mjs",
88
"private": true,
99
"scripts": {
10+
"watch": "bunchee index.ts --no-sourcemap -w",
1011
"build": "bunchee index.ts --no-sourcemap",
1112
"types:check": "tsc --noEmit",
1213
"clean": "rimraf dist"

core/use-condition-watcher.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
syncQuery2Conditions,
2121
isEquivalent,
2222
deepClone,
23+
pick,
2324
} from 'vue-condition-watcher/_internal'
2425
import { containsProp, isNoData as isDataEmpty, isObject, isServer, rAF } from 'vue-condition-watcher/_internal'
2526

@@ -103,7 +104,8 @@ export default function useConditionWatcher<Cond extends Record<string, any>, Re
103104
} = createEvents()
104105

105106
const resetConditions = (cond?: Record<string, any>): void => {
106-
Object.assign(_conditions, isObject(cond) && !cond.type ? cond : backupIntiConditions)
107+
const conditionKeys = Object.keys(_conditions)
108+
Object.assign(_conditions, isObject(cond) ? pick(cond, conditionKeys) : backupIntiConditions)
107109
}
108110

109111
const isLoading = computed(() => !error.value && !data.value)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"scripts": {
3232
"clean": "turbo run clean",
3333
"build": "turbo run build",
34+
"watch": "turbo run watch --parallel",
3435
"types:check": "turbo run types:check",
3536
"format": "prettier --write ./**/*.ts",
3637
"lint": "eslint . --ext .ts --cache",
@@ -39,7 +40,6 @@
3940
"test:2": "vue-demi-switch 2 vue2 && vitest run",
4041
"test:3": "vue-demi-switch 3 && vitest run",
4142
"test:all": "pnpm format && pnpm build && pnpm test:3 && pnpm test:2 && vue-demi-switch 3",
42-
"watch": "tsc --watch",
4343
"prepublishOnly": "pnpm clean && pnpm build",
4444
"publish-beta": "pnpm publish --tag beta"
4545
},

test/use-condition-watcher.test.ts

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ import useConditionWatcher from 'vue-condition-watcher'
22
import { isRef, isReactive, isReadonly, createApp, nextTick, defineComponent } from 'vue-demi'
33
import type { VueWrapper } from '@vue/test-utils'
44
import { mount } from '@vue/test-utils'
5-
import { describe, expect, test, vi } from 'vitest'
5+
import { describe, expect, test, vi, beforeEach } from 'vitest'
66

77
describe('Basic test of vue-condition-watcher', () => {
8-
it(`Check return value type`, () => {
9-
const config = {
8+
let basicTestConfig = {}
9+
beforeEach(() => {
10+
basicTestConfig = {
1011
fetcher: (params) => new Promise((resolve) => resolve(params)),
1112
conditions: {
1213
gender: ['male'],
1314
results: 9,
1415
},
1516
}
16-
const { conditions, data, error, isLoading, execute } = useConditionWatcher(config)
17+
})
18+
19+
it(`Check return value type`, () => {
20+
const { conditions, data, error, isLoading, execute } = useConditionWatcher(basicTestConfig)
1721

1822
expect(isReactive(conditions)).toBeTruthy()
1923
expect(isRef(data)).toBeTruthy()
@@ -24,30 +28,15 @@ describe('Basic test of vue-condition-watcher', () => {
2428
})
2529

2630
it(`Check data, error, isLoading is readonly`, () => {
27-
const config = {
28-
fetcher: (params) => new Promise((resolve) => resolve(params)),
29-
conditions: {
30-
gender: ['male'],
31-
results: 9,
32-
},
33-
}
34-
35-
const { data, error, isLoading } = useConditionWatcher(config)
31+
const { data, error, isLoading } = useConditionWatcher(basicTestConfig)
3632

3733
expect(isReadonly(data)).toBeTruthy()
3834
expect(isReadonly(error)).toBeTruthy()
3935
expect(isReadonly(isLoading)).toBeTruthy()
4036
})
4137

4238
it(`Condition should be change`, () => {
43-
const config = {
44-
fetcher: (params) => new Promise((resolve) => resolve(params)),
45-
conditions: {
46-
gender: ['male'],
47-
results: 9,
48-
},
49-
}
50-
const { conditions } = useConditionWatcher(config)
39+
const { conditions } = useConditionWatcher(basicTestConfig)
5140

5241
expect(conditions).toMatchObject({
5342
gender: ['male'],
@@ -61,6 +50,39 @@ describe('Basic test of vue-condition-watcher', () => {
6150
results: 10,
6251
})
6352
})
53+
54+
it('Reset conditions to initial value', () => {
55+
const { conditions, resetConditions } = useConditionWatcher(basicTestConfig)
56+
57+
conditions.results = 10
58+
conditions.gender = ['male', 'female']
59+
60+
resetConditions()
61+
62+
expect(conditions).toMatchObject({
63+
gender: ['male'],
64+
results: 9,
65+
})
66+
})
67+
68+
it('Reset conditions to custom value and only assign if property exists.', () => {
69+
const { conditions, resetConditions } = useConditionWatcher(basicTestConfig)
70+
71+
conditions.results = 10
72+
conditions.gender = ['male', 'female']
73+
74+
resetConditions({
75+
gender: ['female'],
76+
results: 19,
77+
type: '2',
78+
useless: '12312321',
79+
})
80+
81+
expect(conditions).toMatchObject({
82+
gender: ['female'],
83+
results: 19,
84+
})
85+
})
6486
})
6587

6688
// const tick = async (times: number) => {

test/utils.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
isEquivalent,
77
deepClone,
88
typeOf,
9+
pick,
910
} from 'vue-condition-watcher/_internal'
1011
import { describe, expect, test } from 'vitest'
1112

@@ -197,3 +198,15 @@ describe('utils: syncQuery2Conditions', () => {
197198
})
198199
})
199200
})
201+
202+
describe('utils: pick', () => {
203+
it('Assign only if property exists in target object', () => {
204+
const targetObj = { name: 'Runkids', age: 10 }
205+
const resObj = pick({ age: 20, name: 'runkids', nickname: 'egg' }, Object.keys(targetObj))
206+
207+
expect(Object.assign(targetObj, resObj)).toMatchObject({
208+
name: 'runkids',
209+
age: 20,
210+
})
211+
})
212+
})

turbo.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
"dependsOn": ["^build"]
77
},
88
"types:check": {},
9+
"watch": {
10+
"cache": false
11+
},
912
"clean": {
1013
"cache": false
1114
}

0 commit comments

Comments
 (0)