-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from 4Catalyzer/named-pending
Support renderPending with named routes
- Loading branch information
Showing
4 changed files
with
185 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import resolver from '../src/resolver'; | ||
|
||
export function timeout(delay) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, delay); | ||
}); | ||
} | ||
|
||
export class InstrumentedResolver { | ||
constructor() { | ||
// This should be a rejected promise to prevent awaiting on done before | ||
// trying to resolve, but Node doesn't like naked unresolved promises. | ||
this.done = new Promise(() => {}); | ||
} | ||
|
||
async * resolveElements(match) { | ||
let resolveDone; | ||
this.done = new Promise((resolve) => { | ||
resolveDone = resolve; | ||
}); | ||
|
||
yield* resolver.resolveElements(match); | ||
resolveDone(); | ||
} | ||
} |
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,105 @@ | ||
import React from 'react'; | ||
import ReactTestUtils from 'react-dom/test-utils'; | ||
|
||
import createRender from '../../src/createRender'; | ||
import getFarceResult from '../../src/server/getFarceResult'; | ||
|
||
async function render(url, routeConfig) { | ||
const { element } = await getFarceResult({ | ||
url, | ||
routeConfig, | ||
render: createRender({}), | ||
}); | ||
|
||
return ReactTestUtils.renderIntoDocument(element); | ||
} | ||
|
||
describe('getFarceResult', () => { | ||
it('should support nested routes', async () => { | ||
const instance = await render( | ||
'/foo/baz/a', | ||
[ | ||
{ | ||
path: 'foo', | ||
Component: ({ children }) => <div className="foo">{children}</div>, | ||
children: [ | ||
{ | ||
path: 'bar', | ||
Component: () => <div className="bar" />, | ||
}, | ||
{ | ||
path: 'baz/:qux', | ||
Component: ({ params }) => ( | ||
<div className="baz">{params.qux}</div> | ||
), | ||
}, | ||
], | ||
}, | ||
], | ||
); | ||
|
||
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'foo'); | ||
expect( | ||
ReactTestUtils.scryRenderedDOMComponentsWithClass(instance, 'bar'), | ||
).toHaveLength(0); | ||
|
||
const bazNode = ReactTestUtils.findRenderedDOMComponentWithClass( | ||
instance, 'baz', | ||
); | ||
expect(bazNode.textContent).toBe('a'); | ||
}); | ||
|
||
it('should support named child routes', async () => { | ||
const instance = await render( | ||
'/foo/bar/qux/a', | ||
[ | ||
{ | ||
path: 'foo', | ||
Component: ({ nav, main }) => ( | ||
<div className="foo"> | ||
{nav} | ||
{main} | ||
</div> | ||
), | ||
children: [ | ||
{ | ||
path: 'bar', | ||
children: { | ||
nav: [ | ||
{ | ||
path: '(.*)?', | ||
Component: () => <div className="bar-nav" />, | ||
}, | ||
], | ||
main: [ | ||
{ | ||
path: 'baz', | ||
Component: () => <div className="baz" />, | ||
}, | ||
{ | ||
path: 'qux/:quux', | ||
Component: ({ params }) => ( | ||
<div className="qux">{params.quux}</div> | ||
), | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
); | ||
|
||
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'foo'); | ||
ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'bar-nav'); | ||
|
||
expect( | ||
ReactTestUtils.scryRenderedDOMComponentsWithClass(instance, 'baz'), | ||
).toHaveLength(0); | ||
|
||
const quxNode = ReactTestUtils.findRenderedDOMComponentWithClass( | ||
instance, 'qux', | ||
); | ||
expect(quxNode.textContent).toBe('a'); | ||
}); | ||
}); |