Skip to content

Commit

Permalink
修改写法+下掉promise单测逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxiaokou committed Sep 27, 2023
1 parent 8e074da commit cb533a0
Show file tree
Hide file tree
Showing 38 changed files with 86 additions and 343 deletions.
3 changes: 1 addition & 2 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
},
"testEnvironment": "jsdom",
"testMatch": [
"**/packages/core/**/?(*.)+(spec|test).[jt]s?(x)",
"**/packages/webpack-plugin/**/?(*.)+(spec|test).[jt]s?(x)"
"**/packages/**/?(*.)+(spec|test).[jt]s?(x)"
],
"testPathIgnorePatterns": ["dist", "node_modules"]
}
51 changes: 0 additions & 51 deletions packages/api-proxy/__tests__/web/action-sheet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ import '@testing-library/jest-dom/extend-expect'
import {
showActionSheet
} from '../../src/platform/api/action-sheet/index.web'
import mpx from '../../../core/src/index'
import apiProxy from '../../src/index'
mpx.use(apiProxy, {
usePromise: true
})

// function manualPromise (promise, execResolve, execReject) {
// return new Promise((resolve, reject) => {
// console.log(promise(), '&&&')
// promise().then(res => resolve(res)).catch(err => reject(err))
// execResolve && execResolve()
// execReject && execReject()
// })
// }

describe('test toast', () => {
afterAll(() => {
Expand Down Expand Up @@ -91,41 +77,4 @@ describe('test toast', () => {
errMsg: 'showActionSheet:fail cancel'
})
})

test('should exec promise then', () => {
const execResolve = () => {
const actionSheet = document.body.lastChild
const list = actionSheet.lastChild.firstChild
list.childNodes[2].click()
}

mpx.showActionSheet({
itemList: ['A', 'B', 'C']
}).then(res => {
expect(res).toEqual({
errMsg: 'showActionSheet:ok',
tapIndex: 2
})
execResolve()
})
})

test('should exec promise catch', () => {
const execReject = () => {
const actionSheet = document.body.lastChild
const cancelBtn = actionSheet.lastChild.lastChild
cancelBtn.click()
}

// return manualPromise(() => {
mpx.showActionSheet({
itemList: ['A', 'B', 'C']
}).catch(err => {
expect(err).toEqual({
errMsg: 'showActionSheet:fail cancel'
})
execReject()
})
// }, null, execReject)
})
})
12 changes: 0 additions & 12 deletions packages/api-proxy/__tests__/web/install.spec.js

This file was deleted.

36 changes: 0 additions & 36 deletions packages/api-proxy/__tests__/web/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ import {
showModal
} from '../../src/platform/api/modal/index.web'

import mpx from '../../../core/src/index'
import apiProxy from '../../src/index'
mpx.use(apiProxy, {
usePromise: true
})

// function manualPromise (promise, execResolve, execReject) {
// return new Promise((resolve, reject) => {
// promise().then(res => resolve(res)).catch(err => reject(err))
// execResolve && execResolve()
// execReject && execReject()
// })
// }

describe('test modal', () => {
afterAll(() => {
document.body.lastChild.remove()
Expand Down Expand Up @@ -76,26 +62,4 @@ describe('test modal', () => {
confirm: true
})
})
test('should exec promise then', () => {
const execResolve = () => {
const modal = document.body.lastChild
const box = modal.lastChild
const btns = box.lastChild
const confirmBtn = btns.lastChild
confirmBtn.click()
}

// return manualPromise(() => {
mpx.showModal({
title: 'promise'
}).then(res => {
expect(res).toEqual({
errMsg: 'showModal:ok',
cancel: false,
confirm: true
})
execResolve()
})
// }, execResolve)
})
})
37 changes: 1 addition & 36 deletions packages/api-proxy/__tests__/web/storage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {
clearStorage,
clearStorageSync
} from '../../src/platform/api/storage/index.web'
import mpx from '../../../core/src/index'
import apiProxy from '../../src/index'
mpx.use(apiProxy, {
usePromise: true
})

