Skip to content

Commit

Permalink
feat(taro): 新增 submit
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jun 30, 2020
1 parent 4118439 commit 4f1d662
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"require": "./_cjs/react/index.js",
"import": "./react/index.js"
},
"./taro": {
"require": "./_cjs/taro/index.js",
"import": "./taro/index.js"
},
"./types": {
"require": "./_cjs/types/index.js",
"import": "./types/index.js"
Expand Down
9 changes: 9 additions & 0 deletions src/taro/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Taro 3 工具库。
*
* @packageDocumentation
*/

// @index(['./**/*.ts', '!./**/*.test.*'], f => `export * from '${f.path}'`)
export * from './submit'
// @endindex
42 changes: 42 additions & 0 deletions src/taro/submit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as Taro from '@tarojs/taro'
import { wait } from '../utils'

describe('submit', () => {
const showLoading: any = jest.fn()
const hideLoading: any = jest.fn()
const showToast: any = jest.fn()

beforeAll(() => {
jest.mock(
'@tarojs/taro',
() =>
({
showLoading,
hideLoading,
showToast,
} as typeof Taro),
)
})

test('表现正常', async () => {
const { submit } = await import('./submit')
const res = await submit(async _ => {
await _.start('加载中...')
await wait(500)
await _.fail('加载失败')
await _.success('加载成功')
return 1
})
expect(showLoading)
.toBeCalled()
.toBeCalledTimes(1)
.toBeCalledWith(
expect.objectContaining({
title: '加载中...',
}),
)
expect(showToast).toBeCalled().toBeCalledTimes(2)
expect(hideLoading).toBeCalled().toBeCalledTimes(3)
expect(res).toBe(1)
})
})
77 changes: 77 additions & 0 deletions src/taro/submit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { hideLoading, showLoading, showToast } from '@tarojs/taro'
import { wait } from '../utils'

export interface SubmitActionPayload {
/**
* 开始提示。
*
* @param message 提示信息
*/
start(message: string): Promise<any>

/**
* 失败提示。
*
* @param message 提示信息
* @param duration 持续时间(毫秒),默认 1500
*/
fail(message: string, duration?: number): Promise<any>

/**
* 成功提示。
*
* @param message 提示信息
* @param duration 持续时间(毫秒),默认 1500
*/
success(message: string, duration?: number): Promise<any>
}

/**
* 对提交类行为的封装。
*
* @param action 行为
* @returns 返回行为结果
* @example
* ```typescript
* await submit(async _ => {
* await _.start('保存中...')
* await save(data)
* await _.success('保存成功')
* })
* ```
*/
export function submit<TResult>(
action: (payload: SubmitActionPayload) => Promise<TResult>,
): Promise<TResult> {
const payload: SubmitActionPayload = {
start(message) {
showLoading({
title: message,
mask: true,
})
return Promise.resolve()
},
fail(message, duration = 1500) {
hideLoading()
showToast({
title: message,
icon: 'none',
duration: duration,
})
return wait(duration)
},
success(message, duration = 1500) {
hideLoading()
showToast({
title: message,
icon: 'success',
duration: duration,
})
return wait(duration)
},
}
return action(payload).then(res => {
hideLoading()
return res
})
}

0 comments on commit 4f1d662

Please sign in to comment.