-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
12 changed files
with
231 additions
and
102 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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') | ||
} | ||
}; | ||
} |
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
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,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() | ||
}) | ||
}) |
Oops, something went wrong.