Skip to content

Commit

Permalink
fix(h5):processOpenApi中将targetApi的绑定延迟到Promise中,防止在测试时被import时运行,影响其他…
Browse files Browse the repository at this point in the history
…API测试
  • Loading branch information
CaiDingxian committed Apr 15, 2022
1 parent f01ae87 commit 369d7a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
16 changes: 9 additions & 7 deletions packages/taro-h5/__tests__/location/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ describe('location', () => {
longitude: 5,
speed: 6,
verticalAccuracy: 0,
errMsg: 'getLocation: ok'
errMsg: 'getLocation:ok'
}

test('should get location info object from wx', () => {
// @ts-ignore
window.wx = {
getLocation (options) {
options.complete(mockLocation)
options.success(mockLocation)
Object.defineProperty(window, 'wx', {
value: {
getLocation: (options) => {
options.complete(mockLocation)
options.success(mockLocation)
}
}
}
})
return Taro.getLocation({
type: 'WGS84'
})
Expand All @@ -39,7 +41,7 @@ describe('location', () => {
})
.finally(() => {
// @ts-ignore
window.wx = null
delete window.wx
})
})

Expand Down
11 changes: 5 additions & 6 deletions packages/taro-h5/src/api/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,12 @@ export function processOpenApi ({
formatOptions = options => options,
formatResult = res => res
}: IProcessOpenApi) {
// @ts-ignore
const targetApi = window?.wx?.[name] || standardMethod
if (typeof targetApi !== 'function') {
return weixinCorpSupport(name)
}

return (options = {}) => {
// @ts-ignore
const targetApi = window?.wx?.[name] || standardMethod
if (typeof targetApi !== 'function') {
return weixinCorpSupport(name)
}
const obj = Object.assign({}, defaultOptions, options)
return new Promise((resolve, reject) => {
['fail', 'success', 'complete'].forEach(k => {
Expand Down

0 comments on commit 369d7a8

Please sign in to comment.