Skip to content

Commit

Permalink
feat: 新增 mp 系列工具
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 24, 2020
1 parent 6e818d1 commit 2cf912a
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 0 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"require": "./_cjs/date/index.js",
"import": "./date/index.js"
},
"./mp": {
"require": "./_cjs/mp/index.js",
"import": "./mp/index.js"
},
"./package.json": "./package.json",
"./react": {
"require": "./_cjs/react/index.js",
Expand Down
15 changes: 15 additions & 0 deletions src/mp/ensureInMiniProgram.ts
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('不在小程序环境中')
}
14 changes: 14 additions & 0 deletions src/mp/index.ts
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
18 changes: 18 additions & 0 deletions src/mp/miniProgramConfig.ts
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
}
73 changes: 73 additions & 0 deletions src/mp/navigatePageTo.test.ts
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')}`,
}),
)
})
})
29 changes: 29 additions & 0 deletions src/mp/navigatePageTo.ts
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,
})
},
})
})
})
}
5 changes: 5 additions & 0 deletions src/mp/redirectPageTo.ts
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)
}

0 comments on commit 2cf912a

Please sign in to comment.