const storageKey = 'storage key'
const storageValue = 'storage value'
const storageSyncKey = 'storage sync key'
Expand Down Expand Up @@ -50,15 +46,6 @@ describe('test storage', () => {
expect(complete.mock.calls.length).toBe(1)
})

test('setStorage promise then', () => {
mpx.setStorage({
key: storageSyncKey,
data: storageSyncValue
}).then(res => {
expect(res).toEqual({ errMsg: 'setStorage:ok' })
})
})

test('getStorage', () => {
const success = jest.fn()
const fail = jest.fn()
Expand All @@ -77,14 +64,6 @@ describe('test storage', () => {
expect(complete.mock.calls.length).toBe(1)
})

test('getStorage promise then', () => {
mpx.getStorage({
key: storageSyncKey
}).then(res => {
expect(res.data).toEqual(storageSyncValue)
})
})

test('getStorageInfo', () => {
const success = jest.fn()
const fail = jest.fn()
Expand All @@ -102,12 +81,6 @@ describe('test storage', () => {
expect(complete.mock.calls.length).toBe(1)
})

test('getStorageInfo promise then', () => {
mpx.getStorageInfo().then(res => {
expect(res.keys).toEqual([storageKey, storageSyncKey])
})
})

test('removeStorage', () => {
const success = jest.fn()
const fail = jest.fn()
Expand All @@ -126,14 +99,6 @@ describe('test storage', () => {
expect(getStorageInfoSync()).toEqual({ keys: [storageSyncKey], limitSize: null, currentSize: null })
})

test('removeStorage promise then', () => {
mpx.removeStorage({
key: storageKey
}).then(res => {
expect(res).toEqual({ errMsg: 'removeStorage:ok' })
})
})

test('clearStorage', () => {
const success = jest.fn()
const fail = jest.fn()
Expand Down
16 changes: 0 additions & 16 deletions packages/api-proxy/__tests__/web/toast.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import {
showToast, hideToast
} from '../../src/platform/api/toast/index.web'

import mpx from '../../../core/src/index'
import apiProxy from '../../src/index'
mpx.use(apiProxy, {
usePromise: true
})

describe('test toast', () => {
afterAll(() => {
document.body.lastChild.remove()
Expand Down Expand Up @@ -130,14 +124,4 @@ describe('test toast', () => {
expect(success.mock.calls[0][0].errMsg).toBe('hideToast:ok')
expect(complete.mock.calls.length).toBe(1)
})

test('should exec showToast promise then', () => {
return mpx.showToast({
title: 'show'
}).then(res => {
expect(res).toEqual({
errMsg: 'showToast:ok'
})
})
})
})
16 changes: 0 additions & 16 deletions packages/api-proxy/api-proxy-resolver.js

This file was deleted.

10 changes: 2 additions & 8 deletions packages/api-proxy/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
"testEnvironment": "jsdom",
"rootDir": "../",
"moduleNameMapper": {
"\\.(css|styl)$": "identity-obj-proxy",
"^@mpxjs/(.*)$": "<rootDir>/$1"
},
"globals": {
"__mpx_mode__": "web",
"__mpx_env__": "web"
},
"resolver": "./api-proxy/api-proxy-resolver.js"
"\\.(css|styl)$": "identity-obj-proxy"
}
}
1 change: 0 additions & 1 deletion packages/api-proxy/src/common/js/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './web'
export * from './utils'
export * from './promisify'
5 changes: 3 additions & 2 deletions packages/api-proxy/src/common/js/promisify.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const blackList = [
'reportMonitor',
'createOffscreenCanvas',
'reportEvent',
'connectSocket'
'connectSocket',
'base64ToArrayBuffer'
]

function getMapFromList (list) {
Expand Down Expand Up @@ -97,4 +98,4 @@ function promisify (listObj, whiteList, customBlackList) {
return result
}

export { promisify }
export default promisify
3 changes: 2 additions & 1 deletion packages/api-proxy/src/install.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as platformApi from './platform'
import { getEnvObj, promisify } from './common/js'
import { getEnvObj } from './common/js'
import promisify from './common/js/promisify'

export default function install (target, options = {}) {
const {
Expand Down
5 changes: 2 additions & 3 deletions packages/api-proxy/src/platform/api/action-sheet/index.ali.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { changeOpts, getEnvObj, noop } from '../../../common/js'
const ALI_OBJ = getEnvObj()
import { changeOpts, noop } from '../../../common/js'
function showActionSheet (options = {}) {
const opts = changeOpts(options, {
itemList: 'items'
Expand All @@ -21,7 +20,7 @@ function showActionSheet (options = {}) {
}
}

ALI_OBJ.showActionSheet(opts)
my.showActionSheet(opts)
}

export {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { changeOpts, getEnvObj } from '../../../common/js'

const ALI_OBJ = getEnvObj()
import { changeOpts } from '../../../common/js'

function addPhoneContact (options = {}) {
const opts = changeOpts(options, {
weChatNumber: 'alipayAccount'
})

ALI_OBJ.addPhoneContact(opts)
my.addPhoneContact(opts)
}

export {
Expand Down
7 changes: 1 addition & 6 deletions packages/api-proxy/src/platform/api/animation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { getEnvObj, envError } from '../../../common/js'

const ENV_OBJ = getEnvObj()

function createAnimation (options = {}) {
if (!ENV_OBJ.createAnimation) {
return envError('createAnimation')()
}
return ENV_OBJ.createAnimation(options)
}
const createAnimation = ENV_OBJ.createAnimation || envError('createAnimation')

export {
createAnimation
Expand Down
7 changes: 1 addition & 6 deletions packages/api-proxy/src/platform/api/audio/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { getEnvObj, envError } from '../../../common/js'

const ENV_OBJ = getEnvObj()

function createInnerAudioContext (options = {}) {
if (!ENV_OBJ.createInnerAudioContext) {
return envError('createInnerAudioContext')()
}
return ENV_OBJ.createInnerAudioContext(options)
}
const createInnerAudioContext = ENV_OBJ.createInnerAudioContext || envError('createInnerAudioContext')

export {
createInnerAudioContext
Expand Down
14 changes: 2 additions & 12 deletions packages/api-proxy/src/platform/api/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@ import { getEnvObj, envError } from '../../../common/js'

const ENV_OBJ = getEnvObj()

function base64ToArrayBuffer (base64) {
if (!ENV_OBJ.base64ToArrayBuffer) {
return envError('base64ToArrayBuffer')()
}
return ENV_OBJ.base64ToArrayBuffer(base64)
}
const base64ToArrayBuffer = ENV_OBJ.base64ToArrayBuffer || envError('base64ToArrayBuffer')

function arrayBufferToBase64 (arrayBuffer) {
if (!ENV_OBJ.arrayBufferToBase64) {
return envError('arrayBufferToBase64')()
}
return ENV_OBJ.arrayBufferToBase64(arrayBuffer)
}
const arrayBufferToBase64 = ENV_OBJ.arrayBufferToBase64 || envError('arrayBufferToBase64')

export {
base64ToArrayBuffer,
Expand Down
10 changes: 3 additions & 7 deletions packages/api-proxy/src/platform/api/ble-connection/index.ali.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { getEnvObj } from '../../../common/js'

const ALI_OBJ = getEnvObj()

function closeBLEConnection (options = {}) {
ALI_OBJ.disconnectBLEDevice(options)
my.disconnectBLEDevice(options)
}

function createBLEConnection (options = {}) {
ALI_OBJ.connectBLEDevice(options)
my.connectBLEDevice(options)
}

function onBLEConnectionStateChange (callback) {
ALI_OBJ.onBLEConnectionStateChanged(callback)
my.onBLEConnectionStateChanged(callback)
}

export {
Expand Down
Loading

0 comments on commit cb533a0

Please sign in to comment.