Commit 12fd0431 by Wee

test: add test suite for 1.3

parent ccf60f8a
import * as React from 'react'
import LiveRoute from '../src/index'
import { Route, Link, Switch, Router } from 'react-router-dom'
import { createMemoryHistory } from 'history'
import { render, fireEvent, cleanup } from 'react-testing-library'
const textGenerator = (name: string) => `🚥, ROUTE OF _${name}_`
const LinkGenerator = ({ to }) => (
<Link to={`/${to}`} id={`to${to.toUpperCase()}`} data-testid={`to${to.toUpperCase()}`}>
{`link_to_${to}`}
</Link>
)
const componentGenerator = (name: string) =>
class ClassComA extends React.Component {
render() {
return <h1>{textGenerator(name)}</h1>
}
}
function App() {
return (
<div>
<LiveRoute path="/a" livePath="/b" component={componentGenerator('live-on-b')} />
<LiveRoute path="/a" livePath={['/b', '/c']} component={componentGenerator(`live-on-bc`)} />
<LiveRoute path="/a" alwaysLive={true} component={componentGenerator('always-live')} />
<Route path="/b" render={() => <h1>{textGenerator('b')}</h1>} />
<Route path="/c" render={() => <h1>{textGenerator('c')}</h1>} />
<LinkGenerator to="a" />
<LinkGenerator to="b" />
<LinkGenerator to="c" />
</div>
)
}
// automatically unmount and cleanup DOM after the test is finished.
afterEach(cleanup)
// this is a handy function that I would utilize for any component
// that relies on the router being in context
function renderWithRouter(ui, { route = '/', history = createMemoryHistory({ initialEntries: [route] }) } = {}) {
return {
...render(<Router history={history}>{ui}</Router>),
// adding `history` to the returned utilities to allow us
// to reference it in our tests (just try to avoid using
// this to test implementation details).
history
}
}
function routesLives(container: HTMLElement, yesOrNot: 'yes' | 'not', expectLives: string[]) {
const judger = yesOrNot === 'yes' ? expect(container.innerHTML) : expect(container.innerHTML).not
expectLives.forEach(route => {
judger.toContain(`${textGenerator(route)}`)
})
}
test('live route through different urls', () => {
const leftClick = { button: 0 }
const { container, getByTestId } = renderWithRouter(<App />)
routesLives(container, 'not', ['live-on-b', `live-on-bc`, 'always-live', 'b', 'c'])
fireEvent.click(getByTestId('toA'), leftClick)
routesLives(container, 'yes', ['live-on-b', `live-on-bc`, 'always-live'])
routesLives(container, 'not', ['b', 'c'])
fireEvent.click(getByTestId('toB'), leftClick)
routesLives(container, 'yes', ['live-on-b', `live-on-bc`, 'always-live', 'b'])
routesLives(container, 'not', ['c'])
fireEvent.click(getByTestId('toC'), leftClick)
routesLives(container, 'yes', [`live-on-bc`, 'always-live', 'c'])
routesLives(container, 'not', ['live-on-b', 'b'])
})
import React from 'react'
import { BrowserRouter } from 'react-router-dom'
// Just render plain div with its children
const MockBrowserRouter = ({ children }) => <div>{children}</div>
export default MockBrowserRouter
{
"name": "react-live-route-demo-1",
"name": "react-live-route-demo",
"version": "1.0.0",
"description": "",
"keywords": [],
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -13,7 +13,8 @@
"dev": "tsc -b -w --pretty",
"prepare": "npm run build",
"lint": "tslint src",
"test": "jest"
"test": "jest",
"test:watch": "jest --watch"
},
"peerDependencies": {
"react": ">=15"
......@@ -35,6 +36,7 @@
"@types/react-router-dom": "^4.3.1",
"jest": "^24.5.0",
"jest-environment-jsdom": "^24.5.0",
"react-testing-library": "^6.0.0",
"ts-jest": "^24.0.0",
"tslint": "^5.14.0",
"tslint-config-alloy": "^0.2.1",
......
......@@ -18,15 +18,15 @@ enum LiveState {
type CacheDom = HTMLElement | null
interface IProps extends RouteProps {
livePath?: string
livePath?: string | string[]
alwaysLive: boolean
onHide?: Function
onReappear?: Function
name?: string
}
const debugLog = (message: any) => {
// console.log(message)
const debugLog = (...messages: string[]) => {
// console.log(...messages)
}
/**
......@@ -184,7 +184,7 @@ class LiveRoute extends React.Component<IProps, any> {
* Back up current router every time it is rendered normally, backing up to the next livePath rendering
*/
computeMatchWithLive(props, nextProps, nextContext, match) {
debugLog(`>>> ` + this.props.name + ` <<<`)
debugLog(`>>> name: ${this.props.name} <<<`)
// compute if livePath match
const { livePath, alwaysLive } = nextProps
const nextPropsWithLivePath = { ...nextProps, paths: livePath }
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -98,6 +98,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/runtime@^7.3.1", "@babel/runtime@^7.3.4":
version "7.3.4"
resolved "http://registry.npm.taobao.org/@babel/runtime/download/@babel/runtime-7.3.4.tgz#73d12ba819e365fcf7fd152aed56d6df97d21c83"
integrity sha1-c9ErqBnjZfz3/RUq7VbW35fSHIM=
dependencies:
regenerator-runtime "^0.12.0"
"@babel/template@^7.0.0", "@babel/template@^7.1.0", "@babel/template@^7.1.2", "@babel/template@^7.2.2":
version "7.2.2"
resolved "http://registry.npm.taobao.org/@babel/template/download/@babel/template-7.2.2.tgz#005b3fdf0ed96e88041330379e0da9a708eb2907"
......@@ -276,6 +283,11 @@
"@types/istanbul-lib-coverage" "^1.1.0"
"@types/yargs" "^12.0.9"
"@sheerun/mutationobserver-shim@^0.3.2":
version "0.3.2"
resolved "http://registry.npm.taobao.org/@sheerun/mutationobserver-shim/download/@sheerun/mutationobserver-shim-0.3.2.tgz#8013f2af54a2b7d735f71560ff360d3a8176a87b"
integrity sha1-gBPyr1Sit9c19xVg/zYNOoF2qHs=
"@types/babel__core@^7.1.0":
version "7.1.0"
resolved "http://registry.npm.taobao.org/@types/babel__core/download/@types/babel__core-7.1.0.tgz#710f2487dda4dcfd010ca6abb2b4dc7394365c51"
......@@ -984,6 +996,16 @@ diff@^3.2.0:
resolved "http://registry.npm.taobao.org/diff/download/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
integrity sha1-gAwN0eCov7yVg1wgKtIg/jF+WhI=
dom-testing-library@^3.13.1:
version "3.17.1"
resolved "http://registry.npm.taobao.org/dom-testing-library/download/dom-testing-library-3.17.1.tgz#3291bc3cf68c555ba5e663697ee77d604aaa122b"
integrity sha1-MpG8PPaMVVul5mNpfud9YEqqEis=
dependencies:
"@babel/runtime" "^7.3.4"
"@sheerun/mutationobserver-shim" "^0.3.2"
pretty-format "^24.0.0"
wait-for-expect "^1.1.0"
domexception@^1.0.1:
version "1.0.1"
resolved "http://registry.npm.taobao.org/domexception/download/domexception-1.0.1.tgz#937442644ca6a31261ef36e3ec677fe805582c90"
......@@ -2685,7 +2707,7 @@ prelude-ls@~1.1.2:
resolved "http://registry.npm.taobao.org/prelude-ls/download/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=
pretty-format@^24.5.0:
pretty-format@^24.0.0, pretty-format@^24.5.0:
version "24.5.0"
resolved "http://registry.npm.taobao.org/pretty-format/download/pretty-format-24.5.0.tgz#cc69a0281a62cd7242633fc135d6930cd889822d"
integrity sha1-zGmgKBpizXJCYz/BNdaTDNiJgi0=
......@@ -2789,6 +2811,14 @@ react-router@^4.3.1:
prop-types "^15.6.1"
warning "^4.0.1"
react-testing-library@^6.0.0:
version "6.0.0"
resolved "http://registry.npm.taobao.org/react-testing-library/download/react-testing-library-6.0.0.tgz#81edfcfae8a795525f48685be9bf561df45bb35d"
integrity sha1-ge38+uinlVJfSGhb6b9WHfRbs10=
dependencies:
"@babel/runtime" "^7.3.1"
dom-testing-library "^3.13.1"
react@^16.3.2:
version "16.7.0"
resolved "http://registry.npm.taobao.org/react/download/react-16.7.0.tgz#b674ec396b0a5715873b350446f7ea0802ab6381"
......@@ -2836,6 +2866,11 @@ realpath-native@^1.1.0:
dependencies:
util.promisify "^1.0.0"
regenerator-runtime@^0.12.0:
version "0.12.1"
resolved "http://registry.npm.taobao.org/regenerator-runtime/download/regenerator-runtime-0.12.1.tgz#fa1a71544764c036f8c49b13a08b2594c9f8a0de"
integrity sha1-+hpxVEdkwDb4xJsToIsllMn4oN4=
regex-not@^1.0.0, regex-not@^1.0.2:
version "1.0.2"
resolved "http://registry.npm.taobao.org/regex-not/download/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c"
......@@ -3539,6 +3574,11 @@ w3c-hr-time@^1.0.1:
dependencies:
browser-process-hrtime "^0.1.2"
wait-for-expect@^1.1.0:
version "1.1.0"
resolved "http://registry.npm.taobao.org/wait-for-expect/download/wait-for-expect-1.1.0.tgz#6607375c3f79d32add35cd2c87ce13f351a3d453"
integrity sha1-Zgc3XD950yrdNc0sh84T81Gj1FM=
walker@~1.0.5:
version "1.0.7"
resolved "http://registry.npm.taobao.org/walker/download/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment