Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing tests by passing Router through render #349

Merged
merged 1 commit into from
Dec 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 96 additions & 26 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router, Link, route } from 'src';
import { h } from 'preact';
import { h, render } from 'preact';
import assertCloneOf from '../test_helpers/assert-clone-of';

chai.use(assertCloneOf);
Expand All @@ -12,14 +12,35 @@ describe('preact-router', () => {
});

describe('Router', () => {
let scratch;
let router;

beforeEach(() => {
scratch = document.createElement('div');
document.body.appendChild(scratch);
});

afterEach(() => {
document.body.removeChild(scratch);
router.componentWillUnmount();
});

it('should filter children based on URL', () => {
let router = new Router({});
let children = [
<foo path="/" />,
<foo path="/foo" />,
<foo path="/foo/bar" />
];

render(
(
<Router ref={ref => (router = ref)}>
{children}
</Router>
),
scratch
);

expect(
router.render({ children }, { url:'/foo' })
).to.be.cloneOf(children[1]);
Expand All @@ -34,13 +55,22 @@ describe('preact-router', () => {
});

it('should support nested parameterized routes', () => {
let router = new Router({});
let children = [
<foo path="/foo" />,
<foo path="/foo/:bar" />,
<foo path="/foo/:bar/:baz" />
];

render(
(
<Router ref={ref => (router = ref)}>
{children}
</Router>
),
scratch
);


expect(
router.render({ children }, { url:'/foo' })
).to.be.cloneOf(children[0]);
Expand All @@ -55,13 +85,21 @@ describe('preact-router', () => {
});

it('should support default routes', () => {
let router = new Router({});
let children = [
<foo default />,
<foo path="/" />,
<foo path="/foo" />
];

render(
(
<Router ref={ref => (router = ref)}>
{children}
</Router>
),
scratch
);

expect(
router.render({ children }, { url:'/foo' })
).to.be.cloneOf(children[2]);
Expand All @@ -76,32 +114,59 @@ describe('preact-router', () => {
});

it('should support initial route prop', () => {
let router = new Router({ url:'/foo' });
let children = [
<foo default />,
<foo path="/" />,
<foo path="/foo" />
];

render(
(
<Router url="/foo" ref={ref => (router = ref)}>
{children}
</Router>
),
scratch
);

expect(
router.render({ children }, router.state)
).to.be.cloneOf(children[2]);

expect(new Router({})).to.have.deep.property('state.url', location.pathname + (location.search || ''));
render(null, scratch);

render(
(
<Router ref={ref => (router = ref)}>
{children}
</Router>
),
scratch
);

expect(router).to.have.deep.property('state.url', location.pathname + (location.search || ''));
});

it('should support custom history', () => {
let push = sinon.spy();
let replace = sinon.spy();
let listen = sinon.spy();
let getCurrentLocation = sinon.spy(() => ({pathname: '/initial'}));
let router = new Router({
history: { push, replace, getCurrentLocation },
children: [
<index path="/" />,
<foo path="/foo" />,
<bar path="/bar" />
]
});

let children = [
<index path="/" />,
<foo path="/foo" />,
<bar path="/bar" />
];

render(
(
<Router history={{ push, replace, getCurrentLocation, listen }} ref={ref => (router = ref)}>
{children}
</Router>
),
scratch
);

router.componentWillMount();

Expand All @@ -121,23 +186,28 @@ describe('preact-router', () => {

describe('route()', () => {
let router;

before( () => {
router = new Router({
url: '/foo',
children: [
<foo path="/" />,
<foo path="/foo" />
]
}, {});
let scratch;

beforeEach(() => {
scratch = document.createElement('div');
document.body.appendChild(scratch);

render(
(
<Router url="/foo" ref={ref => (router = ref)}>
<foo path="/" />
<foo path="/foo" />
</Router>
),
scratch
);

sinon.spy(router, 'routeTo');

router.componentWillMount();
});

after( () => {
afterEach(() => {
router.componentWillUnmount();
document.body.removeChild(scratch);
});

it('should return true for existing route', () => {
Expand Down