-
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
7 changed files
with
158 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,15 @@ | ||
import { inMiniProgram } from '../utils' | ||
|
||
let $mp: ReturnType<typeof inMiniProgram> | undefined | ||
|
||
export function ensureInMiniProgram<T>( | ||
cb: (mp: Exclude<typeof $mp, false | undefined>) => Promise<T>, | ||
): Promise<T> { | ||
if ($mp === undefined) { | ||
$mp = inMiniProgram() | ||
} | ||
if ($mp) { | ||
return cb($mp) | ||
} | ||
return Promise.reject('不在小程序环境中') | ||
} |
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,14 @@ | ||
/** | ||
* 小程序工具库。 | ||
* | ||
* @packageDocumentation | ||
*/ | ||
|
||
/* istanbul ignore file */ | ||
|
||
// @index(['./**/*.ts', '!./**/*.test.*'], f => `export * from '${f.path}'`) | ||
export * from './ensureInMiniProgram' | ||
export * from './miniProgramConfig' | ||
export * from './navigatePageTo' | ||
export * from './redirectPageTo' | ||
// @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,18 @@ | ||
import { assign } from '../utils' | ||
|
||
export interface MiniProgramConfig { | ||
webUrlToMiniProgramUrl?: (url: string) => string | ||
} | ||
|
||
const miniProgramConfig: MiniProgramConfig = {} | ||
|
||
export function getMiniProgramConfig(): Readonly<MiniProgramConfig> { | ||
return miniProgramConfig | ||
} | ||
|
||
export function setMiniProgramConfig( | ||
config: Partial<MiniProgramConfig>, | ||
): Readonly<MiniProgramConfig> { | ||
assign(miniProgramConfig, config) | ||
return miniProgramConfig | ||
} |
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,73 @@ | ||
describe('navigatePageTo', () => { | ||
const navigateTo = jest | ||
.fn() | ||
.mockImplementation((options: WechatMiniprogram.NavigateToOption) => { | ||
if (options.url.includes('fail')) { | ||
options.fail?.({} as any) | ||
} else { | ||
options.success?.({} as any) | ||
} | ||
}) | ||
const redirectTo = jest | ||
.fn() | ||
.mockImplementation((options: WechatMiniprogram.RedirectToOption) => { | ||
options.success?.({} as any) | ||
}) | ||
const switchTab = jest | ||
.fn() | ||
.mockImplementation((options: WechatMiniprogram.SwitchTabOption) => { | ||
options.success?.({} as any) | ||
}) | ||
|
||
beforeAll(() => { | ||
jest.mock('../utils/inMiniProgram', () => ({ | ||
inMiniProgram: (): Partial<WechatMiniprogram.Wx> => ({ | ||
navigateTo: navigateTo, | ||
redirectTo: redirectTo, | ||
switchTab: switchTab, | ||
}), | ||
})) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
test('表现正常', async () => { | ||
const { navigatePageTo } = await import('./navigatePageTo') | ||
await navigatePageTo('/') | ||
expect(navigateTo).toBeCalled().toBeCalledTimes(1) | ||
}) | ||
|
||
test('redirect 正常', async () => { | ||
const { navigatePageTo } = await import('./navigatePageTo') | ||
await navigatePageTo('/', true) | ||
expect(navigateTo).not.toBeCalled() | ||
expect(redirectTo).toBeCalled().toBeCalledTimes(1) | ||
}) | ||
|
||
test('switchTab 兜底', async () => { | ||
const { navigatePageTo } = await import('./navigatePageTo') | ||
await navigatePageTo('/fail') | ||
expect(navigateTo).toBeCalled().toBeCalledTimes(1) | ||
expect(redirectTo).not.toBeCalled() | ||
expect(switchTab).toBeCalled().toBeCalledTimes(1) | ||
}) | ||
|
||
test('支持 WEB URL', async () => { | ||
const { navigatePageTo } = await import('./navigatePageTo') | ||
const { setMiniProgramConfig } = await import('./miniProgramConfig') | ||
setMiniProgramConfig({ | ||
webUrlToMiniProgramUrl: url => `/webview?url=${encodeURIComponent(url)}`, | ||
}) | ||
await navigatePageTo('http://foo.bar') | ||
expect(navigateTo) | ||
.toBeCalled() | ||
.toBeCalledTimes(1) | ||
.toBeCalledWith( | ||
expect.objectContaining({ | ||
url: `/webview?url=${encodeURIComponent('http://foo.bar')}`, | ||
}), | ||
) | ||
}) | ||
}) |
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,29 @@ | ||
import { ensureInMiniProgram } from './ensureInMiniProgram' | ||
import { getMiniProgramConfig } from './miniProgramConfig' | ||
import { isUrl } from '../utils' | ||
|
||
export function navigatePageTo(url: string, redirect = false): Promise<any> { | ||
return ensureInMiniProgram(mp => { | ||
return new Promise((resolve, reject) => { | ||
if (isUrl(url)) { | ||
const { webUrlToMiniProgramUrl } = getMiniProgramConfig() | ||
if (typeof webUrlToMiniProgramUrl === 'function') { | ||
url = webUrlToMiniProgramUrl(url) | ||
} | ||
} | ||
;(redirect | ||
? ((mp.redirectTo as any) as typeof mp.navigateTo) | ||
: mp.navigateTo)({ | ||
url: url, | ||
success: resolve, | ||
fail: () => { | ||
mp.switchTab({ | ||
url: url, | ||
success: resolve, | ||
fail: reject, | ||
}) | ||
}, | ||
}) | ||
}) | ||
}) | ||
} |
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,5 @@ | ||
import { navigatePageTo } from './navigatePageTo' | ||
|
||
export function redirectPageTo(url: string): Promise<any> { | ||
return navigatePageTo(url, true) | ||
} |