Skip to content

Commit

Permalink
feat(router): 尝试支持reLaunch
Browse files Browse the repository at this point in the history
  • Loading branch information
Littly committed Mar 20, 2019
1 parent f98c146 commit 67a5e85
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 102 deletions.
2 changes: 1 addition & 1 deletion packages/taro-h5/src/api/unsupportedApi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const openDocument = temporarilyNotSupport('openDocument')
// export const navigateTo = temporarilyNotSupport('navigateTo')
// export const redirectTo = temporarilyNotSupport('redirectTo')
// export const switchTab = temporarilyNotSupport('switchTab')
export const reLaunch = temporarilyNotSupport('reLaunch')
// export const reLaunch = temporarilyNotSupport('reLaunch')

// 位置
export const getLocation = temporarilyNotSupport('getLocation')
Expand Down
9 changes: 9 additions & 0 deletions packages/taro-h5/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ declare namespace TaroH5 {
refs: {
[key: string]: any
};
$router: {
path: string;
scene: number;
query: {
[key: string]: string;
};
shareTicket: string;
referrerInfo: {};
};
}

class PureComponent<P = {}, S = {}> extends Component<P, S> { }
Expand Down
58 changes: 0 additions & 58 deletions packages/taro-router/__tests__/router.spec.tsx

This file was deleted.

8 changes: 1 addition & 7 deletions packages/taro-router/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
const path = require('path')

module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
testURL: 'https://taro.aotu.io',
globals: {
window: true
},
moduleNameMapper: {
'nervjs': path.join(__dirname, 'node_modules', 'nervjs')
// 'nervjs': require.resolve('nervjs')
}
};
}
7 changes: 4 additions & 3 deletions packages/taro-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
"index.js"
],
"scripts": {
"test": "jest",
"build": "rollup -c rollup.config.js",
"build": "npm run test && rollup -c rollup.config.js",
"dev": "rollup -c rollup.config.js --watch",
"prepack": "npm run build"
"prepack": "npm run build",
"test": "jest",
"test:dev": "jest --watch"
},
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { History } from '../src/utils/types'
import createHistory from '../src/history/createHashHistory'
import { createNavigateTo, createNavigateBack, createRedirectTo } from '../src/apis'
import { History } from '../utils/types'
import createHistory from '../history/createHistory'
import { createNavigateTo, createNavigateBack, createRedirectTo } from '../apis'

let history: History

beforeEach(() => {
history = createHistory()
history = createHistory({
mode: 'browser',
basename: '/',
firstPagePath: '/pages/index/index',
customRoutes: {
'/index': '/pages/index/index',
'/about': '/pages/about/abtou'
}
})
})

xdescribe('navigateTo/navigateBack/redirectTo', () => {
describe('navigateTo/navigateBack/redirectTo', () => {
const location1 = {
pathname: '/',
path: '/pages/index/index',
state: { key: '0' },
search: '',
hash: '',
Expand All @@ -19,7 +27,7 @@ xdescribe('navigateTo/navigateBack/redirectTo', () => {

const url2 = '/pages/about/index?para=1'
const location2 = {
pathname: '/pages/about/index',
path: '/pages/about/index',
state: { key: '1' },
search: '?para=1',
hash: '',
Expand All @@ -30,7 +38,7 @@ xdescribe('navigateTo/navigateBack/redirectTo', () => {

const url3 = '/pages/settings/index?para2=2'
const location3 = {
pathname: '/pages/settings/index',
path: '/pages/settings/index',
state: { key: '1' },
search: '?para2=2',
hash: '',
Expand Down Expand Up @@ -65,7 +73,7 @@ xdescribe('navigateTo/navigateBack/redirectTo', () => {
// jsdom无法准确模拟history的全部功能,这里使用spy代替
const spy = spyOn(window.history, 'go')
navigateBack({ delta: 1 })
expect(spy).toHaveBeenCalledWith(1)
expect(spy).toHaveBeenCalledWith(-1)
})

it('should notify listeners with proper params when calling redirectTo', () => {
Expand Down
140 changes: 140 additions & 0 deletions packages/taro-router/src/__tests__/router-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import Taro from '@tarojs/taro-h5'
import Nerv from 'nervjs'

import createHistory from '../history/createHistory'
import Router from '../router/router'

let mockHistory

window.scrollTo = jest.fn()
const wait = (timeout) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, timeout)
})
}

beforeEach(() => {
mockHistory = createHistory({
mode: 'browser',
basename: '/',
firstPagePath: '/pages/index/index',
customRoutes: {
'/pages/index/index': '/index',
'/pages/about/about': '/about'
}
})
})

describe('router component', () => {

it('should work!', async done => {
const url1 = '/pages/index/index'
const url2 = '/pages/about/index'

class Base extends Taro.Component {
idx
render () {
return (
<div>{this.idx}</div>
)
}
}

const getPage = idx => {
return class Page extends Base {
idx = idx
}
}

class RouterComponent extends Taro.Component {
render () {
return (
<Router
history={mockHistory}
routes={[{
path: url1,
componentLoader: () => Promise.resolve({
default: getPage(0)
}),
isIndex: true
}, {
path: url2,
componentLoader: () => Promise.resolve({
default: getPage(1)
}),
isIndex: false
}]}
customRoutes={{
"/pages/index/index": "/index",
"/pages/about/index": "/about"
}}
/>
)
}
}

const routerComponent = <RouterComponent />
const getComputedStyle = window.getComputedStyle
Nerv.render(routerComponent, document.createElement('div'))
const dom = routerComponent.dom

await wait(100)
expect(window.getComputedStyle(dom.childNodes[0]).display).toEqual('block')
Taro.navigateTo({
url: '/pages/about/about'
})
await wait(100)
expect(getComputedStyle(dom.childNodes[0]).display).toEqual('none')
expect(getComputedStyle(dom.childNodes[1]).display).toEqual('block')
done()
})

it('should be able to get $router property via this', async done => {
const url1 = '/pages/index/index'
const url2 = '/pages/about/index'
let routerParams
class Page extends Taro.Component {
render() {
routerParams = this.$router
return (
<div />
)
}
}
const componentLoader = () => Promise.resolve({
default: Page
})

class RouterComponent extends Taro.Component {
render () {
return (
<Router
history={mockHistory}
routes={[{
path: url1,
componentLoader,
isIndex: true
}, {
path: url2,
componentLoader,
isIndex: false
}]}
customRoutes={{
"/pages/index/index": "/index",
"/pages/about/index": "/about"
}}
/>
)
}
}
Nerv.render(<RouterComponent />, document.createElement('div'))
await wait(100)
expect(routerParams).toMatchObject({})
Taro.navigateTo({
url: '/pages/about/about'
})
await wait(100)
expect(routerParams).toMatchObject({})
done()
})
})
Loading

0 comments on commit 67a5e85

Please sign in to comment.