-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |